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

How do I highlight a pattern without executing a search or moving the cursor?

Answer

:let @/ = "pattern"

Explanation

Assigning a string directly to the search register @/ with :let causes Vim to highlight all matches (if hlsearch is enabled) without performing a search or moving the cursor. This is useful when you want to mark occurrences of a term as a visual aid while keeping your cursor position.

How it works

  • @/ is the search register — it holds the most recent search pattern
  • :let @/ = "word" writes a new value into it, which Vim immediately uses for hlsearch highlighting
  • The cursor does not move, and the jumplist is not updated (unlike /word<CR>)
  • To clear the highlights without running :nohlsearch, set it to an empty string: :let @/ = ""

Example

You are reviewing code and want to highlight all occurrences of TODO across the buffer without jumping to the first match:

:let @/ = "TODO"

All TODO strings are now highlighted. Your cursor stays where it is. Press n to jump through them whenever you are ready.

Tips

  • Combine with set hlsearch in your vimrc so highlights are always active
  • Use <C-r>/ in command-line mode to paste the current search pattern into a command
  • In a mapping, you can set @/ to the word under the cursor with:
    :let @/ = expand('<cword>')
    
  • This technique is commonly used in custom mappings to implement *-like highlighting without moving the cursor

Next

How do I use a Vimscript expression as the replacement in a substitution?