Skip to content
Tutorials Center
My learning

Topics

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.

JS Stand-in: simulate point movement

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-f and C-M-b jump over a whole balanced expression: a parenthesized group, a string, a symbol.
  • C-M-u moves up out of the enclosing parentheses; C-M-d moves down into one.
  • C-M-a and C-M-e jump to the start and end of the current function (a "defun").
  • C-M-n and C-M-p move 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:

  1. M-> and M-< to hop between the ends.
  2. M-g g and a line number.
  3. C-v C-v M-v to page.
  4. C-l three times to see the three positions.
  5. M-f across a line, then C-a and C-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.

JS Stand-in: paragraph jumps

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. 1. Which key moves the cursor forward by one word?

    M-f is forward-word; C-f moves a character.
  2. 2. How do you jump to the end of the buffer?

    M-> is end-of-buffer and M-< is the beginning.
  3. 3. Which command asks for a line number to jump to?

    M-g g (goto-line) prompts for a number.
  4. 4. What does C-M-f do in a programming mode?

    C-M-f is 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 g and C-l handle long distances.
  • C-M-f, C-M-b, C-M-u, C-M-a and C-M-e move by code structure.
  • C-u C-SPC returns to a previous position.

Next: Files, Saving and Quitting