How do I use a named mark as a motion target for operators like delete, yank, or indent in Vim?
Answer
d'a
Explanation
Named marks are not just jump destinations — they serve as motion targets for any operator. d'a deletes all lines from the current line through the line that contains mark a, making marks a precise way to define operation ranges without counting lines or entering visual mode.
How it works
d— The delete operator, applied linewise when paired with a mark motion.'a— The mark motion: moves to the beginning of the line containing marka. The operation spans from the current line to that line, inclusive.
Any operator works with mark motions:
| Command | Effect |
|---|---|
y'a |
Yank from current line to mark a's line |
c'a |
Change from current line to mark a's line |
>'a |
Indent from current line to mark a's line |
='a |
Auto-indent from current line to mark a's line |
gq'a |
Reformat text from current line to mark a's line |
Example
Mark the start of a block, navigate to its end, then delete the entire block:
ma " set mark 'a' on the current line
10j " move 10 lines down
d'a " delete from here back up to the marked line
Tips
- Use backtick (
`a) instead of'for characterwise motion:d`adeletes from the cursor position to the exact character where markawas set. - Uppercase (global) marks work across files:
d'Adeletes from the current line to wherever markAwas set, even in another buffer. - Combine with Ex commands using marks as ranges:
:'a,.ddeletes from marka's line to the current line.