Emacs Key Bindings

Table of Contents

Top

1 Emacs Key bindings

1.1 Default Key Bindings and Useful Commands

1.1.1 Overview

The popular Ctrl-v (paste), Ctrl-c (copy), Ctrl-z (undo) can be enabled by typing: M-x cua-mode or by inserting =(cua-mode) in Emacs configuration file. It also supports the Vim key bindings by installing the evil package (M-x package-install evil).

Note: The Emacs standard key bindings can be overridden by user configuration or modes like cua-mode or evil mode that emulates VI.

See also:

1.1.2 Key Notation and Conventions

Key Notation

Key Name Modern Keyboard Equivalence
M Meta Alt
A Meta Alt
C Control Ctrl
S Shift Shift
SPC Space Space
  • Some texts uses A - Alt instead of Meta.

Key Sequence

Prefix Key Purpose
C-x <keys> General extend commands
C-h <keys> Help Commands
C-c <keys> Mode specific extended commands
M-x Reserved to execute Emacs Commands.
C-g Reserved to cancel Emacs commands.

Key Mnemonics

Letter Mnemonic
p previous
n next
b backward
f forward
a beginning
e end

See also:

1.1.3 Key Bindings and Commands by Task

  1. Quitting
    Key Binding Command (M-x <cmd>) Description
      kill-emacs End Emacs and save all buffers automatically.
    C-x C-c save-buffers-kill-emacs Ask to kill each buffer and close Emacs.
    C-g keyboard-quit Cancel any emacs command or prompt.
    C-x k kill-buffer Close current buffer.
    C z or C-x C-z suspend-emacs Iconify Emacs in graphical mode or suspend Emacs process when in terminal.
         

    Note: Neither C-z or C-x C-c work in Cua mode.

  2. File
    Key Binding Function Description
    C-x C-f find-file Open File.
    C-x C-v find-alternate-file Open another file and kill current buffer.
    C-x C-s save-buffer Save Current Buffer.
    C-x C-w write-file Save Buffer as different file.
    C-x s save-some-buffers Save all buffers interactively.
    C-u C-x s   Save all buffers.
    C-x C-C save-buffers-kill-terminal Closes Emacs and asks to save each buffers.
    C-x i insert-file Insert the content of a file at cursor position.
    - revert-buffer Update a buffer when the file is changed on disk.
    C-x C-q read-only-mode Toggle read-only mode.
  3. Buffers
    Key Binding Function Description
    C-x b switch-to-buffer Select Another Buffer
    C-x C-b list-buffers List all buffers
    C-x k kill-buffer Kill a buffer
    C-x [right] next-buffer Move to next buffer
    C-x [left] previous-buffer Move to previous buffer
  4. Navigation
       
    C-a Go to start of the line
    C-e Go to end of the line.
    C-k Cut/Delete from cursor current position to the end of the line.
    M-< Move to top of buffer
    M-> Move to Bottom of buffer
    M-f Move forward one word
    M-b Move backward one word
    M-[left key] Move backward one word
    M-[right key] Move forward one word
    Mg-g <line-num> Go to line number
    Mg-c <cursor-pos> Go to character position
  5. Bookmarks
       
    C-x r m Add current buffer to bookmarks.
    C-x r b Open a buffer from bookmarks.
    C-x r l List bookmarks.
  6. Search
       
    C-s Forward Search
    C-r Backward Search
    M-% Replace
    C-s Jump to next occurrence
    C-r Jump to previour occurrence
    C-g Exit search
    M-s . Find Symbol under cursor
    M-s w Find Symbol under cursor, match symbols with underscore, dot, hyphen ..
    M-s o List all matching lines
  7. Highlight
       
    M-s-h-. Highlight symbol under cursor
    M-s-h-p Highlight phrase
    M-s-h-r Highlight regex
    M-s-h-l Highlight lines matching a regex
    M-s-h-u Unhighlight regex
  8. Select, Copy, Cut and Paste
       
    C - _ Undo
    C - x u Redo
    C - Space Begin Selection
    C - G Cancel Selection
    C-x h Select the whole buffer
    M-w Copy
    C-y Paste (Yank)
    C-w Cut (Wipe out), Delete and copy to clipboard (Kill Ring)
       
  9. Word Case / Comment and Uncomment
       
    M-u Make word uppercase
    M-l Make word lowercase
    M-c Make word title (Capitalize only first letter)
       
    M-; Comment/ Uncomment Selected Code
  10. Windows
    Key Binding Command (M-x <cmd>) Description
    C-x k kill-buffer Close current buffer
    C-x b switch-to-buffer Switch to buffer
    C-x C-b list-buffers Choose buffer Window
    C-x 0 delete-window Delete Current Window
    C-x 1 delete-other-window Keep the current window and delete the remaining windows.
    C-x 2 split-window-below Split Horizontally
    C-x 3   Split Vertically
    C-x o   Switch Window
  11. Frames
    Key Binding Command (M-x <cmd>) Description
    C-x 5 0 kill-buffer Close current frame
    C-x 5 1 delete-other-frames Close all frames except current one
    C-x 5 2 make-frame-command Create a new frame
    C-x 5 o other-frame Alternate frame
    C-z or C-x C-z iconify-or-deiconify-frame Minimize current frame
    C-x 5 f find-file-other-frame Open file in a new frame.
    C-x 5 r find-file-read-only-other-frame Open file in a new frame in read-only mode.
    C-x 5 b switch-to-buffer-other-frame Switch to buffer in a new frame.
         

    Note:

    • The key bind C-z is overridden by cua-mode if it enabled.
    • In terminal the key binding C-x C-z or the command M-x iconify-or-deiconify-frame suspends the Emacs process. To return to the process: type in the Unix shell fg and return.
  12. Restricted Editing
    Key Binding M-x <cmd> Description
    C-x n n narrow-to-region Narrow to selected text (region)
    C-x n w widen Back to full buffer
    C-x n d narrow-to-defun Narrow to an s-expression

    This set of commands allow to edit a limited area of the buffer.

    Notes: This key bindings are overridden by cua-mode and C-x cuts the selected text in this mode. If the keys are overridden then the M-x <command> command must be used or the commands assigned to a new key binding.

  13. Keyboard Macros
    Key Binding M-x <cmd>  
    C-x ( kmacro-start-macro  
    C-x ) kmacro-end-macro  
    C-x e or f4 kmacro-end-and-call-macro  
  14. Informationm, Documentation and Help
    Key Binding Command (M-x command) Description
    C-h ? help-for-help Show all commands to get help on Emacs.
    C-h a apropos Command Apropos
    C-h i info Info documentation reader
      info-apropos Search for a string in emacs info pages
      woman Browser Unix man pages
         
    C-h f describe-function Describe function
    C-h v describe-variable Describe variable
    C-h m describe-mode Describe mode
    C-h s describe-syntax Describe syntax
    C-h b describe-bindings Describe key bindings for current buffer
    C-h k describe-key Describe a particular key binding.
         
      find-library Find a library *.el file loaded with require
        Example: M-x find-library org

