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

How do I position the cursor at a specific character offset from a search match?

Answer

/pattern/e+2

Explanation

Vim's search command supports an offset suffix that controls where the cursor lands after a match. The syntax /{pattern}/{offset} lets you position the cursor relative to the start or end of the match — useful when you need to land at a precise insertion point without manually navigating after the search.

How it works

The offset is appended after the pattern, separated by /:

  • /e — cursor lands at the end of the match
  • /e+N — N characters past the end of the match
  • /e-N — N characters before the end of the match
  • /s+N or /b+N — N characters after the start of the match
  • /s-N or /b-N — N characters before the start of the match
  • /+N — N lines below the match line
  • /-N — N lines above the match line

Example

Given this line:

const result = computeValue();
  • /computeValue/e+1<CR> — lands cursor on ( (1 char past end of "computeValue")
  • /computeValue/s-1<CR> — lands cursor on space before "computeValue"
  • /computeValue/e-4<CR> — lands cursor on V (4 chars before end)

Tips

  • Offsets persist: press n or N to repeat the search and land at the same offset each time
  • Combine with operators: c/result/e+1<CR> changes from cursor to just past the end of "result"
  • The same offset syntax works in Ex address ranges: :/foo/e,/bar/s-1 delete deletes precisely between two patterns
  • Use /e without a number to jump to the last character of a match — handy for ci" style edits where you need to start just inside a delimiter

Next

How do I encode and decode JSON data in Vimscript for configuration and plugin development?