# 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:

vim
/the

If this works, your original pattern might have a typo.

Hidden Characters

The text might contain characters you can't see:

vim
:set list

This shows special characters like tabs (^I) and trailing spaces ($).

Case Sensitivity Issues

Vim's default case sensitivity depends on your settings:

vim
:set ignorecase? smartcase?
  • ignorecase on: Search is case-insensitive
  • smartcase on: If pattern contains uppercase, search is case-sensitive

Recommended settings:

vim
set ignorecase
set smartcase

With these, /test matches Test, TEST, and test, but /Test only matches Test.

Force Case Sensitivity

Add \C anywhere in pattern for case-sensitive:

vim
/Test\C

Add \c for case-insensitive:

vim
/test\c

Pattern Contains Special Characters

Many characters have special meaning in Vim regex:

CharacterMeaning
.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:

vim
/search\.term
/\$100
/C:\\Users

Or use \V for "very nomagic" mode:

vim
/\V$100

This treats everything literally except ``.

Regex Patterns Not Working

Vim uses different regex syntax than Perl or JavaScript:

Vim vs Perl Regex

FeatureVimPerl
Group\(...\)(...)
Or`\`\
One or more\++
Zero or one\=?
Non-greedy\{-}*?

Use "Very Magic" Mode

Add \v for Perl-like regex:

vim
/\v(pattern)+\w{3,5}

This makes ()+{}| work without escaping.

Common Regex Issues

Matching word boundaries:

vim
/\<word\>    " Matches whole word 'word'

Matching start/end of line:

vim
/^start      " Matches 'start' at beginning of line
/end$        " Matches 'end' at end of line

Matching character classes:

vim
/[a-z]       " Matches any lowercase letter
/[^0-9]      " Matches any non-digit
/[A-Za-z_]   " Matches letters and underscore

Search Highlighting Issues

No Highlighting

vim
:set hlsearch

This enables search highlighting. To toggle:

vim
:set hlsearch!

Highlighting Won't Turn Off

Press :noh or :nohlsearch to clear highlighting without disabling it:

vim
:noh

Map a key to clear highlighting:

vim
nnoremap <Esc><Esc> :noh<CR>

Double-press Escape to clear highlighting.

Highlighting Too Bright/Distracting

Change the highlight color:

vim
hi Search guibg=LightBlue guifg=Black
" Or for terminal
hi Search ctermbg=LightBlue ctermfg=Black

For incsearch (highlighting while typing):

vim
set incsearch
hi IncSearch guibg=Yellow

Search Range Issues

Search only finds matches in visible range with certain settings. To search the entire file:

vim
" This is the default, but ensure it's set
set wrapscan

With wrapscan, search wraps around the file.

File Type Specific Issues

Some file types have syntax-specific search settings:

vim
" Check if syntax-specific settings override your preferences
:verbose set ignorecase?

Ignored Files

Vim might be configured to ignore certain patterns:

vim
:set wildignore?

This doesn't affect search directly, but check for any custom settings.

Search History and Registers

Access previous searches:

vim
" Browse search history
q/
" Or use arrow keys after pressing /
/<Up>

Incremental Search Not Working

vim
:set incsearch

This shows matches as you type the pattern.

To search across lines, use \n:

vim
/line1\nline2

Or search for any character including newline:

vim
/pattern\_s*pattern

\_s matches any whitespace including newlines.

If Vim's search is slow on large files:

vim
:grep pattern
:vimgrep pattern **/*.py

Results go to the quickfix list:

vim
:copen    " Open quickfix window
:cn       " Next match
:cp       " Previous match

Substitute Issues

Substitute uses the same patterns as search:

vim
:%s/old/new/g

Common Substitute Problems

Pattern not found but you know it exists:

Check for hidden characters:

vim
:set list
%s/pattern/replacement/g

Substitute asks for confirmation every time:

Remove the c flag:

vim
:%s/old/new/g    " Without 'c' flag

Only substitutes first occurrence per line:

Add the g flag:

vim
:%s/old/new/g    " 'g' for global (all on line)

Search in Current Visual Selection

Select text, then search within it:

vim
" Visual mode selection, then:
:'<,'>s/old/new/g

Or use \%V atom:

vim
/\%Vpattern    " Only match within visual selection

Debug Search Patterns

Test Patterns Incrementally

Build your pattern step by step:

vim
/test        " First, find 'test'
/test\.\+    " Then add complexity
/\vtest\.\+  " Try with very magic

Show All Matches

vim
:g/pattern

This prints all lines matching the pattern.

Count Matches

vim
:%s/pattern//gn

The 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.