How do I persistently highlight specific patterns with custom colors in Vim?
:call matchadd('Search', 'pattern')
The matchadd() function lets you add persistent highlights to patterns without affecting the search register.
:call matchadd('Search', 'pattern')
The matchadd() function lets you add persistent highlights to patterns without affecting the search register.
:lvimgrep /pattern/ %
While :vimgrep populates the global quickfix list, :lvimgrep uses the window-local location list instead.
:syntax match Conceal /pattern/ conceal
Vim's conceal feature lets you visually hide text that matches a pattern, or replace it with a single character.
:%s/\v(old)/\=toupper(submatch(0)[0]).tolower(submatch(0)[1:])/g
Standard substitutions don't preserve the original case of matched text.
<C-f> (from command-line mode)
When you are partway through typing a long or complex Ex command on the : prompt, you can press to open the command-line window.
/\(function\s\+\)\zs\w\+
Vim's \zs and \ze atoms let you control which part of a matched pattern gets highlighted and operated on.
/\zs and \ze
The \zs and \ze atoms let you define where the actual match starts and ends within a larger pattern.
/pattern1/;/pattern2/
Vim's search offsets allow chaining two patterns together with a semicolon.
/\Cpattern
Vim's ignorecase and smartcase settings change how all searches behave globally.
:helpgrep {pattern}
:helpgrep searches the full text of every Vim help file for a pattern and loads all matches into the quickfix list.
//
In Vim, pressing // (two forward slashes) in Normal mode repeats the last search pattern.
:nohlsearch
After a search in Vim, matched text is highlighted as long as hlsearch is enabled.
:/start/,/end/s/pattern/replacement/g
You can restrict a substitution to a range defined by two patterns.
\u and \l in :s replacement
Vim's substitute command supports special case modifiers in the replacement string that let you change the case of captured text on the fly.
/\(foo\)\@<=bar
Use \@<= for positive lookbehind.
:%s/; /;\r/g
In the replacement part, use \r to insert a newline.
:%s/\d\+/\=submatch(0)*2/g
Use \= to evaluate expressions.
/\Vexact.string
Prefix with \V for very nomagic mode where almost all characters are treated literally.
:vimgrep /pattern/ **/*.py
By specifying a file glob pattern with :vimgrep, you can restrict the search to specific file types.
/pattern\{-}
The \{-} quantifier in Vim regex matches as few characters as possible, unlike which matches as many as possible (greedy).