Skip to content

Latest commit

 

History

History
711 lines (583 loc) · 43.9 KB

Keybindings.org

File metadata and controls

711 lines (583 loc) · 43.9 KB

Emacs Key Bindings

Emacs Key bindings

Default Key Bindings and Useful Commands

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:

Key Notation and Conventions

Key Notation

  • Some texts uses A - Alt instead of Meta.
KeyNameModern Keyboard Equivalence
MMetaAlt
AMetaAlt
CControlCtrl
SShiftShift
SPCSpaceSpace

Key Sequence

Prefix KeyPurpose
C-x <keys>General extend commands
C-h <keys>Help Commands
C-c <keys>Mode specific extended commands
M-xReserved to execute Emacs Commands.
C-gReserved to cancel Emacs commands.

Key Mnemonics

LetterMnemonic
pprevious
nnext
bbackward
fforward
abeginning
eend

See also:

Key Bindings and Commands by Task

Quitting

Key BindingCommand (M-x <cmd>)Description
kill-emacsEnd Emacs and save all buffers automatically.
C-x C-csave-buffers-kill-emacsAsk to kill each buffer and close Emacs.
C-gkeyboard-quitCancel any emacs command or prompt.
C-x kkill-bufferClose current buffer.
C z or C-x C-zsuspend-emacsIconify Emacs in graphical mode or suspend Emacs process when in terminal.

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

File

Key BindingFunctionDescription
C-x C-ffind-fileOpen File.
C-x C-vfind-alternate-fileOpen another file and kill current buffer.
C-x C-ssave-bufferSave Current Buffer.
C-x C-wwrite-fileSave Buffer as different file.
C-x ssave-some-buffersSave all buffers interactively.
C-u C-x sSave all buffers.
C-x C-Csave-buffers-kill-terminalCloses Emacs and asks to save each buffers.
C-x iinsert-fileInsert the content of a file at cursor position.
-revert-bufferUpdate a buffer when the file is changed on disk.
C-x C-qread-only-modeToggle read-only mode.

Buffers

Key BindingFunctionDescription
C-x bswitch-to-bufferSelect Another Buffer
C-x C-blist-buffersList all buffers
C-x kkill-bufferKill a buffer
C-x [right]next-bufferMove to next buffer
C-x [left]previous-bufferMove to previous buffer

Navigation

C-aGo to start of the line
C-eGo to end of the line.
C-kCut/Delete from cursor current position to the end of the line.
M-<Move to top of buffer
M->Move to Bottom of buffer
M-fMove forward one word
M-bMove 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

Bookmarks

C-x r mAdd current buffer to bookmarks.
C-x r bOpen a buffer from bookmarks.
C-x r lList bookmarks.

Search

C-sForward Search
C-rBackward Search
M-%Replace
C-sJump to next occurrence
C-rJump to previour occurrence
C-gExit search
M-s .Find Symbol under cursor
M-s wFind Symbol under cursor, match symbols with underscore, dot, hyphen ..
M-s oList all matching lines

Highlight

M-s-h-.Highlight symbol under cursor
M-s-h-pHighlight phrase
M-s-h-rHighlight regex
M-s-h-lHighlight lines matching a regex
M-s-h-uUnhighlight regex

Select, Copy, Cut and Paste

C - _Undo
C - x uRedo
C - SpaceBegin Selection
C - GCancel Selection
C-x hSelect the whole buffer
M-wCopy
C-yPaste (Yank)
C-wCut (Wipe out), Delete and copy to clipboard (Kill Ring)

Word Case / Comment and Uncomment

M-uMake word uppercase
M-lMake word lowercase
M-cMake word title (Capitalize only first letter)
M-;Comment/ Uncomment Selected Code

Windows

Key BindingCommand (M-x <cmd>)Description
C-x kkill-bufferClose current buffer
C-x bswitch-to-bufferSwitch to buffer
C-x C-blist-buffersChoose buffer Window
C-x 0delete-windowDelete Current Window
C-x 1delete-other-windowKeep the current window and delete the remaining windows.
C-x 2split-window-belowSplit Horizontally
C-x 3Split Vertically
C-x oSwitch Window

Frames

