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

How do I land on a specific position relative to a search match?

Answer

/pattern/e

Explanation

Search offsets let you place the cursor at a specific position relative to the match. The /e offset places the cursor at the end of the matched text instead of the beginning.

How it works

  • /pattern/e places the cursor at the end of the match
  • /pattern/e+1 places it one character after the end
  • /pattern/b+3 places it 3 characters from the beginning
  • /pattern/+2 places it 2 lines below the match

Example

Searching for function in:

function calculateTotal(items) {
  • /function lands on f
  • /function/e lands on n (end of "function")
  • /function/e+1 lands on the space after "function"

Tips

  • These offsets work with ? (backward search) too
  • /pattern/-1 positions one line above the match
  • Offsets are remembered for n/N repeats
  • Especially powerful with :s for targeted replacements

Next

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