1.1.4 Selected Key Bindings and Commands for Programming

  1. File Editing
       
    C-x C-f Open a new file or a existing file.
    C-x C-s Save current buffer
    C-x b Swithc Buffer
    M-; Comment/ Uncomment Selected Code
    M-/ Autocomplete word (Equivalent to VIM Ctrl + p)
    C-g Cancel any Emacs command
       
  2. Navigation
       
    C-a Move the cursor to the beggining of line
    C-e Move the cursor to the end of line
    M-< Move the cursor to the beggining of buffer
    M-> Move to the cursor tor end of buffer
    M-x goto-char Got a character position
    Mg-g <line-num> Go to line number
    Mg-c <cursor-pos> Go to character position
    M-m Jump to first non whitespace in current line.
    C-x r m Make bookmark
    C-x r b Jump to bookmarks
  3. Text Editing
       
    C-y Copy selected text by cursor (Copy region)
    C-y Paste when no text is selected. (Yank)
    C-k Delete current line from cursor position.
    C-w Cut (Wipe Out)
    M-del Delete previous word
    M-d Delete next word
    C-x C-o Delete all empty line around the cursor.
    M-q Justify / Re-align current paragraph
       
    C-x C - (minus) Decrease font size
    C-x C + (plus) Increase font size
       
    M-x replace-string Batch replace string in selected region or buffer.
    M-x replace-regexp Batch replace regex pattern in selected text or buffer.
    M-x regexp-builder Build and test Emacs regex patterns.
       
  4. Indentation
    M-x <command>    
    back-to-identation M-m Move to the first non-whitespace character on the current line.
    ident-region C-M-\ Indent all lines in a region.
         
    ident-rigidly C-x TAB Select a region and type C-x TAB. Type right, left (move 1 space) or
        S-right, S-left (move 1 tab) to move the block to left or right.
        This keybinding doesn't work if cua-mode is activated.
         
         

    Indent entire buffer

    1. C-x h -> Select the whole buffer.
    2. C-M-\ or M-x indent-region

