# Vim Search Not Working
You type /pattern and expect matches but Vim says "Pattern not found" or highlights the wrong things. Vim's search is powerful but has nuances that can trip you up. Here's how to fix search issues.
Basic Search Not Finding Anything
Check for Typos
The simplest cause. Try searching for something you know exists:
/theIf this works, your original pattern might have a typo.
Hidden Characters
The text might contain characters you can't see:
:set listThis shows special characters like tabs (^I) and trailing spaces ($).
Case Sensitivity Issues
Vim's default case sensitivity depends on your settings:
:set ignorecase? smartcase?ignorecaseon: Search is case-insensitivesmartcaseon: If pattern contains uppercase, search is case-sensitive
Recommended settings:
set ignorecase
set smartcaseWith these, /test matches Test, TEST, and test, but /Test only matches Test.
Force Case Sensitivity
Add \C anywhere in pattern for case-sensitive:
/Test\CAdd \c for case-insensitive:
/test\cPattern Contains Special Characters
Many characters have special meaning in Vim regex:
| Character | Meaning |
|---|---|
. | Any character |
* | Zero or more of previous |
+ | One or more of previous |
? | Zero or one of previous |
[ | Start character class |
] | End character class |
( | Start group |
) | End group |
{ | Start quantifier |
} | End quantifier |
| Escape character |
^ | Start of line |
$ | End of line |
To search for literal special characters, escape them:
/search\.term
/\$100
/C:\\UsersOr use \V for "very nomagic" mode:
/\V$100This treats everything literally except ``.
Regex Patterns Not Working
Vim uses different regex syntax than Perl or JavaScript:
Vim vs Perl Regex
| Feature | Vim | Perl | ||
|---|---|---|---|---|
| Group | \(...\) | (...) | ||
| Or | `\ | | `\ | |
| One or more | \+ | + | ||
| Zero or one | \= | ? | ||
| Non-greedy | \{-} | *? |
Use "Very Magic" Mode
Add \v for Perl-like regex:
/\v(pattern)+\w{3,5}This makes ()+{}| work without escaping.
Common Regex Issues
Matching word boundaries:
/\<word\> " Matches whole word 'word'Matching start/end of line:
/^start " Matches 'start' at beginning of line
/end$ " Matches 'end' at end of lineMatching character classes:
/[a-z] " Matches any lowercase letter
/[^0-9] " Matches any non-digit
/[A-Za-z_] " Matches letters and underscoreSearch Highlighting Issues
No Highlighting
:set hlsearchThis enables search highlighting. To toggle:
:set hlsearch!Highlighting Won't Turn Off
Press :noh or :nohlsearch to clear highlighting without disabling it:
:nohMap a key to clear highlighting:
nnoremap <Esc><Esc> :noh<CR>Double-press Escape to clear highlighting.
Highlighting Too Bright/Distracting
Change the highlight color:
hi Search guibg=LightBlue guifg=Black
" Or for terminal
hi Search ctermbg=LightBlue ctermfg=BlackFor incsearch (highlighting while typing):
set incsearch
hi IncSearch guibg=YellowSearch Range Issues
Search only finds matches in visible range with certain settings. To search the entire file:
" This is the default, but ensure it's set
set wrapscanWith wrapscan, search wraps around the file.
File Type Specific Issues
Some file types have syntax-specific search settings:
" Check if syntax-specific settings override your preferences
:verbose set ignorecase?Ignored Files
Vim might be configured to ignore certain patterns:
:set wildignore?This doesn't affect search directly, but check for any custom settings.
Search History and Registers
Access previous searches:
" Browse search history
q/
" Or use arrow keys after pressing /
/<Up>Incremental Search Not Working
:set incsearchThis shows matches as you type the pattern.
Multi-line Search
To search across lines, use \n:
/line1\nline2Or search for any character including newline:
/pattern\_s*pattern\_s matches any whitespace including newlines.
Using grep for External Search
If Vim's search is slow on large files:
:grep pattern
:vimgrep pattern **/*.pyResults go to the quickfix list:
:copen " Open quickfix window
:cn " Next match
:cp " Previous matchSubstitute Issues
Substitute uses the same patterns as search:
:%s/old/new/gCommon Substitute Problems
Pattern not found but you know it exists:
Check for hidden characters:
:set list
%s/pattern/replacement/gSubstitute asks for confirmation every time:
Remove the c flag:
:%s/old/new/g " Without 'c' flagOnly substitutes first occurrence per line:
Add the g flag:
:%s/old/new/g " 'g' for global (all on line)Search in Current Visual Selection
Select text, then search within it:
" Visual mode selection, then:
:'<,'>s/old/new/gOr use \%V atom:
/\%Vpattern " Only match within visual selectionDebug Search Patterns
Test Patterns Incrementally
Build your pattern step by step:
/test " First, find 'test'
/test\.\+ " Then add complexity
/\vtest\.\+ " Try with very magicShow All Matches
:g/patternThis prints all lines matching the pattern.
Count Matches
:%s/pattern//gnThe n flag suppresses substitution, and g counts all occurrences.
Quick Reference
```vim " Case insensitive search set ignorecase set smartcase
" Enable highlighting set hlsearch set incsearch
" Clear highlighting :noh
" Very magic regex (Perl-like) /\vpattern
" Very nomagic (literal) /\Vpattern
" Word boundaries /\<word\>
" Clear search pattern :let @/ = "" ```
With these techniques, you can handle any search situation in Vim.