¶What will we learn?
We covered the basics of using Emacs in The Absolute Beginners’ Guide to Emacs but we didn’t really get a chance to talk about the key bindings you might want to use to navigate around file buffers.
In this video I’ll show you the built-in bindings you can use to move around in a file very efficiently without moving your fingers from the letter keys!
I will also tell you the exact Emacs commands that these keys bind to so that you can bind them to new keys if you like or even use them directly in Emacs Lisp code!
I recommend opening a text buffer in Emacs while watching this episode so that you can try these bindings and follow along.
¶Moving the Point
In Emacs, the term “point” refers to the location of the cursor in a given buffer. The point is the place where text will be inserted when you type and its location can be changed using movement keys.
Emacs allows you to use the arrow keys to move the point just like in many typical text editors.
In addition to the arrow keys, Emacs also provides a set of movement keys using alphabetical letters. The interesting thing is that these keys are chosen because of the words they map to so that they are easy to remember!
- The letter
f
stands for “forward” - The letter
b
stands for “backward” - The letter
n
stands for “next” - The letter
p
stands for “previous”
¶Basic Movements
Basic point movements are done using the keys I mentioned above in combination with the CTRL key:
C-f
executesforward-char
to move the point forward by one characterC-b
executesbackward-char
to move the point backward by one characterC-n
executesnext-line
to move the point forward by one line, keeping horizontal positionC-p
executesprevious-line
to move the point backward by one line, keeping horizontal position
The benefit of these bindings is that they are closer to where your fingers already rest on the keyboard. No more moving your right hand to the arrow keys just to navigate around the buffer!
The <LEFT>
and <RIGHT>
arrow keys are slightly different in that they call left-char
and right-char
which properly respect the direction right-to-left text!
¶Moving to Beginning and End of Line
It’s very common to want to move to the beginning or end of a line of text!
C-a
executesmove-beginning-of-line
which moves the point to the beginning of the lineC-e
executesmove-end-of-line
which moves the point to the end of the line
You can think of this like:
a
is the beginning of the English alphabete
stands for end of line
¶Moving to Beginning and End of Buffer
Sometimes you want to jump to the beginning or end of the buffer quickly!
M-<
executesbeginning-of-buffer
to move the point to the beginning of the bufferM->
executesend-of-buffer
to move the point to the end of the buffer.
If you’re using the GUI version of Emacs (not in the terminal) you can also press C-<HOME>
and C-<END>
to accomplish the same thing!
¶Moving to a Specific Line or Column
When a compiler tells you an error is on a specific line number, you’ll want to know how to get there quickly!
M-g M-g
(press Alt+g twice!) executesgoto-line
which prompts you for the line numberM-g <TAB>
executesmove-to-column
which prompts you for the column number
TIP: you can easily show line numbers in a buffer by running M-x display-line-numbers-mode
!
¶Moving by Words, Sentences, and Paragraphs
There are other bindings that move in larger steps in the buffer, usually corresponding to units that you understand like words, sentences, and paragraphs.
¶Words
To move forward and backward by words using the same keys, use ALT (Meta):
M-f
executesforward-word
to move the point forward by one wordM-b
executesbackward-word
to move the point backward by one word
Note that there is no equivalent for C-n
and C-p
!
¶Paragraphs
You can move between paragraphs using the following keys:
M-}
executesforward-paragraph
to move the point forward by one paragraphM-{
executesbackward-paragraph
to move the point backward by one paragraph
These keys can also be used in code! The definition of a “paragraph” is basically any section of text that is separated by blank lines (see documentation for the paragraph-start
variable).
¶Sentences
You can move between sentences using keys similar to jumping to beginning or end of the line:
M-e
executesforward-sentence
to move the point forward to the end of the sentence or the next sentenceM-a
executesbackward-sentence
to move the point backward to the beginning of the sentence or the preceding sentence
What qualifies as a sentence is a bit more complex (see documentation for the sentence-end
function). If the normal sentence patterns aren’t found, it defaults to following paragraph boundaries, so it works in code too!
¶Scrolling the Window
Aside from using the expected <PageUp>
and <PageDown>
keys for scrolling the window, you can also use these keys:
C-v
executesscroll-up-command
to move the window to the next page of content in the bufferM-v
executesscroll-down-command
to move the window to the previous page of content in the buffer
The reversed up/down terminology is a little confusing, but it’s a more literal description of the direction the buffer contents are moving.
¶“Centering” the Window
These key bindings can be helpful to move the scroll position to give you context above and below the current line:
C-l
executesrecenter-top-bottom
which scrolls the buffer so that the line where the point is positioned in the center of the window. If you press it repeatedly it will cycle so that the current line will show at the top of the window, then the bottom of the window, then at the center again!C-M-l
executesreposition-window
which will attempt to scroll the screen to fit as much of the current “thing” as possible in the window (current paragraph, current function definition, etc)
¶Searching for Text
You can easily move around in the buffer by searching for text with the following bindings:
C-s
executesisearch-forward
which prompts for the search term and places the cursor after the occurrence found after the point position after you press<RET>
(Enter)C-r
executesisearch-backward
which prompts for the search term and places the cursor before the occurrence found before the point position after you press<RET>
These commands are incremental which means that you see the results as you type!
If you keep the prompt open (without pressing <RET>
), you can keep pressing the C-s
and C-r
keys to navigate forward and backward in all the results for this string in the same buffer!
Tips:
- These commands can be useful to just jump forward or backward to a specific character or string on the same line!
- You can also set a mark just before searching to make it easy to select a specific region.
¶What’s next?
In the next video, we’ll talk about how you can select regions of text using Emacs’ “mark” commands!
After that, we’ll continue covering the fundamental key bindings for editing and manipulating text.