Lesson 5 of 38 ยท Basics and Navigation
Moving the Cursor
Learn Emacs navigation: characters, words, lines, sentences, paragraphs, pages, buffer ends, go to line, recenter, and Lisp-aware movement by expressions and defuns.
Moving around is the first skill in any editor. Emacs offers a layered set of movement commands: small steps with C- keys, bigger steps with M- keys, and structural jumps with C-M- keys. The pattern is regular. Once you notice it, you can guess many commands before you look them up.
The pattern: smaller with Control, larger with Meta
| Unit | Forward | Backward | Mnemonic |
|---|---|---|---|
| Character | C-f |
C-b |
forward, backward |
| Word | M-f |
M-b |
|
| Line (vertical) | C-n |
C-p |
next, previous |
| Start / end of line | C-e |
C-a |
end, a (the first letter) |
| Sentence | M-e |
M-a |
like line, one size up |
| Paragraph | M-} |
M-{ |
|
| Expression (sexp) | C-M-f |
C-M-b |
|
| Defun (function) | C-M-e |
C-M-a |
end / beginning |
The arrow keys, Home, End, PageUp and PageDown also work, and you will use them while learning. The letter keys keep your hands on the home row, which is faster once they are muscle memory.
Going far
| Keys | Command | Effect |
|---|---|---|
M-< |
beginning-of-buffer |
Top of the buffer |
M-> |
end-of-buffer |
Bottom of the buffer |
C-v |
scroll-up-command |
Page down |
M-v |
scroll-down-command |
Page up |
C-l |
recenter-top-bottom |
Center the cursor line; press again to put it at the top, then the bottom |
M-g g or M-g M-g |
goto-line |
Prompt for a line number |
M-g c |
goto-char |
Prompt for a character position |
M-m |
back-to-indentation |
First non-blank character on the line |
M-r |
move-to-window-line-top-bottom |
Middle, then top, then bottom of the window |
C-l is easy to forget and extremely handy: when you jump to a spot and it sits at the bottom of the screen, C-l brings it to the middle.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
The | marks point. Notice that M-f moves to the end of the next word, and M-b to the beginning of the previous word.
Moving by structure
In Lisp, C, JavaScript and many other languages, the C-M- commands know about brackets:
C-M-fandC-M-bjump over a whole balanced expression: a parenthesized group, a string, a symbol.C-M-umoves up out of the enclosing parentheses;C-M-dmoves down into one.C-M-aandC-M-ejump to the start and end of the current function (a "defun").C-M-nandC-M-pmove to the next and previous parenthesized group at the same level.
These work in any programming mode and are quick for traversing code. C-M-f on the opening { of a block jumps past its closing }.
Lines that wrap
C-n and C-p move by visual lines in a wrapped paragraph. If you need physical lines, M-x toggle-truncate-lines stops wrapping. The variable line-move-visual controls the behavior. C-x C-n sets a "goal column" so vertical movement keeps to one column; C-u C-x C-n removes it.
Remember where you were
You do not have to navigate backwards by hand. Emacs remembers locations in a mark ring. After a big jump such as M->, press C-u C-SPC to go back to where you were. The mark lesson explains this fully, and it is one of the best habits to build early.
Practice routine
Open any long text file and try these moves for five minutes:
M->andM-<to hop between the ends.M-g gand a line number.C-vC-vM-vto page.C-lthree times to see the three positions.M-facross a line, thenC-aandC-e.
Avoid holding C-f or C-n for a long time. If you catch yourself doing that, a larger unit (word, paragraph, search) is almost always faster. The search lesson shows the fastest way to move to any visible or non-visible text.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Check your understanding
Quick check ยท 4 questions
of 4 answered You scored / 4 ยท perfect!
-
1. Which key moves the cursor forward by one word?
M-fis forward-word;C-fmoves a character. -
2. How do you jump to the end of the buffer?
M->is end-of-buffer andM-<is the beginning. -
3. Which command asks for a line number to jump to?
M-g g(goto-line) prompts for a number. -
4. What does
C-M-fdo in a programming mode?C-M-fis forward-sexp: it jumps across a whole bracketed group, string or symbol.
Summary
- Control keys move by small units (
C-f,C-b,C-n,C-p,C-a,C-e); Meta keys by larger ones (M-f,M-b,M-a,M-e,M-{,M-}). M-<,M->,C-v,M-v,M-g gandC-lhandle long distances.C-M-f,C-M-b,C-M-u,C-M-aandC-M-emove by code structure.C-u C-SPCreturns to a previous position.
Track your progress
Create a free account to tick off lessons and pick up where you left off on any device.
of Emacs lessons completed