Emacs Advent Calendar 01: Moving around in Text

Tue 2015-12-01

Everyone knows how to move around in texts using the arrow keys. Unsurprisingly this also works in Emacs, but there are tons of additional keybindings to take advantage of. Learning at least some of them enables you to move around without having to lift your hand away to the arrow keys.

The commands C-b and C-f move point (Emacs' name for the cursor) one character to the left and to the right respectively. Pressing C-p or C-n advances point one line up or down. Thinking of the directions as "backward", "forward", "previous" and "next" can serve as a mnemonic.

Pressing M-b or M-f moves point left or right one word at a time.

Several commands exist that move point by lines. The command C-a moves point to the beginning of the current line (like Home), C-e jumps to the end of it (like End). The commands M-< and M-> move point to the first or last line of the whole buffer.

Scrolling the buffer is done with C-v and M-v. Using the first one moves down one screen, the second one moves up.

Pressing M-g g (M-g M-g also works) makes Emacs ask for a line number to directly move point to.

Finally there is one thing to keep in mind: When lines are longer than the width of the window, Emacs wraps them around by default. Thus one logical line can take the space of multiple screen lines. Most commands operate on logical lines (e.g. C-a or C-e), but for some others (like C-n and C-p) it is more useful to work on screen lines.

All of these commands and some more are described in section 7.2 (Changing the Location of Point) in the Emacs Reference Manual.


This text by Ludger Sandig is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.