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 positionm{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:
- Press
mato set markaat your current position (line 42) - Press
ggto jump to the top of the file - After reviewing the code, press
'ato 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 (
a–z) per buffer — use them freely - Uppercase marks (
A–Z) 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 ato delete marka, ordelmarks!to delete all lowercase marks - Marks are saved in the
viminfofile (orshadain Neovim), so global marks persist across Vim sessions - Combine marks with operators:
d'adeletes from the cursor to the line of marka,y`ayanks from the cursor to the exact position of marka