Vim marks are a surprisingly powerful tool for navigating large files, but most people only use them to jump back to where they were.

Let’s say you’re editing a long configuration file, and you need to reference a specific section repeatedly. You can use marks to quickly jump between these locations without endless scrolling or :set cursorline.

Imagine you’re in a file and you see a section you want to come back to. You’d type "A (that’s a double quote followed by A) to set a mark named A at your current cursor position. Now, no matter where you go in the file, you can instantly jump back to that spot by typing 'A (that’s a single quote followed by A).

This isn’t just for jumping back to the exact line. If you use '' (two single quotes), you’ll jump back to where you were before the last jump. This is incredibly useful for quickly toggling between two locations. For example, you find a variable definition, jump to its usage, make a change, and then '' takes you right back to the definition.

Vim has different types of marks:

  • Lowercase marks (a-z): These are local to the current buffer. When you close the file, these marks are gone.
  • Uppercase marks (A-Z): These are global. They persist even if you close the buffer, and can even persist across Vim sessions if viminfo is configured correctly.
  • Special marks:
    • ": The location of the last cursor position when you exited the buffer. This is what '' jumps to.
    • ': The location of the cursor when you last entered NORMAL mode.
    • ^: The location of the cursor when you last entered INSERT mode.
    • .: The location of the cursor after the last . command (change or delete).

Here’s a quick example of setting and jumping:

" In your file, navigate to a line.
:normal 10G " Jump to line 10
" Set a mark named 'x' at this position.
"mx
" Now, navigate somewhere else.
:normal 100G " Jump to line 100
" Jump back to the mark 'x'.
'x
" Notice you are back at line 10.
" Now jump back to line 100.
''

You can even use marks in motions. For instance, to delete from your current position to mark b, you’d type d'b. To yank from mark c to the end of the line, you’d type y followed by 'c and then A.

The :marks command is your friend here. Typing :marks will show you a list of all the marks you’ve set, along with their line numbers and the file they belong to. This is invaluable for understanding what marks are active and where they are.

:marks

This will output something like:

0       /path/to/your/file.txt
a       /path/to/your/file.txt
b       /path/to/another/file.txt
...

If you’re working with multiple files and want to persist marks between sessions, ensure your viminfo is set up. A typical line in your .vimrc might look like:

set viminfo='100,f100,'<'50,h " Preserves marks, history, etc.

The '<,'> part of the viminfo setting is specifically what tells Vim to save marks across sessions. Without it, your uppercase marks will vanish when you close Vim.

When you set a mark with mX (uppercase X), Vim stores the file name and the line number. If you later open a different file and try to jump to 'X, Vim will try to load that file and jump to the saved position. This is why uppercase marks are "global." If you have a mark A in file1.txt and then open file2.txt, typing 'A will attempt to switch to file1.txt and go to mark A.

The true power of marks lies in combining them with Vim’s motion commands. Instead of just jumping, you can operate on text between marks. For example, y'a yanks from the current cursor position to mark a. c'b changes text from the current cursor position to mark b. You can also use line-based marks: :'a,'b co 5 will copy the lines between mark a and mark b and paste them after line 5.

The most common pitfall is forgetting that uppercase marks are global and can potentially switch your current file context unexpectedly if you’re not careful. Always check :marks if you’re unsure where a mark might take you.

The next logical step after mastering marks is to explore Vim’s register system, which offers even more sophisticated ways to store and retrieve text and positions.

Want structured learning?

Take the full Vim course →