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

How do I search for the word under the cursor without whole-word boundaries?

Answer

g*

Explanation

The g* command searches forward for the text under the cursor without adding word boundary anchors. Unlike * which matches only whole words (wrapping the pattern in \< and \>), g* finds partial matches inside longer words too. This is useful when you want to find all occurrences of a substring, not just exact standalone matches.

How it works

  • * searches for the word under the cursor with word boundaries: /\<word\>/
  • g* searches for the word under the cursor without boundaries: /word/
  • g# does the same as g* but searches backward
  • # searches backward with word boundaries (the reverse of *)

The difference is subtle but important when your search target appears as part of other words.

Example

Given the text with the cursor on port:

port = 8080
import os
export default
report = generate_report()
transport_layer = None

Pressing * searches for /\<port\>/ and matches only the standalone port on line 1.

Pressing g* searches for /port/ and matches port inside all five lines: port, import, export, report, generate_report, and transport_layer.

Tips

  • Use * (with boundaries) when renaming a variable — you want exact matches only
  • Use g* (without boundaries) when searching for a substring pattern across the codebase
  • Both * and g* set the search register, so you can follow up with :%s//replacement/g to substitute all matches without retyping the pattern
  • After pressing * or g*, use n and N to step through matches forward and backward
  • Neither * nor g* moves the cursor initially to the next match if the cursor is already on a match — press n to advance
  • Combine g* with cgn for a powerful partial-match rename workflow: g* to search, then cgnreplacement<Esc> and . to selectively replace
  • Both commands respect the ignorecase and smartcase settings for case sensitivity

Next

How do I edit multiple lines at once using multiple cursors in Vim?