How do I search for an exact multi-word phrase or special text I've selected?
Answer
* (in visual mode)
Explanation
In normal mode, * searches for the word under the cursor with word-boundary anchors. But in visual mode, pressing * searches forward for the exact text you have selected — including multi-word phrases, partial words, and text containing characters that would otherwise confuse a normal-mode * search. Press # to search backward instead.
How it works
- Enter visual mode (
v) and select the text you want to find - Press
*— Vim escapes the selection, builds a search pattern, and finds the next occurrence - Use
n/Nto cycle through subsequent matches
The visual * trick is particularly valuable for:
- Phrases with spaces (e.g.,
error connecting to) that normal*cannot match - Identifiers with dots or hyphens (e.g.,
my-config.key) where word boundaries break the match - Any text where typing the pattern manually would require heavy escaping
Example
With this buffer:
fmt.Println("hello world")
fmt.Println("hello world")
fmt.Fprintf(os.Stderr, "hello world")
Visually select fmt.Println (7 chars), then press *. Vim searches for fmt\.Println, jumping to the second occurrence — skipping the fmt.Fprintf line.
Tips
- Works in both character-wise (
v) and line-wise (V) visual mode - After pressing
*, you are back in normal mode andn/Nnavigate matches normally - The search is case-sensitive by default (respects
ignorecase/smartcase) - Many users map
*in visual mode in their config to make this behaviour always available, since it is not enabled in all Vim builds without a mapping