Posted by: rydow | June 19, 2007

Cheat sheet for emacs

Hi again, time for some basic emacs tips:

Basics :

  • C-x-f find file (open file)
  • C-x-s save current buffer
  • C-x s save buffers that have been altered
  • C-x-w save as
  • C-x-c quit


  • C-h a cmd Get help on command
  • C-h k Answers “what does this key combination do?”

Killing and yanking :

  • C-k kill to end of line
  • M-d kill to end of word (adding to kill ring)
  • M-delete back-kill to start of word (adding to kill ring)
  • C-y yank (paste)
  • M-y yank previous(Do C-y M-y M-y M-y to yank third last kill)
  • C-space start mark
  • M-w kill from mark to here
  • C-insert copy as kill from mark to here

Navigating :

  • C-e goto end of current line
  • C-a goto beginning of current line
  • M-< goto beginning buffer
  • M-> goto end of buffer
  • M-x goto-line RET goes to specified line
  • C-M-f goto closing brace (standing on the opening brace)
  • C-M-b goto opening brace (standing on the closing brace)
  • C-u C-space which takes you back to wherever you were previously working
  • M-m position cursor on start of indentation

Window/Buffer commands:

  • C-x 2 Split window horizontally
  • C-x 3 Split window vertically
  • C-x 1 Close all other windows but the one where the cursor is
  • C-x 0 (Zero)Close this window, keep the other
  • C-x o (oh!) Jump to next window
  • C-x b View another buffer
  • C-x-b Pick another buffer to view
  • C-l Center buffer around line
  • C-u 1 C-l Make this line the top line of the buffer

Search and replace :

  • C-s incremental search forward
  • C-r incremental search backward
  • M-% Query replace
  • C-M-% Query replace regexp
  • M-x occur Find regexp in current buffer
  • M-x grep-find Find regexp recursively from a directory
  • M-x occur find occurences in this file, present as a list

Miscellaneous :

  • C-g quit whatever command (you did something you did not intend)
  • M-\ remove white space before and after cursor
  • M-^ join this line with the prevoius and fix white space
  • M-x delete-trailing-whitespace removes blanks after last char on all lines
  • C-x C-t transpose lines, move the current line one line upwards
  • M-t transpose words, swap the word behind the cursor for the one after
  • M-l make the rest of this word lower case
  • M-u make the rest of this word lower case
  • C-x C-l make region lower case
  • C-x C-u make region upper case

Keyboard Macros:

  • ‘C-x (‘ to begin recording
  • ‘C-x )’ to end recording
  • ‘C-x e’ to execute the macro (e.g. ‘C-u 25 C-x e’ to execute the macro 25 times)

Example : incremental searching (plain or regexp) within a macro to find the next place you need to munge and then use a combination of cursor positioning, killing, yanking, copying to registers and whatnot to munge the text however necessary. Then you just ‘C-x e’ to your heart’s content.

Odd ones, that are very useful every once in a while:

  • C-x r k kill rectangle
  • C-x r y yank rectangle

Navigating code :

  • M-. Jump to tag (where does this function reside? go there!)
  • M-* Go back to where i was before jumping to tag
  • M-x goto-line RET goes to specified line

To be able to use this feature you need to set up a TAGS file for your source tree. This may be done with the command ctags -e -R standing in the top level directory of your source. For more info on tags do “C-h a tag” in emacs and ctags –help in a shell.

Those are the ones that I use almost every day, apart from ediff, compiling and other things that will show up in a later post.


  1. thanks,
    was looking for the key-binding to jump to closing brace, and came across this.

  2. This is a very good tip especially to those fresh to
    the blogosphere. Brief but very precise information… Appreciate your sharing this one.
    A must read article!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: