Basic Keybindings
Quit Emacs
You can quit Emacs with the following keybinding:
C-x C-c
Where C
is the control
key.
Suspend Emacs
You can suspend Emacs using the following keybinding :
C-z
It gets you back to your shell. If you want to resume your emacs session, enter fg
in your terminal.
File handling
-
Re-Save open file under the same filename (Save):
C-x C-s
-
Write as
filename
(Save As):C-x C-w
filename
The new file name will be prompted in the minibuffer.
-
Create new file or load existing file (New / Load):
C-x C-f
filename
With the mnemonic here for f meaning file. You will be prompted for a file path in the minibuffer.
-
Visit alternate file
C-x C-f
If the file does not exist yet, you will be prompted the path of the file to create in the minibuffer.
Abort current command
Often you will get into a state where you have a partially typed command sequence in progress, but you want to abort it. You can abort it with either of the following keybindings:
C-g
EscEscEsc
Multiples windows or frames
“Window” in Emacs refers to what might otherwise be called a “pane” or “screen division”. Some window manipulation commands include:
- Split current window horizontally: C-x 2
- Split current window vertically: C-x 3
- Select next window: C-x o
- Close current window: C-x 0
- Close all other windows, except the current one: C-x 1
A “frame” in Emacs is what might otherwise be called a “window”. Frames are manipulated using these commands:
- Create new frame: C-x 5 2
- Delete current frame: C-x 5 0
- Delete other frames: C-x 5 1
Switching windows can be acheived using
- S-left, S-right, S-up, S-down (that is, Shift in conjunction with an arrow key) to switch to the neighboring window in a direction, or
- C-x o to switch to the next window.
Buffers
-
Example of a buffer list
CRM Buffer Size Mode Filename[/Process] . * .emacs 3294 Emacs-Lisp ~/.emacs % *Help* 101 Help search.c 86055 C ~/cvs/emacs/src/search.c % src 20959 Dired by name ~/cvs/emacs/src/ * *mail* 42 Mail % HELLO 1607 Fundamental ~/cvs/emacs/etc/HELLO % NEWS 481184 Outline ~/cvs/emacs/etc/NEWS *scratch* 191 Lisp Interaction * * Messages* 1554 Messages
The first field of a line indicates:
- ‘.’ the buffer is current.
- ‘%’ a read-only buffer.
- ‘*’ the buffer is modified.
-
Select buffer. You can select out of any open buffer with the following keybinding:
C-x b
You will be prompted for the buffer name you wish to switch to.
-
List buffers:
C-x C-b
-
Save-some-buffer, giving the choice which buffer to save or not:
C-x s
-
Kill one buffer:
C-x k
-
Operations on marked buffers:
S Save the marked buffers
A View the marked buffers in this frame.
H View the marked buffers in another frame.
V Revert the marked buffers.
T Toggle read-only state of marked buffers.
D Kill the marked buffers.
M-s a C-s Do incremental search in the marked buffers.
M-s a C-M-s Isearch for regexp in the marked buffers.
U Replace by regexp in each of the marked buffers.
Q Query replace in each of the marked buffers.
I As above, with a regular expression.
P Print the marked buffers.
O List lines in all marked buffers which match a given regexp (like the function `occur’).
X Pipe the contents of the marked buffers to a shell command.
N Replace the contents of the marked buffers with the output of a shell command.
! Run a shell command with the buffer’s file as an argument.
E Evaluate a form in each of the marked buffers. This is a very flexible command. For example, if you want to make all of the marked buffers read only, try using (read-only-mode 1) as the input form.
W - As above, but view each buffer while the form is evaluated.
k - Remove the marked lines from the Ibuffer buffer, but don’t kill the associated buffer.
x - Kill all buffers marked for deletion.
-
Save-some-buffer, giving the choice which buffer to save or not:
C-x s
-
Switch to the next buffer:
C-x RIGHT
-
Switch to previous buffer:
C-x LEFT
Search and Replace
In Emacs, basic search tool (I-Search
) allows you to search after or before the location of your cursor.
-
To search for sometext after the location of your cursor (
search-forward
) hit C-ssometext
. If you want to go to the next occurence ofsometext
, just press C-s again (and so on for the next occurences). When cursor lands in the right location, press Enter to exit the search prompt. -
To search before the location of your cursor (
search-backward
), use C-r the same way you usedbefore. -
To switch from
search-backward
tosearch-forward
, press 2 times C-s. And press 2 times C-r tosearch backward
when you’re insearch-forward
prompt. -
Search and replace:
M-% (or Esc-% )
oldtext
Enternewtext
Enter- Confirm: y
- Skip: n
- Quit: q
- Replace all: !
Region - Cut, Copy, Paste
-
Set mark in cursor location:
C-space or C-@
-
Kill region (Cut):
C-w
-
Copy region to kill ring:
M-w or Esc-w
-
Yank (Paste) most recently killed:
C-y
-
Yank (Paste) next last killed:
M-y or Esc-y
Kill
kill
is the command used by Emacs for the deletion of text. The kill
command is analogous to the cut
command in Windows. Various commands exist that ‘kills’ one word (M-d), the rest of the line (C-k), or larger text blocks. The deleted text is added to the kill-ring
, from which it can later be yanked
.
Select and cut (kill)
Killing and yanking
Similar to the select-and-cut
feature in Windows, here we have C-spc. The key binding C-spc will start the selection, the user can move the mark with the help of arrow keys or other command to make a selection. Once selection is complete - push the C-w to kill the selected text
Some basic commands which can be used as quick reference for kill
command (taken from Emacs tutorial)
M-DEL Kill the word immediately before the cursor M-d Kill the next word after the cursor C-k Kill from the cursor position to end of line M-k Kill to the end of the current sentence
Yank
yank
describes the insertion of previously deleted text, e.g. using C-y which yanks the most recently killed text. Yank
command is analogous to the paste
command in Windows.
Yank text killed previously
We know that the kill
command adds the text killed to a kill-ring
. To retrieve the deleted text from the kill-ring
use M-y command repeatedly until the desired text is yanked.
(Note: For the M-y key to work the previous command should be a YANK
otherwise it wouldn’t work)
Cursor (point) movement
In addition to cursor movements using the arrow keys, Home, End, Page up, and Page down, emacs defines a number of keystrokes that can move the cursor over smaller or larger pieces of text:
By character:
- Backward character: C-b
- Forward character: C-f
By word
- Backward word: M-b (i.e. Alt b, or Meta b)
- Forward word: M-f
By line:
- Beginning of current line: C-a
- Beginning of current line first(non-space)character:M-m
- End of current line: C-e
- Previous line: C-p
- Next line: C-n
Entire buffer:
- Beginning of buffer: M-<
- End of buffer: M->
By ‘block’, depending on context (mode):
Typical key bindings:
- Backward sentence/statement: M-a
- Forward sentence/statement: M-e
- Beginning of function: M-C-a
- End of function: M-C-e
Prefix arguments
In order to move several ‘steps’ at once, the movement commands may be given a prefix argument by pressing ESC or C-u and a number before the listed keystrokes. For C-u, the number is optional and defaults to 4.
E.g. ESC 3 C-n moves 3 lines down, while C-u M-f moves 4 words forward.
Undo
To undo something you just did:
C-_ or C-x u or C-/
Case
-
Capitalize word: M-c
-
Convert word to upper case: M-u
-
Convert word to lower case: M-l
Key bindings notation
Emacs’ documentation uses a consistent notation for all key bindings, which is explained here:
Key chords
A “key chord” is obtained by pressing two or more keys simultaneously. Key chords are denoted by separating all keys by dashes (-
). They usually involve modifier keys, which are put up front:
- C-: control;
- S-: shift;
- M-: alt (the “M” stands for “Meta” for historical reasons).
Other keys are simply denoted by their name, like:
- a: the
a
key; - left: the left arrow key;
- SPC: the space key;
- RET: the return key.
Examples of key chords thus include:
- C-a: pressing control and a simultaneously;
- S-right: pressing shift and right simultaneously;
- C-M-a: pressing control, alt and a simultaneously.
Key sequences
“Key sequences” are sequences of keys (or key chords), which must be typed one after the other. They are denoted by separating all key (or chord) notations by a space.
Examples include:
- C-x b: pressing control and x simultaneously, then releasing them and pressing b;
- C-x C-f: pressing control and x simultaneously, then releasing x and pressing f (since both chords involve the control modifier, it is not necessary to release it).
Using ESC instead of Alt
Key chords using the Alt modifier can also be entered as a key sequence starting with ESC. This can be useful when using Emacs over a remote connection that does not transmit Alt key chords, or when these key combinations are captured e.g by a window manager.
Example:
M-x can be entered as ESC x.
Describing key bindings in Emacs lisp files
The same notation that is described here can be used when defining key bindings in Emacs lisp files.
Example:
(global-set-key (kbd “C-x C-b”) ‘buffer-menu)
binds the key sequence C-x C-b to the command buffer-menu