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 asg*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
*andg*set the search register, so you can follow up with:%s//replacement/gto substitute all matches without retyping the pattern - After pressing
*org*, usenandNto step through matches forward and backward - Neither
*norg*moves the cursor initially to the next match if the cursor is already on a match — pressnto advance - Combine
g*withcgnfor a powerful partial-match rename workflow:g*to search, thencgnreplacement<Esc>and.to selectively replace - Both commands respect the
ignorecaseandsmartcasesettings for case sensitivity