1.1.5 Ubiquitous Emacs Key Bindings

Some Emacs Key bindings (Emacs-style key bindings) are ubiquitous in Unix apps like Bash, Sh and all shells that uses the GNU readline library.

Key Emacs Bash Shell
C-a Move the cursor to the beggining of line same
C-e Move the cursor to the end of line same
     
C-n Move the cursor to the next line (downward) Show next command in the history.
C-p Move the cursor to the previous line (upward) Show the previou command in the history.
C-j New line character (same as Return) same
     
M-b Move the cursor backward one word same
M-f Move the cursor forward one word same
     
M-t Swap current word with previous same
     
M-d Delete the next word same
     
C-k Delete the remaining of line from current cursor position same
C-y Paste the deleted line with (C-k) or copied line (Yank) same
M-w Copy the slected text  
     
     
C-r Search forward for a string Search for previous command
C-s Search backward for a string No implemented.
     
     
c-l Redraws the screen with the cursor in the middle. Clear the screen.
     
C-_ Undo same
     

See also:

1.2 Key Bindings for Dired-mode and commands to find files

1.2.1 Dired mode key bindings

Dired mode provides a file manager in Emacs and it allows to browser directories, open files, apply operations like copy, move, rename on multiple files and apply shell commands on files. It can be invoked with C-x d or M-x dired.

Key binding Command Description
C-x d M-x dired Open directory, enters in dired mode
C-x 4 d M-x dired-other-window Open directory in other window
C-x 5 d M-x dired-other-frame Open directtory in other frame
C-x C-f   Open a file on directory of dired buffer
     
  M-x find-dired Find all files in directory matching a pattern.
  M-x dired-omit-files Omit garbage or unimportant files
