vimtricks.wiki Concise Vim tricks, one at a time.

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

  1. Enter visual mode (v) and select the text you want to find
  2. Press * — Vim escapes the selection, builds a search pattern, and finds the next occurrence
  3. Use n / N to 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 and n/N navigate 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

Next

How do I run text I've yanked or typed as a Vim macro without recording it first?