How do I see all search matches highlighted as I type the pattern?
Answer
:set incsearch hlsearch
Explanation
The combination of incsearch and hlsearch gives you live, interactive search highlighting. As you type your search pattern, Vim highlights the first match in real-time (incsearch), and once you press Enter, all matches throughout the file stay highlighted (hlsearch).
Setup
:set incsearch " Highlight matches as you type
:set hlsearch " Keep all matches highlighted after search
The workflow
- Press
/to start searching - As you type, the first match is highlighted in real-time
- Press
<CR>— all matches in the file are highlighted - Press
n/Nto navigate between matches - Press
:noh(or:nohlsearch) to clear the highlighting
Neovim bonus: highlight all matches while typing
Neovim adds an additional feature:
:set inccommand=split " Live preview of :s results in a split
:set inccommand=nosplit " Live preview inline
With inccommand, running :%s/old/new/g shows you a live preview of all replacements before you press Enter.
Recommended mappings
" Quick toggle for search highlight
nnoremap <leader>h :nohlsearch<CR>
" Clear highlight on pressing Escape twice
nnoremap <Esc><Esc> :nohlsearch<CR>
Tips
incsearchis on by default in Neovim but not in Vim- In Vim 8.2+,
incsearchalso works with:s— showing a preview of the first replacement - Use
<C-g>and<C-t>during incremental search to jump to the next/previous match while still typing hlsearchpersists until you explicitly clear it with:nohor start a new search- Add
set hlsearch incsearchto your vimrc for a permanent setup