SaltyCrane Blog — Notes on JavaScript and web development

From Eclipse to Emacs

I am trying to learn Emacs after about a year of using Eclipse 3.2/3.3. See this post for some reasons why. Below is a list of Eclipse keyboard shortcuts and their Emacs equivalents. I'm using Emacs 22.1 for Windows.

Update 2009-07-20: After almost 2 years, I am still happily using Emacs (now Emacs 23 on Ubuntu). I made a few small corrections/updates to the table below.

ECLIPSE COMMANDSIMILAR EMACS COMMAND
(Emacs manuals use C for the CTRL key and M for the Meta or Alt key. So, e.g. C-x means CTRL+X. For Emacs long commands, you can type TAB to complete the command name.)

Cancel a command
ESC
Cancel a command
C-g

Open resource
CTRL+SHIFT+R
Find file
Keybinding: C-x C-f
Command: M-x find-file
Notes: Type in the file to open. Hit TAB for filename completion. Hit up and down arrows for recently used files.

Save file
CTRL+S
Save buffer
Keybinding: C-x C-s
Command: M-x save-buffer

Save As
Write file
Keybinding: C-x C-w
Command: M-x write-file

Close
CTRL+F4

Unlike Eclipse, Emacs' buffers and windows are distinct so closing a buffer and closing a window require separate commands.

Kill this buffer
Keybinding: C-x k
Command: M-x kill-this-buffer

Delete window
Keybinding: C-x 0
Command: M-x delete-window

Undo/Redo
CTRL+Z / CTRL+Y

Undo/ Undo Undo
Keybinding: C-_, C-x u, C-/
Command: M-x undo

To redo, interrupt the undo sequence by entering any command (e.g. C-f), then undo again. This undoes what you just undid, which is like redo. See 21.1 Undo in the GNU Emacs Manual.

This behavior can be confusing so there is also a RedoMode which behaves more like Eclipse and other editors. See RedoMode on the Emacs Wiki.

Switch editors
Keybinding: CTRL+F6 or CTRL+SHIFT+F6
Command: CTRL+3 Next Editor or CTRL+3 Previous Editor

Switch buffers
Keybinding: C-x RIGHTARROW or C-x LEFTARROW
See also: 24 Using Multiple Buffers in the Emacs Manual

Switch views
Keybinding: CTRL+F7 or CTRL+SHIFT+F7
Command: CTRL+3 Next View or CTRL+3 Previous View

After splitting into 2 windows with C-x 2, use C-x o to switch between windows.

To scroll the other window, use C-M-v. This is useful to reference one window while editing in the other.

To close the other window, type C-x 1 in the window you want to keep.

Find and Replace
Keybinding: CTRL+F
Incremental Search
Keybinding: C-s

Unconditional Replace
Command: M-x replace-string RET string RET newstring RET

Regexp Replacement
Command: M-x replace-regexp RET regexp RET newstring RET

See also: 20.9 Replacement Commands

Show line numbers
Keybinding: none
Command: CTRL+3 Show Line Numbers

Linum (Emacs >= 23 only)
Keybinding: none
Command: M-x linum-mode

For Emacs <= 22, look in your status bar for the line number or see http://www.emacswiki.org/cgi-bin/wiki/LineNumbers

Code Completion
Keybinding: CTRL+SPC

Dynamic Abbrev Expansion
Keybinding: M-/
Command: M-x dabbrev-expand

This command is much faster and simpler than Eclipse because it doesn't use the compiler/interpreter. However, I think it is less robust for the same reason. At this point, I think I like the Eclipse version better.

See also 34.6 Dynamic Abbrev Expansion in the manual.

Keys
Command: CTRL+3 Keys - General

Key Bindings
Keybinding: C-h b
Command: M-x describe-bindings

Comment
Keybinding: CTRL+/

Comment DWIM (Do What I Mean)
Keybinding: M-;
Command: M-x comment-dwim

Shift Right (or Left)
Keybinding: TAB (SHIFT+BACKSPACE)

Shift Region Right (or Left)
Keybinding: C-c > (C-c <)
Command: ???

Note: This doesn't work if you're in CUA mode (i.e. using C-x/C-c/C-v Cut and Paste)

Find text in Workspace
Keybinding: CTRL+ALT+G
Command: Find Text in Workspace

grep (and variants)
Command: M-x lgrep RET string RET filepattern RET directory RET.

See also 32.4 Searching with Grep under Emacs

I still miss this one in Eclipse. grep has more power than Eclipse, but the combination of pretty highlighting and searching across files is nice in Eclipse. I have created an Elisp function to at least grep for selected text with one key stroke. Put the following in your .emacs and you'll get something similar to CTRL+ALT+G in Eclipse.
;; my eclipse CTRL+ALT+G replacement
(defun grep-selected (start end)
  (interactive "r") 
  (grep (concat "grep -nH -e " 
                (buffer-substring start end)
                " * .*"))
(global-set-key "\C-\M-g" 'grep-selected)
I am just learning to customize emacs with Elisp so I'm sure there are improvements that can be made.

Comments


#1 Konstantin commented on :

For a code completion the Eclipse has the same functionality like emacs ,Hippy competition (keybinding is "Alt +/").


#2 Eliot commented on :

Konstantin, thanks for the tip!


#3 James commented on :

Kill buffer: C-x k
Close window: C-x 0


#4 Eliot commented on :

James, Thanks for the correction. I updated the table above.