How do I view or manipulate the search pattern register in Vim?
:echo @/
The / register holds the most recent search pattern.
:echo @/
The / register holds the most recent search pattern.
:call matchadd('Search', 'pattern')
The matchadd() function lets you add persistent highlights to patterns without affecting the search register.
/pattern<CR>cgnreplacement<Esc>.
The gn text object selects the next search match, and cgn changes it.
:lvimgrep /pattern/ %
While :vimgrep populates the global quickfix list, :lvimgrep uses the window-local location list instead.
qa/pattern<CR>dd@aq
By starting a macro with a search command, the macro becomes conditional — it jumps to the next match before acting, and terminates when no more matches are f
:syntax match Conceal /pattern/ conceal
Vim's conceal feature lets you visually hide text that matches a pattern, or replace it with a single character.
:set grepprg=rg\ --vimgrep
Vim's :grep command uses an external program to search files and populate the quickfix list.
q/k?pattern<CR>
Vim's command-line history window (q: for Ex commands, q/ for search) opens a full editing buffer containing your history.
:%s/\v(old)/\=toupper(submatch(0)[0]).tolower(submatch(0)[1:])/g
Standard substitutions don't preserve the original case of matched text.
:keeppattern %s/old/new/g
When you run a :s or :%s substitute command, Vim updates the search register (@/) with the substitution pattern.
command-line #ex-commands #search #editing #registers #substitute
/\(function\s\+\)\zs\w\+
Vim's \zs and \ze atoms let you control which part of a matched pattern gets highlighted and operated on.
/\zs and \ze
The \zs and \ze atoms let you define where the actual match starts and ends within a larger pattern.
/pattern1/;/pattern2/
Vim's search offsets allow chaining two patterns together with a semicolon.
:set iskeyword+={char}
iskeyword defines which characters are considered word characters in Vim.
:%s/\s\+/ /g
The pattern \s\+ matches one or more whitespace characters (spaces, tabs).
:'<,'>s/pattern/replacement/g
When you make a visual selection and then type :, Vim automatically inserts ' as the range — the marks for the start and end of the last visual selection.
"/p
Vim stores the last search pattern in the search register "/.
/\Cpattern
Vim's ignorecase and smartcase settings change how all searches behave globally.
:helpgrep {pattern}
:helpgrep searches the full text of every Vim help file for a pattern and loads all matches into the quickfix list.
//
In Vim, pressing // (two forward slashes) in Normal mode repeats the last search pattern.