How do I clear search highlights without disabling hlsearch permanently?
:nohlsearch
After a search in Vim, matched text is highlighted as long as hlsearch is enabled.
: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.
<C-r>/ or q/
Press q/ to open search history in a window.
set hlsearch
Use set hlsearch to highlight all matches of the last search pattern.
set ignorecase smartcase
Use ignorecase for case-insensitive search.
set incsearch
With set incsearch, Vim shows search matches as you type the pattern.
:%s/<C-r>a/replacement/g
In command-line mode, a inserts the contents of register a.
:let @/ = 'pattern'
Use :let @/ = 'pattern' to set the search register directly.
/<C-r>a
While in the search prompt (/), press a to insert the contents of register a.
: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).
:set shortmess-=S
The shortmess option controls which messages are shortened.
ciw + new text + Esc, then n.
The ciw command followed by new text, combined with and .
:s/pattern/replace/flags
The substitute command supports several flags that modify its behavior.
:%S/old/new/g (vim-abolish)
vim-abolish by Tim Pope provides :%S (Subvert), a substitute command that preserves the case pattern of the original text.