Kurt Keller's Blog

tech notes and more

search options

Searching in vim with / ? n N should be well known to all users. How searches behave can be controlled by several options.

highlighting

:set hlsearch " highlight all search results

With the hlsearch turned on, all matches to your search are highlighted. From time to time, however, having too much highlighted after you’ve found your desired match can be distracting. The current highlighting can be cleared without turning off hlsearch by simply using

:nohl

case

:set ignorecase " ignore case when searching
:set smartcase  " do not ignore case for search terms with upper case letters

With ignorecase the search is performed in a case insensitive manner, with noignorecase searches are always case sensitive. There is also a mode which brings together the best of both settings. When, in addition to ignorecase, the option smartcase is also activated, then searches are done in a case insensitive manner unless the search term contains one or more upper case letters, in which case the matching is case sensitive.

incremental

:set incsearch " incremental search

Usually the whole search string must be typed after / or ?. With incsearch the search is done incrementally. As soon as you type the first character of the search string, vim highlights the possible matches (provided hlsearch is set) and temporarily moves the cursor to the first match. The more letters you type, the further you narrow down the possible matches. ESC will cancel the search and return the cursor to the position where it was when you started searching, ENTER completes the search and moves the cursor to the first match.

Share

Leave a Reply