Quick Emacs Reference

M = Alt or Esc key

C = Control key

  1. The usage of in the command description implies combination. E.g. M C-s means pressing Esc once followed by pressing Control and s together.
  2. For Meta-key-based (M) commands, the Alt key is generally combined with another following key stroke, while the Esc key is pressed once before the following key stroke. E.g. M-a could be achieved by pressing Alt and a together or Esc followed by a.
  3. The editing operations within Emacs happen in temporary buffers. Selecting a save operation transfers the buffer content to the file under consideration.
  4. One of the most powerful operations within Emacs is the ability to break a given window into multiple windows.
  5. Any mistake in typing down a command can be rectified by the quit operation (using C-g).
  6. If you don’t remember any command, the auto-completion mini-buffer in M-x mode is sometimes helpful. E.g. typing M-x comment and hitting TAB would give good hints about the possible command completion that you may like to do. I found out about M-x comment-region during latex editing by using this approach.

Basic Commands:

C-x C-f Create/open a file in buffer
C-x C-v Reload an existing open file
C-x C-s Save the file (from the buffer)
C-x C-w Save as
C-x k Kill buffer
C-x 0 Delete window
C-x 1 Delete all other windows (except the current one)
C-x 2 Split window row-wise
C-x 3 Split window column-wise
C-x o Shift focus to other (next) window
C-g Quit a command
C-x C-c Exit emacs

Navigation Commands:

M-x goto-line Go to a specified line number
C-a Go to the beginning of line
C-e Go to the end of line
M-< Go to the start of file
M-> Go to the end of file
C-v Page down
M-v Page up
C-M-n Go to the next parenthesis group
C-M-p Go to the previous parenthesis group
C-a Go to the beginning of line
C-e Go to the end of line

Editing Commands:

C-k Delete a line from the cursor position
C-/ Undo
SPACE C-/ Redo
C-SPACE Mark beginning of a region (for marking more than one line)
C-x h Select all
C-w Delete the marked region (or marked line)
M-w Copy the marked region (or marked line)
C-y Paste the copied region (through M-w) or deleted region (through C-w)
M-\ Delete all white-spaces around a point
M-SPACE Leave just one white-space around a point
M-% Query replace (Use ! for all following occurrences and ^ to go back to previous match)

Search Commands:

C-s I-search (incrementally search) string forward. To quit searching, press C-g.
C-r I-search (incrementally search) string backward. To quit searching, press C-g.
M-c In I-search mode (after invoking C-s or C-r), toggle case-sensitivity
C-w In I-search mode, add more characters to the search keyword on the fly
C-M-s Search (incrementally) regexp forward. To quit searching, press C-g.
C-M-r Search (incrementally) regexp backward. To quit searching, press C-g.
M-% Search and replace (Use y or n to control replacement for the current match; Use ! to replace all following occurrences; Use ^ to go back to previous match;)

Programming Commands:

M C-a Go to the beginning of a function
M C-e Go to the end of a function
M-C-f Match parenthesis forward
M-C-b Match parenthesis backward
C-c C-c Comment out marked area (marking done using C-SPACE)
C-u C-c C-c Uncomment marked area (marking done using C-SPACE)
TAB Indent the current line or marked region (marking done using C-SPACE)
M-x compile Prompts for compilation command. Type the following: g++ -g -o <file> <file>.cpp, where <file> is the generated debuggable executable, and <file>.cpp is the program being compiled.
M-x gdb Prompts for gdb command. Type the following: gdb <file>, where <file> is the executable generated by a debug build.

Shell Commands:

M-x shell Start a shell
M-! Run a shell command