We recently learned about position-based ways to navigate and reason about text. While these coordinate motions are a broad way to navigate to any point in your buffer at all, it’s not often how we actually think about locations in the buffer. We don’t usually want to go to “line 57 col 6,” but we sometimes want to go to “that paragraph about Emacs” or “the section on Charles Dickens.” That is, while we often want to return to various positions in the buffer, these positions aren’t mere coordinates but places of interest.
Vim has three different mechanisms to give names to positions in the buffer that we can leverage to capture our intuition about places. The first of these is called “marks,” and allows you to name arbitrary positions in the buffer and return to them at any time. Vim also places some convenient marks on its own as you’re editing, which you can leverage in the same way. The names for marks are single characters, like a
.
- Mark current position —
m<name>
(e.g.ma
)- Uppercase marks (e.g.
mA
) preserve both position as well as filename, so you can use them to move across different buffers
- Uppercase marks (e.g.
- Go to marked line —
'<name>
(e.g.'a
) - Go to exact marked position —
`<name>
(e.g.`a
) - Go to most recent mark —
'' ``
- Vim automatically sets marks prior to each “jump.” So these commands are a common way to “go back to where I just was before this”
- Go backwards/forwards through marks in buffer —
[' ]' [` ]`
Secondly, Vim also keeps track of the locations of the “changes” that you make in the file, allowing you to navigate places you’ve been in your recent editing activity:
- Go to previous/next edit location —
g; g,
(I use these often, e.g. “What was I doing before this?”)
Observe how g;
and g,
mirror a similar idea in ;
and ,
which are used in f t
searches, so you can think of them in the same way.
And finally, Vim also keeps track of recent cursor positions, prior to any “jumps” (e.g. following a help link, or visiting the definition of an identifier when writing code):
- Jump back (o)ut to previous location / Jump back (i)n / Jump to definition —
C-o C-i C-]
(useful for navigating code)
Mark navigations within a buffer are motions, meaning they are nouns and can be combined with verbs. On the other hand, navigating changes and recent cursor positions are treated as “jumps” and are not nouns. They cannot be used with verbs. Lastly, unlike navigating (lowercase) marks and changes, jumps may move across files.