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

How do I extend a visual selection to a search match?

Answer

v/pattern<CR>

Explanation

Starting a search while in visual mode extends the selection to the search match. This is a precise way to select text up to a specific pattern.

How it works

  • Enter visual mode with v
  • Type /pattern to search forward
  • The selection extends from the original position to the match
  • Press n to extend further to the next match

Example

function process(data, options, callback) {

With cursor on p in process, press v/callback<CR> to select from process to callback.

Tips

  • ?pattern extends backward
  • n and N adjust the selection to subsequent matches
  • Combine with operators after selection
  • d/pattern<CR> deletes to the pattern without visual mode
  • /pattern/e includes the entire match in the selection

Next

How do you yank a single word into a named register?