Key BindingCommand (M-x <cmd>)Description
C-x 5 0kill-bufferClose current frame
C-x 5 1delete-other-framesClose all frames except current one
C-x 5 2make-frame-commandCreate a new frame
C-x 5 oother-frameAlternate frame
C-z or C-x C-ziconify-or-deiconify-frameMinimize current frame
C-x 5 ffind-file-other-frameOpen file in a new frame.
C-x 5 rfind-file-read-only-other-frameOpen file in a new frame in read-only mode.
C-x 5 bswitch-to-buffer-other-frameSwitch 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.

Restricted Editing

Key BindingM-x <cmd>Description
C-x n nnarrow-to-regionNarrow to selected text (region)
C-x n wwidenBack to full buffer
C-x n dnarrow-to-defunNarrow 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.

Keyboard Macros

Key BindingM-x <cmd>
C-x (kmacro-start-macro
C-x )kmacro-end-macro
C-x e or f4kmacro-end-and-call-macro

Informationm, Documentation and Help

Key BindingCommand (M-x command)Description
C-h ?help-for-helpShow all commands to get help on Emacs.
C-h aaproposCommand Apropos
C-h iinfoInfo documentation reader
info-aproposSearch for a string in emacs info pages
womanBrowser Unix man pages
C-h fdescribe-functionDescribe function
C-h vdescribe-variableDescribe variable
C-h mdescribe-modeDescribe mode
C-h sdescribe-syntaxDescribe syntax
C-h bdescribe-bindingsDescribe key bindings for current buffer
C-h kdescribe-keyDescribe a particular key binding.
find-libraryFind a library *.el file loaded with require
Example: M-x find-library org

Selected Key Bindings and Commands for Programming

File Editing

C-x C-fOpen a new file or a existing file.
C-x C-sSave current buffer
C-x bSwithc Buffer
M-;Comment/ Uncomment Selected Code
M-/Autocomplete word (Equivalent to VIM Ctrl + p)
C-gCancel any Emacs command

Navigation

C-aMove the cursor to the beggining of line
C-eMove 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-charGot a character position
Mg-g <line-num>Go to line number
Mg-c <cursor-pos>Go to character position
M-mJump to first non whitespace in current line.
C-x r mMake bookmark
C-x r bJump to bookmarks

Text Editing

C-yCopy selected text by cursor (Copy region)
C-yPaste when no text is selected. (Yank)
C-kDelete current line from cursor position.
C-wCut (Wipe Out)
M-delDelete previous word
M-dDelete next word
C-x C-oDelete all empty line around the cursor.
M-qJustify / Re-align current paragraph
C-x C - (minus)Decrease font size
C-x C + (plus)Increase font size
M-x replace-stringBatch replace string in selected region or buffer.
M-x replace-regexpBatch replace regex pattern in selected text or buffer.
M-x regexp-builderBuild and test Emacs regex patterns.

Indentation

M-x <command>
back-to-identationM-mMove to the first non-whitespace character on the current line.
ident-regionC-M-\Indent all lines in a region.
ident-rigidlyC-x TABSelect 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

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.

KeyEmacsBash Shell
C-aMove the cursor to the beggining of linesame
C-eMove the cursor to the end of linesame
C-nMove the cursor to the next line (downward)Show next command in the history.
C-pMove the cursor to the previous line (upward)Show the previou command in the history.
C-jNew line character (same as Return)same
M-bMove the cursor backward one wordsame
M-fMove the cursor forward one wordsame
M-tSwap current word with previoussame
M-dDelete the next wordsame
C-kDelete the remaining of line from current cursor positionsame
C-yPaste the deleted line with (C-k) or copied line (Yank)same
M-wCopy the slected text
C-rSearch forward for a stringSearch for previous command
C-sSearch backward for a stringNo implemented.
c-lRedraws the screen with the cursor in the middle.Clear the screen.
C-_Undosame

See also:

