Learn Practice Daily golf Reference

How to search in Vim

In Normal mode, type / and the word, then press ↵ to search forward. n goes to the next match and N to the previous one. Use ? instead of / to search backwards.

Try it here

Find the TODOs

Search for TODO with /TODO and ↵, and delete the line with dd. Then n finds the next one, and . repeats the delete.

1 of 2Search for TODO, then dd
1import { send } from './mail';
1
2export function remind(user) {
3 // TODO: check the opt-out flag
4 const subject = 'Your basket misses you';
5 const body = render('basket', user);
6 // TODO: limit to one a week
7 return send(user.email, subject, body);
8}
NORMALLoading the editor…1/20.0s1:1

This editor needs a keyboard. Read on, and play it at your computer.

TypeWhat it does
/word ↵Search forward for word.
?word ↵Search backwards.
n / NNext match / previous match. After a ? search, n keeps going backwards.
* / #Search forward / backwards for the whole word under the cursor.
/word\c ↵Ignore case for this search: \c anywhere in the pattern. \C forces case to match.
/\<word\> ↵Only the whole word, not password or wordy.
/ ↵Search forward for the last pattern again.
/ then ↑Step back through earlier searches (type a few letters first to see only those that start with them).
:noh ↵Clear the highlighting until the next search.

Case-insensitive search

Searches match case by default, so /todo won't find TODO. For one search, add \c to the pattern: /todo\c. To change it for good, turn on two options together: :set ignorecase smartcase. ignorecase ignores case everywhere; smartcase brings it back as soon as you type a capital letter, so /todo finds every spelling and /Todo finds only Todo. Put set ignorecase smartcase in ~/.vimrc, or vim.opt.ignorecase = true and vim.opt.smartcase = true in Neovim's init.lua. The setting applies to :s patterns too; smartcase doesn't apply to * and #.

Clear the search highlight

With the hlsearch option on, every match stays highlighted after a search. :noh (short for :nohlsearch) and ↵ hides the highlight until your next search; :set nohlsearch turns it off altogether. Neovim has hlsearch on from the start and clears it with Ctrl-l as well. Vim starts with it off; :set hlsearch turns it on.

incsearch is the other display option: it shows the first match while you are still typing the pattern. Neovim has it on, and Vim's defaults.vim turns it on.