Vim Search & Quickfix
Search commands, quickfix, and location list.
Basic Search
Forward search
/pattern " Search forward
Backward search
?pattern " Search backward
Navigate results
n " Next match (same direction)
N " Previous match (opposite direction)
Word under cursor
* " Search word under cursor (forward)
# " Search word under cursor (backward)
g* " Partial word match (forward)
g# " Partial word match (backward)
Search Options
Case sensitivity
/pattern\c " Case insensitive
/pattern\C " Case sensitive
:set ignorecase " Always ignore case
:set smartcase " Ignore case unless uppercase used
Highlight
:set hlsearch " Highlight matches
:nohlsearch " Clear highlight (temporary)
:noh " Short form
Incremental
:set incsearch " Show matches as you type
Search History
Navigate history
/<Up> " Previous search
/<Down> " Next search
q/ " Open search history window
Reuse last pattern
// " Repeat last search
?? " Repeat last backward search
Anchors
Line anchors
/^pattern " Start of line
/pattern$ " End of line
/^pattern$ " Exact line
Word boundaries
/\<word\> " Exact word
/\<word " Word start
/word\> " Word end
Character Classes
Predefined classes
/\d " Digit [0-9]
/\D " Non-digit
/\w " Word char [a-zA-Z0-9_]
/\W " Non-word char
/\s " Whitespace
/\S " Non-whitespace
/\a " Alphabetic [a-zA-Z]
/\l " Lowercase [a-z]
/\u " Uppercase [A-Z]
/\x " Hex digit [0-9A-Fa-f]
Custom classes
/[aeiou] " Any vowel
/[^aeiou] " Not a vowel
/[0-9] " Any digit
/[a-zA-Z] " Any letter
Quantifiers
Repetition
/x* " Zero or more x
/x\+ " One or more x
/x\? " Zero or one x
/x\{3} " Exactly 3 x
/x\{3,5} " 3 to 5 x
/x\{3,} " 3 or more x
/x\{,5} " Up to 5 x
Non-greedy
/x\{-} " Non-greedy (as few as possible)
/.\{-}pattern " Match shortest to pattern
Groups and Alternation
Groups
/\(pattern\) " Group (for backreference)
/\%(pattern\) " Non-capturing group
Alternation
/cat\|dog " cat or dog
/\(cat\|dog\)s " cats or dogs
Backreferences
/\(word\).*\1 " word appears twice
/\(\w\+\)\s\+\1 " Repeated word
Very Magic Mode
Magic modes
/\v " Very magic (regex-like)
/\V " Very nomagic (literal)
/\m " Magic (default)
/\M " Nomagic
Very magic example
/\v(cat|dog)s? " No escaping needed
/\(cat\|dog\)s\? " Normal magic (escaped)
Special Patterns
Any character
/. " Any single character
/\_s " Whitespace including newline
/\_. " Any character including newline
Line/column
/\%3l " Match on line 3
/\%5c " Match at column 5
/\%>10l\%<20l " Lines 11-19
Visual area
/\%V " Inside visual selection
/\%Vpattern\%V " Pattern in visual area
Practical Patterns
IP address
/\d\{1,3}\.\d\{1,3}\.\d\{1,3}\.\d\{1,3}
Email
/\w\+@\w\+\.\w\+
URL
/https\?:\/\/\S\+
Trailing whitespace
/\s\+$
Empty lines
/^$
Lines with only whitespace
/^\s*$
Duplicate words
/\v<(\w+)\s+\1>
Search and Do
Execute on matches
:g/pattern/command " Run command on matching lines
:v/pattern/command " Run on non-matching lines
Count matches
:%s/pattern//gn " Count without replacing
List matches
:g/pattern/p " Print matching lines
:g/pattern/# " Print with line numbers
Search Offset
Position after search
/pattern/+2 " 2 lines below match
/pattern/-1 " 1 line above match
/pattern/e " End of match
/pattern/b+3 " 3 chars from start of match
Quickfix Basics
Open quickfix window
:copen " Open quickfix window
:cclose " Close quickfix window
:cwindow " Open if errors, close if none
Navigate quickfix
:cnext " Next error
:cprev " Previous error
:cfirst " First error
:clast " Last error
:cc 5 " Go to error 5
Quickfix shortcuts
]q " Next quickfix (with vim-unimpaired)
[q " Previous quickfix
Grep to Quickfix
Internal grep
:vimgrep /pattern/ **/*.adoc " Search all adoc files
:vimgrep /pattern/g % " Search current file (all matches)
:vimgrep /pattern/j **/*.adoc " Don't jump to first match
External grep
:grep pattern **/*.adoc " Use external grep
:grep -r pattern . " Recursive grep
Set grepprg
:set grepprg=rg\ --vimgrep " Use ripgrep
:set grepprg=ag\ --vimgrep " Use silver searcher
Build Errors
Make
:make " Run make, populate quickfix
:make test " Run make test
Compiler
:compiler python " Set compiler for python
:compiler gcc " Set compiler for gcc
Location List
Location list (per-window)
:lopen " Open location list
:lclose " Close location list
:lnext " Next location
:lprev " Previous location
:lfirst " First location
:llast " Last location
Grep to location list
:lvimgrep /pattern/ **/*.adoc " Populate location list
:lgrep pattern **/*.adoc " External grep to location list
Quickfix History
Navigate history
:colder " Older quickfix list
:cnewer " Newer quickfix list
:chi " Show quickfix history
Filter Quickfix
Filter entries
:Cfilter /pattern/ " Keep matching (needs plugin or custom)
:packadd cfilter " Load cfilter plugin (Vim 8.1+)
:Cfilter /error/ " Keep only errors
:Cfilter! /warning/ " Remove warnings
Quickfix Commands
Run on each entry
:cdo s/old/new/g " Substitute in each file
:cdo update " Save each file
:cfdo %s/old/new/g | update " Per-file substitute and save
Location list equivalent
:ldo s/old/new/g " Substitute in each location
:lfdo %s/old/new/g | update " Per-file
Populate Quickfix
From buffer
:cbuffer " Read errors from current buffer
:cgetbuffer " Same, don't jump
:caddexpr "file.txt:10:error message" " Add single entry
From file
:cfile errors.txt " Read errors from file
:cgetfile errors.txt " Same, don't jump
From expression
:cexpr system('grep -rn pattern .') " From command output
Integration Patterns
Search project and open results
:grep -r TODO . | copen
Find and replace across files
:vimgrep /oldpattern/ **/*.py
:cfdo %s/oldpattern/newpattern/gc | update
Error workflow
:make
:copen
:cnext " Fix each error
Custom Quickfix Mappings
Suggested mappings
nnoremap <leader>co :copen<CR>
nnoremap <leader>cc :cclose<CR>
nnoremap ]q :cnext<CR>
nnoremap [q :cprev<CR>
nnoremap ]Q :clast<CR>
nnoremap [Q :cfirst<CR>