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

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

  1. Press / to start searching
  2. As you type, the first match is highlighted in real-time
  3. Press <CR> — all matches in the file are highlighted
  4. Press n/N to navigate between matches
  5. 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

  • incsearch is on by default in Neovim but not in Vim
  • In Vim 8.2+, incsearch also 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
  • hlsearch persists until you explicitly clear it with :noh or start a new search
  • Add set hlsearch incsearch to your vimrc for a permanent setup

Next

How do I run the same command across all windows, buffers, or tabs?