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

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 mark a. 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`a deletes from the cursor position to the exact character where mark a was set.
  • Uppercase (global) marks work across files: d'A deletes from the current line to wherever mark A was set, even in another buffer.
  • Combine with Ex commands using marks as ranges: :'a,.d deletes from mark a's line to the current line.

Next

How do I inspect a Vim register's full details including its type (charwise, linewise, or blockwise) in Vimscript?