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

How do I delete just the current search match with dgn?

Answer

*Ndgn

Explanation

When you are reviewing repetitive text, you often need to remove one specific match without running a broad substitute. dgn is powerful for this, but it operates on the next search match; combining it with *N anchors the search pattern to the word under your cursor and then jumps back so dgn targets the current match first. This gives you precise, repeatable control in dense files.

How it works

  • * sets the search pattern to the word under the cursor and jumps to the next match
  • N jumps back to the previous match, which puts you on the current occurrence again
  • dgn deletes the next match of the active search pattern

After this first delete, you can continue with dgn for additional matches, or use normal navigation between edits. This is especially useful when you want command-level precision without entering command-line substitution syntax.

Example

Before (cursor on first foo):

foo foo foo

Run:

*Ndgn

After:

 foo foo

Tips

  • Use this when %s/// would be too broad or risky
  • If you want replacement instead of deletion, use the same anchoring idea with *Ncgn

Next

How do I uppercase text inside an HTML tag without changing the tags?