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

How do I set a mark and jump back to it later?

Answer

ma and 'a

Explanation

The ma command sets a mark named a at the current cursor position. You can then jump back to that exact position at any time by pressing 'a (single quote + mark name) to go to the line, or `a (backtick + mark name) to go to the exact line and column.

How it works

  • m{a-z} sets a local mark (scoped to the current buffer) at the cursor position
  • m{A-Z} sets a global mark (works across files) at the cursor position
  • '{mark} jumps to the first non-blank character of the marked line
  • `{mark} jumps to the exact line and column of the mark

Example

You are editing a function on line 42 and need to check something at the top of the file:

  1. Press ma to set mark a at your current position (line 42)
  2. Press gg to jump to the top of the file
  3. After reviewing the code, press 'a to jump back to line 42

Your mark persists until you set it again or close the buffer (for lowercase marks).

Viewing all marks

Use :marks to see a list of all currently set marks with their line numbers, column positions, and file names.

Special marks

  • `. — position of the last change
  • `" — position when you last exited the current buffer
  • `0 — position when you last exited Vim
  • '' — position before the last jump

Tips

  • You have 26 local marks (az) per buffer — use them freely
  • Uppercase marks (AZ) are global and persist across files, letting you jump between buffers
  • Use ]' to jump to the next line with a lowercase mark, and [' for the previous
  • Use delmarks a to delete mark a, or delmarks! to delete all lowercase marks
  • Marks are saved in the viminfo file (or shada in Neovim), so global marks persist across Vim sessions
  • Combine marks with operators: d'a deletes from the cursor to the line of mark a, y`a yanks from the cursor to the exact position of mark a

Next

How do I edit multiple lines at once using multiple cursors in Vim?