TOOL » VIM

Search

There are some special patterns to change how the search will be performed, or to match some special characters.

PatternDescription
\vEnable full regex (very magic).
\VDisable regex mode (very nomagic).
\cForce the search to be case insensitive.
\CForce the search to be case sensitive.
\nMatches the new line character.
\rMatches the carriage return character.

To search for the NULL character (indicated by ^@), type Ctrl+V 000 or \%x00.

viml
/search_text
/search_text\c  "Case insensitive"
/search_text\C  "Case sensitive"

n   "next matching search pattern"
N   "previous matching search pattern"

Highlight

Configure search highlight:

vim
set hlsearch
set nohlsearch

Stop the highlight for the last search:

vim
noh
nohlsearch

Find & Replace

On the find part, the syntax is the same as described on the search section.

On the replace part, the meaning of some patterns change:

PatternDescription
\nMeans the NULL character.
\rMeans a new line.
\0The whole match.
\1From 1 to 9, the Nth match.
vim
:s/before/after/g       "Current line"
:%s/before/after/g      "All lines"
:'<,'>s/before/after/g  "Only selection"
:%s/\v(\w+)/ \1 /g      "Captured group \1"

If the search/replace pattern contains a lot of slashes, use # as a separator:

viml
:s#usrbin#/usr/bin#g

Find & Delete

Delete lines that start with a space:

vim
:g/^\s.*$/d _

Delete empty lines:

vim
:g/^$/d _