How do I search for visually selected text?
y/<C-r>"<CR>
To search for the exact text you have selected in visual mode, yank it and paste it into the search prompt.
y/<C-r>"<CR>
To search for the exact text you have selected in visual mode, yank it and paste it into the search prompt.
/\<word\>
The \ atoms create word boundaries in a search pattern, matching only complete words and not substrings within larger words.
/\v pattern
The \v flag enables "very magic" mode in Vim regex, where most special characters work like standard regular expressions without needing backslashes.
:%s/\(group\)/\1/g
Capture groups in Vim substitute let you match parts of a pattern and reuse them in the replacement.
:'<,'>s/old/new/g
After making a visual selection, you can run a substitute command that only affects the selected text.
:%s/pattern/\r/g
In the replacement part of :s, \r inserts a newline.
:%s/\(\w\+\)/\u\1/g
Vim provides case-conversion atoms in substitute replacements: \u uppercases the next character, \l lowercases it, \U uppercases until \e, and \L lowercases unt
n
After performing a search with / or ?, pressing n repeats the search in the same direction to find the next match.
/pattern\n next-line
Using \n in a Vim search pattern matches a newline character, allowing you to find patterns that cross line boundaries.
/pattern\@!
The \@! atom is a negative lookahead in Vim regex.
\@<=pattern
The \@<= atom is a positive lookbehind in Vim regex.
/pattern\ze followed
The \ze atom marks the end of the match, so you can match a pattern only when it appears before specific text.
:[{,]}s/old/new/g
By using the range [{,]}, you can limit a substitute command to the lines between the enclosing braces — effectively the current function or block.
:set hlsearch
The hlsearch option highlights all matches of the current search pattern throughout the file, making it easy to see where matches occur.
/{pattern}
The / command initiates a forward search in the file.
?{pattern}
The ? command searches backward from the cursor position.
:%s/\V literal.text/replacement/g
The \V (very nomagic) flag treats all characters as literal except for \.
:cdo s/old/new/g
The :cdo command executes a command on every entry in the quickfix list.
:set incsearch
The incsearch option enables incremental search, which highlights matches in real time as you type the search pattern.
/pattern/e
Search offsets let you place the cursor at a specific position relative to the match.