f   Open file at point in current window
o   Open file at point in another window
+ dired-create-directory Create a directory
( dired-hide-details-mode Toggle hide/show details
     
C-x C-q   Makes the dired buffer editable. When finishing
    changing files enter C-c C-c
     
w   Copy file name without directory
C-0 w   Copy file name with full path
m   Mark file or directory in dired mode
u   Unmark file
t   Mark/Unmark all files
     
X or ! dired-o-shell-command Apply shell command to file at point. It can be used to
    extract archives like *.tar, *.zip
     
M-!   Apply shell command to marked files (selected with m)
     
&   Run async shell command on file at point
    or apply shell command to a list of files
s   Change the sorting order of the directory
D   Delete file
g   Update dired buffer if directory has changed
R   Rename file
%R   Rename with regex
y   Show file type
     
q   Kill dired buffer.
m dired-mark Mark current file
u dired-unmark Unmark current file.
g revert-buffer Revert dired buffer (updates it).
  auto-revert-mode Auto update dired buffer.
     

1.2.2 Commands to find files

Find files in the computer:

Command Description
M-x find-name-dired Find all files with given grep match.
   
M-x find-grep-dired Find all file names matching a shell wildcard pattern.
   
M-x find-dired Find all file names with find arguments specified by the user.
   
M-x rgrep Search files that contains some regular expression.
  Needs grep and find installed.
   
M-x lgrep Search for files matching a regular expression in a
  given directory without enter in subdirectories like rgrep.
   
M-x locate Search files using the mlocate app or locate database.
  Needs locate installed.
   

1.3 Key bindings for all Lisp dialects

Useful lisp key bindings to edit and navigate Lisp code.

Delimiter Wrapping

   
M-( Wrap selection in parentheses
M-[ Wrap selection in square brackets
M-{ Wrap selection in curly brackets
   

S-expression Navigation

   
C-M-n Move forward over a parenthetical group
C-M-p Move backward over a parenthetical group
C-M-f Move forward over a balanced expression
C-M-b Move backward over a balanced expression
C-M-k Delete s-expression under cursor
   
C-M-a Move to the beggining of current function
C-M-e Move to the end of current function

2 Useful Commands

2.1 Misc commands

Command Key bindings Description
General    
M-x kill-emacs - Exit Emacs.
M-x save-buffers-kill-terminal C-x C-c Eixt Emacs and asks to save bufers.
M-x help-with-tutorial C-h t Open Emacs built in tutorial
M-x suspend-emacs   Suspend Emacs when in terminal or iconify in graphical mode.
     
M-x suspend-frame C-x C-z Minimize frame or suspend Emacs process in terminal. In terminal (Unix)
    type fg to return to Emacs.
     
M-x quit-window   Quit Emacs.
     
Key bindings    
global-set-key   Define a global key interactively.
global-unset-key   Unset a global key interactively.
local-set-key   Define a key binding to current major mode.
local-unset-key   Unset a key defined locally.
     
Edit    
M-x delete-trailing-whitespace   Delete trailing Whistespace and ^M characters or \r from Dos or Windows.
M-x tabify   Converts all spaces to tab
M-x untabify   Converts all tabs to spaces.
M-x whitespace-mode   Toggle white space view.
     
Bufferf    
M-x linum-mode   Toggle line numbers
M-x revert-file   Update buffer when file is changed externally.
M-x auto-revert-mode   Auto update the current file when its changed externally.
M-x auto-revert-tail-mode   Useful to watch log files. Follows tail of file. Equivalent to $ tail -f [file]
M-x rename-buffer   Rename current buffer.
M-x read-only C-x C-q Toggle the current buffer read only.
M-x auto-fill-mode   Automatically format paragraphs. It works like M-q, but automatically.
     
Operating System    
M-x shell-command M-! Run a shell command and displays the output in a buffer.
     
M-x async-shell-command M-& Run asynchronous command like shells (python, ghci, scala …) and long running
    commands like ping and traceroute. It can launch any application without freeze Emacs.
     
M-x make-directory   Prompts for a directory path and creates a directory.
     
M-x compile   Run a compilation command. Type g to compile again.
     

Notes:

  • The command auto-revert-mode is useful to edit alongside IDEs like Visual Studio or Eclipse. If you change the file it is automatically updated.
  • It may be better to disable C-x C-c, since it is easy to accidentally hit and exit Emacs.

2.2 Change current buffer mode

it is useful to tell Emacs the file format to enable syntax highlight and mode functions when editing a script or configuration file without file extension like .gitconfig or python script without it.

  • M-x sh-mode Shell Script - sh extension
  • M-x pyhton-mode Example: A python script without extension ".py"
  • M-x conf-mode Configuration file like: .gitconfig, .gitignore, smb.conf

3 Evil Mode - VIM Emulation Mode

Note: EVIL - Is not what it seems. It stands for Extensible VI Layer. I this case "evil is good". It is an Emacs mode to emulate VI keybinds and can be installed with M-x package-install evil.

To enable evil mode permanently add to init file:

(require 'evil)
(evil-mode t)

To toggle evil mode type:

  • M-x evil-toggle

Basic Cursor Movement

Emacs Vim/Evil Description
C-n j Move cursror down one line
C-p k Move cursor up one line
C-b h Move cursor left one character
C-f l Move cursor right one character
     

VI Cursor motion:

     k
     |
h ---+--- l
     | 
     j

Movement

Key Sequence Description
Cursor Movements  
^F (Ctrl - F) Forward Screen
^B Backward Screen
   
Line  
0 or ^ Begginging of line
$ End of line
gg Go to first line of file
G Go to last line of the file.
   
Nth line or column  
<n>gg Got to line n, 20gg go to line 20.
<n>| Got to to column n, '10|' got to column 10.
Word  
w Move forward by one word
b Move backward by one word
Paragraph  
{ Go to beggining of next paragraph
} Go to end of next paragraph
Parenthesis  
% Find match brace and parenthesis.
File under cursor  
gf Open file name under cursor
   

Search

Key Sequence Description
/ Search forward
? Search backward
   
* Search for word under cursor. To find next word
  type n and previous type p.

Editing

Key Sequence Description
Undo / Redo  
u Undo
   
Delete  
x Delete char under cursor
X Delete char before cursor
   
dd Delete line
D (shift + d) Delete to the end of line
di' Delete text inside quotes
   
   
Copy  
yy Copy line
Y or y$ Copy from cursor to end of line.
   
Paste  
p Paste after cursor
P Paste before cursor
   
Line  
o Add line below
O Add line above
J Join lines
   
   
   
   

Text Ident

Key Sequence Description
v Select text. Move the cursor to select text.
> Indent to right
< Indent to left
:set shiftwidth=4 Set 4 spaces as indent size.
   

Commands

Command Description
:<Emacs M-x Command> Run any Emacs M-x command like M-x dired with :dired.
:w Save current buffer.
:sav <file name> Save the current buffer with another file name.
:q Kill buffer
:! <shell-commmand> Run shell command. Example: ':! uname -a'
:e <file name> Open file to edit. Equivalent to C-x C-f
:<line nth> Got to nth line. Example: ':20' got to line 20.
:ls List buffers.

Evil Mode Manual:

  • evil-manual1.pdf

Created: 2018-07-26 Thu 07:42

Emacs 25.3.1 (Org mode 8.2.10)

Validate