Skip to content

Linux Foundations / Find your way around Linux ยท Step 57 of 63

Lesson: Emacs Buffer NavigationSwitch
โ† Linux Foundations

Shell and files

Emacs Buffer Navigation

Move through an Emacs buffer by character, word, line, screen and search.

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-f forward one character
  • C-b backward one character
  • C-n next line
  • C-p previous line

Within a line and by word

  • C-a goes to the beginning of the line.
  • C-e goes to the end of the line.
  • M-f moves forward one word.
  • M-b moves 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-v scrolls forward about one screen.
  • M-v scrolls back about one screen.
  • M-< goes to the start of the buffer.
  • M-> goes to the end of the buffer.
  • C-l redraws 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:

text
Goto line: 42

Repeating 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:

text
I-search: server

Press 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

  1. Create a long file: seq 1 200 > numbers.txt.
  2. Open it with emacs -nw numbers.txt.
  3. Press M->, then M-<.
  4. Go to line 100 with M-g g.
  5. Press C-u 10 C-n. You should be on 110.
  6. Search with C-s 150, press Enter, then exit with C-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.