Key Bindings for Dired-mode and commands to find files

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 bindingCommandDescription
C-x dM-x diredOpen directory, enters in dired mode
C-x 4 dM-x dired-other-windowOpen directory in other window
C-x 5 dM-x dired-other-frameOpen directtory in other frame
C-x C-fOpen a file on directory of dired buffer
M-x find-diredFind all files in directory matching a pattern.
M-x dired-omit-filesOmit garbage or unimportant files
fOpen file at point in current window
oOpen file at point in another window
+dired-create-directoryCreate a directory
(dired-hide-details-modeToggle hide/show details
C-x C-qMakes the dired buffer editable. When finishing
changing files enter C-c C-c
wCopy file name without directory
C-0 wCopy file name with full path
mMark file or directory in dired mode
uUnmark file
tMark/Unmark all files
X or !dired-o-shell-commandApply 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
sChange the sorting order of the directory
DDelete file
gUpdate dired buffer if directory has changed
RRename file
%RRename with regex
yShow file type
qKill dired buffer.
mdired-markMark current file
udired-unmarkUnmark current file.
grevert-bufferRevert dired buffer (updates it).
auto-revert-modeAuto update dired buffer.

Commands to find files

Find files in the computer:

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

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-nMove forward over a parenthetical group
C-M-pMove backward over a parenthetical group
C-M-fMove forward over a balanced expression
C-M-bMove backward over a balanced expression
C-M-kDelete s-expression under cursor
C-M-aMove to the beggining of current function
C-M-eMove to the end of current function

Useful Commands

Misc commands

CommandKey bindingsDescription
General
M-x kill-emacs-Exit Emacs.
M-x save-buffers-kill-terminalC-x C-cEixt Emacs and asks to save bufers.
M-x help-with-tutorialC-h tOpen Emacs built in tutorial
M-x suspend-emacsSuspend Emacs when in terminal or iconify in graphical mode.
M-x suspend-frameC-x C-zMinimize frame or suspend Emacs process in terminal. In terminal (Unix)
type fg to return to Emacs.
M-x quit-windowQuit Emacs.
Key bindings
global-set-keyDefine a global key interactively.
global-unset-keyUnset a global key interactively.
local-set-keyDefine a key binding to current major mode.
local-unset-keyUnset a key defined locally.
Edit
M-x delete-trailing-whitespaceDelete trailing Whistespace and ^M characters or \r from Dos or Windows.
M-x tabifyConverts all spaces to tab
M-x untabifyConverts all tabs to spaces.
M-x whitespace-modeToggle white space view.
Bufferf
M-x linum-modeToggle line numbers
M-x revert-fileUpdate buffer when file is changed externally.
M-x auto-revert-modeAuto update the current file when its changed externally.
M-x auto-revert-tail-modeUseful to watch log files. Follows tail of file. Equivalent to $ tail -f [file]
M-x rename-bufferRename current buffer.
M-x read-onlyC-x C-qToggle the current buffer read only.
M-x auto-fill-modeAutomatically format paragraphs. It works like M-q, but automatically.
Operating System
M-x shell-commandM-!Run a shell command and displays the output in a buffer.
M-x async-shell-commandM-&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-directoryPrompts for a directory path and creates a directory.
M-x compileRun 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.

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

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

EmacsVim/EvilDescription
C-njMove cursror down one line
C-pkMove cursor up one line
C-bhMove cursor left one character
C-flMove cursor right one character

VI Cursor motion:

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

Movement

Key SequenceDescription
Cursor Movements
^F (Ctrl - F)Forward Screen
^BBackward Screen
Line
0 or ^Begginging of line
$End of line
ggGo to first line of file
GGo to last line of the file.
Nth line or column
<n>ggGot to line n, 20gg go to line 20.
<n>|Got to to column n, ‘10|’ got to column 10.
Word
wMove forward by one word
bMove 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
gfOpen file name under cursor

Search

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

Editing

Key SequenceDescription
Undo / Redo
uUndo
Delete
xDelete char under cursor
XDelete char before cursor
ddDelete line
D (shift + d)Delete to the end of line
di’Delete text inside quotes
Copy
yyCopy line
Y or y$Copy from cursor to end of line.
Paste
pPaste after cursor
PPaste before cursor
Line
oAdd line below
OAdd line above
JJoin lines

Text Ident

Key SequenceDescription
vSelect text. Move the cursor to select text.
>Indent to right
<Indent to left
:set shiftwidth=4Set 4 spaces as indent size.

Commands

CommandDescription
:<Emacs M-x Command>Run any Emacs M-x command like M-x dired with :dired.
:wSave current buffer.
:sav <file name>Save the current buffer with another file name.
:qKill 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.
:lsList buffers.

Evil Mode Manual:

  • evil-manual1.pdf