vimtricks.wiki Concise Vim tricks, one at a time.

How do I set a named mark so I can jump back to an exact position later?

Answer

m{a-z}

Explanation

Vim's mark system lets you bookmark positions in a buffer and return to them instantly. m{letter} sets a mark at the current cursor position; ' (single quote) + letter jumps to the line, and ` (backtick) + letter jumps to the exact line and column.

How it works

  • ma — set mark a at the current position
  • 'a — jump to the line where mark a was set (first non-blank character)
  • `a — jump to the exact line and column of mark a
  • Lowercase marks az are buffer-local (each buffer has its own set)
  • Uppercase marks AZ are global — they persist across files and Vim sessions (saved in .viminfo/shada)
  • :marks displays all currently set marks
  • :delmarks a deletes mark a; :delmarks! clears all lowercase marks

Example

Mark your position before a large search:

ma          " mark current position as 'a'
/somePattern
" ... navigate around ...
'a          " jump back to the marked line

Or use a global mark to jump between files:

mA          " set global mark 'A' in config.vim
" open another file...
'A          " jump back to config.vim at the marked line

Tips

  • Vim automatically sets special marks: `. (last change), `" (last exit position), '[ and '] (last yanked/changed region)
  • '' (double single-quote) jumps to the last jump position — a quick way to toggle between two locations
  • In a macro, set a mark before the operation and jump back to it at the end to reliably reposition the cursor
  • :marks abcABC lists only those specific marks

Next

How do I run a search and replace only within a visually selected region?