Terminus
Searching in Vim

Searching in Vim

Search for next word (forward search)

To search in vim, open up your file with vim [.inline-code]vim myfile.txt[.inline-code], and press [.inline-code]ESC[.inline-code] to switch to normal mode. Type [.inline-code]/[.inline-code] followed by the word you are searching for. For example, if we want to search for ‘ERROR’ in our file. We type [.inline-code]/ERROR[.inline-code]. This will take us to the first occurrence of the word.

To find the next occurrence, simply type [.inline-code]n[.inline-code]. And to go back to the previous occurrence, type [.inline-code]N[.inline-code]. To stop searching press [.inline-code]ESC[.inline-code] to go normal mode.

[#search-for-previous-word]Search for previous word (backwards search)[#search-for-previous-word]

To search backwards in a file, open up the file, [.inline-code]vim myfile.txt[.inline-code], and press [.inline-code]ESC[.inline-code] to switch to normal mode. Type [.inline-code]?[.inline-code] followed by the word. If we are searching for the word ‘INFO’ backwards, we type [.inline-code]?INFO[.inline-code]. Type [.inline-code]N[.inline-code] to search backwards and [.inline-code]n[.inline-code] to search forwards. To stop searching press [.inline-code]ESC[.inline-code] to go normal mode.

[#for-current-word]Find current word[#for-current-word]

It is possible to find the current word the cursor is currently on. Open up a file in normal mode. Put the cursor on a word, then press [.inline-code]*[.inline-code] to find the next occurrence and [.inline-code]#[.inline-code] for the previous occurrence. On the bottom left corner, we can see the word that is being searched.

[#case-insensitive]Case insensitive search[#case-insensitive]

To ignore the case whilst searching, type [.inline-code]/[.inline-code] followed by a word, followed by [.inline-code]\c[.inline-code]. Let’s go through some examples, [.inline-code]/Linux[.inline-code] is case sensitive, [.inline-code]/Linux\C[.inline-code] is case sensitive, [.inline-code]/Linux\c[.inline-code] is case insensitive. It is also possible to set case sensitive search off in your vim config or the current file, by running [.inline-code]:set ignorecase[.inline-code] in normal mode.

[#highlight-matches]Highlight search[#highlight-matches]

To highlight search matches, set the [.inline-code]hlsearch[.inline-code] option by running [.inline-code]:set hlsearch[.inline-code] in normal mode or inside [.inline-code]~/.vimrc[.inline-code]. Now try searching for a word and it will be highlighted everywhere in the file. To clear search highlighting, run [.inline-code]:set nohlsearch[.inline-code] in normal mode.

[#line-starting-with-word]Search for any line starting with a word[#line-starting-with-word]

Starting with ‘def’: [.inline-code]/^def[.inline-code]

[#line-ending-with-word]Search for any line ending with a word[#line-ending-with-word]

Ending with ‘return': [.inline-code]/return$[.inline-code]

[#escape-special-characters]Escaping special characters[#escape-special-characters]

Find ‘[0]’: [.inline-code]/\[0\][.inline-code]

[#find-tabs-in-file]Find tabs in file[#find-tabs-in-file]

Use [.inline-code]/^][.inline-code], to insert the “tab” character, do not type [.inline-code]^][.inline-code], rather after typing [.inline-code]/[.inline-code] press the tab key.