Emacs lets you move without leaving the main keys. Arrow keys work too, but the commands below are fast and often work in the shell as well. C- means Ctrl and M- means Alt (Meta).
Characters and lines
The letters are abbreviations:
C-fforward one characterC-bbackward one characterC-nnext lineC-pprevious line
Within a line and by word
C-agoes to the beginning of the line.C-egoes to the end of the line.M-fmoves forward one word.M-bmoves backward one word.
A useful habit to notice: C- often works on characters and lines, while M- works on bigger units such as words.
Screens and the whole buffer
C-vscrolls forward about one screen.M-vscrolls back about one screen.M-<goes to the start of the buffer.M->goes to the end of the buffer.C-lredraws with the current line in the middle. Pressing it again moves that line to the top, then the bottom.
On most keyboards < and > need Shift, so press Alt and Shift together with the key.
Going to a line number
Press M-g g, type the number and press Enter:
Goto line: 42Repeating with a number
C-u followed by a number repeats the next command. C-u 8 C-n moves down eight lines.
Searching
C-s starts an incremental search. The cursor jumps as you type:
I-search: serverPress C-s again for the next match, or C-r to search backwards. Press Enter to stop at the current match. Press C-g to cancel and return to where you started (if the search is currently failing, the first C-g only removes the part that did not match).
For a regular expression search, use C-M-s. Emacs has its own regex syntax: for instance, "or" is written \|.
Terminal quirks
Some terminals use C-s and C-q for flow control, which can stop those keys reaching a program. Some terminals or desktops also block certain Alt shortcuts. If a Meta key does nothing, press Esc first and then the letter.
Try it
- Create a long file:
seq 1 200 > numbers.txt. - Open it with
emacs -nw numbers.txt. - Press
M->, thenM-<. - Go to line 100 with
M-g g. - Press
C-u 10 C-n. You should be on110. - Search with
C-s 150, pressEnter, then exit withC-x C-c.
Check your understanding
Emacs Buffer Navigation
All 5 sections ยท no timer
Unlimited retries
Questions from every section are combined into one assessment. Answer them all correctly to complete the article.
Your completion saves in this browser. Sign in to sync it to your account.