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

How do I view or manipulate the search pattern register in Vim?

Answer

:echo @/

Explanation

The / register holds the most recent search pattern. You can read it to see what you last searched for, write to it to set a search pattern programmatically, or use it in substitutions and mappings for powerful search-based workflows.

How it works

  • @/ — the search pattern register (read-only in expressions)
  • :let @/ = 'pattern' — set the search pattern without moving the cursor
  • <C-r>/ — insert the current search pattern in insert or command mode
  • The pattern affects n, N, and hlsearch highlighting

Example

" See the current search pattern
:echo @/
" Output: \vfunction\s+\w+

" Set search pattern to highlight all TODO comments
:let @/ = 'TODO\|FIXME\|HACK'

" Use in substitution (empty pattern reuses last search)
/error<CR>
:%s//warning/g

Tips

  • :%s//replacement/g reuses the last search pattern — very useful after / or *
  • :let @/ = '' clears the search highlighting (like :noh but permanent)
  • histget('/') returns the nth search history entry
  • Use getreg('/') in scripts to safely access the search register

Next

How do I return to normal mode from absolutely any mode in Vim?