Movement by chars, words, lines
As you already noticed, regular arrow keys work as expected. Many proponents of Emacs (and Vim, for that matter) really dislike the arrow keys for a simple reason: using arrows requires to move the hand off the "home row" --- the central position on the keyboard --- and to the side. In Vim this problem is mitigated by using h
, j
, k
and l
keys instead of arrows, because Vim is a modal editor, which means those keys, and all other letters, can be used for commands in the "normal mode", and to actually type the letters the user needs to switch to an "insertion mode".
Emacs is not modal (but, with special extensions, it can be), so arrow movements are bound to combinations:
Meaning | Emacs | macOS | Windows |
---|---|---|---|
Up | C-p | Up | Up |
Down | C-n | Down | Down |
Left | C-b | Left | Left |
Right | C-f | Right | Right |
Yeah, I know... There are good news though: you don't have to use them, arrows are just fine (I use arrows all the time, it's not that big of a movement on a compact keyboard) and you rarely need to move by one character anyway. Emacs provides many ways to effectively navigate text, and as you learn them, over time per-character navigation will become less and less desirable.
But wait, here comes the reason to use them! These combinations have a strong semantic consistency. Firstly, the same Control
can move to the beginning and end of line:
Meaning | Emacs | macOS | Windows |
---|---|---|---|
Beginning of line | C-a | Cmd-Left | Home |
End of line | C-e | Cmd-Right | End |
Secondly, if you press M
(reminder: M
is a Meta-key --- Alt/Option) instead of Control
when moving sideways, the movement "elevates" to the next semantic level. Instead of moving by letters, you move by words:
Meaning | Emacs | macOS | Windows |
---|---|---|---|
Previous word | M-b | Alt-Left | C-Left |
Next word | M-f | Alt-Right | C-Right |
Now, this is where the semantic consistency goes a bit haywire. If you press M-a
instead of C-a
, you move one sentence left. Same for M-e
:
Meaning | Emacs | macOS | Windows |
---|---|---|---|
Beginning of sentence | M-a | ||
End of sentence | M-e |
There are also ways to move by paragraphs and get to the beginning and end of the buffer. More about buffers later. Finally, one of the best features ever --- move to first non-whitespace character on the line with M-m
. Especially useful when writing code with a lot of indentation.
Here's the summary of all the basic movements:
From now on, we'll display related key combinations (bindings) and commands (actual Emacs functions) in a single table, while omitting macOS/Windows columns, since there rarely are any comparable OS key combinations. Here is the first such table, with all basic movements:
Meaning | Binding | Command |
---|---|---|
Up | C-b | previous-line |
Down | C-p | next-line |
Left | C-n | left-char |
Right | C-f | right-char |
Previous word | M-b | backward-word |
Next word | M-f | forwar-word |
Beginning of line | C-a | beginning-of-visual-line |
End of line | C-e | end-of-visual-line |
Beginning of sentence | M-a | org-backward-sentence |
End of sentence | M-e | org-forward-sentence |
First non-whitespace char | M-m | back-to-indentation |