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

How do I paste or reuse my last search pattern from the search register?

Answer

"/p

Explanation

Vim stores the last search pattern in the search register "/. Like any named register, you can paste its contents into the buffer, insert it on the command line, or inspect it with :reg. This lets you reuse a complex regex you just searched for — in a substitute command, a :global, or anywhere else — without retyping it.

How it works

  • "/ is the search register — it always holds the most recent /, ?, *, or # search pattern
  • "/p pastes the search pattern into the buffer as text
  • <C-r>/ inserts the search pattern inline while typing an Ex command (e.g., in :s//replacement/g)
  • :let @/ = 'pattern' programmatically sets the search register — the next n/N will use that pattern
  • :reg / shows the current contents of the search register

Example

You searched for a long regex: /\v<get\w+Handler>

Now you want to use it in a substitute:

:%s/<C-r>//newHandler/g

Instead of retyping the pattern, <C-r>/ inserts it directly into the command line.

Or paste the pattern into a comment in your code:

O# Pattern used: <Esc>""/p

Tips

  • :%s//replacement/g (empty search field) automatically reuses the last search pattern — no need to reference "/ explicitly
  • After a * search, "/ holds \<word\> — you can paste the full word-boundary pattern anywhere
  • :let @/ = '' clears the search register and removes highlighting (a scriptable alternative to :nohlsearch)

Next

How do I run a search and replace only within a visually selected region?