How can you list the matches of Vim's search?
SearchVimSearch Problem Overview
I would like to list the matches, when I hit:
/example
so that I see where all matches are at once.
Search Solutions
Solution 1 - Search
:g//p
In its longer form:
:global/regular-expression/print
You can leave out the pattern/regex and Vim will re-use the previous search term.
Trivia: The grep tool was named after this command sequence.
Solution 2 - Search
You can also do a :
g/pattern/#
that will print the pattern you want and the number of the line.
Solution 3 - Search
if you want to look at this list and jump quickly between the matches, consider using
:vimgrep example %
or
:grep example %
This will populate the "error list" with all of the matches so that you can use :copen
to list them all in the quickfix buffer, press enter on a particular line to jump to that match, or use commands like :cn
and :cp
to go back and forth.
for a thorough explanation, see my reply to a similar question
Solution 4 - Search
Just learned a new one: the Location List
!
Type :lvim foo %
to search for foo
in the current file and enter all matches containing foo
into the location list.
Type :lopen
to open the location list in the quickfix window, which is fully navigable as usual.
Use :lnext
/:lprevious
to to through the list (use tpope/unimpaired mappings for the best experience)
Solution 5 - Search
Another possibility is to use the include file search commands.
[I
This will list all occurrences of the word under the cursor. It may be more than you need though, because it will also search any files that are included in the current file.
But the nice thing about this command is that the search result display also shows a count of the number of matches, in addition to the line number of each match.
:help include-search
to see lots of variants.
A note about
:g//p
This can be reduced further to
:g//
because, as others have said, p(rint) is the default action.
Solution 6 - Search
you can get a nice quickfix
window with the matches form your current search pattern
:vim // %
:copen
super handy if you previously crafted a complex search pattern using just /pattern
Edit: just found out this also works for all open buffers
:bufdo vimgrepadd // %
:copen
Solution 7 - Search
Using :set hlsearch
will highlight all the matches in yellow allowing you to scan the file easily for matches. That may not be what you want though, after searching, :g//p will give you the listed matches
Solution 8 - Search
To elaborate on this ... instead of
/example
:g//p
you can also write directly
:g/example/p
or, as p(rint) is the default action for the :g(lobal) command, this can be shortened to
:g/example
And instead of p(rint), other actions are possible, e.g. d(elete). See :help :global
Solution 9 - Search
g/pattern
If you have :set number
, the above command displays line numbers as well.
If you haven't :set number
, then
g/pattern/#
will display the line numbers.
Solution 10 - Search
" put in your ~/.vimrc file
" START search related configs and helps
"
" ignore case when searching
set ignorecase
" search as characters are entered, as you type in more characters, the search is refined
set incsearch
" highlight matches, in normal mode try typing * or even g* when cursor on string
set hlsearch
" yank those cheat commands, in normal mode type q: than p to paste in the opened cmdline
" how-to search for a string recursively
" :grep! "\<doLogErrorMsg\>" . -r
"
" how-to search recursively , omit log and git files
" :vimgrep /srch/ `find . -type f \| grep -v .git \| grep -v .log`
" :vimgrep /srch/ `find . -type f -name '*.pm' -o -name '*.pl'`
"
" how-to search for the "srch" from the current dir recursively in the shell
" vim -c ':vimgrep /srch/ `find . -type f \| grep -v .git \| grep -v .log`'
"
" how-to highlight the after the search the searchable string
" in normmal mode press g* when the cursor is on a matched string
" how-to jump between the search matches - open the quick fix window by
" :copen 22
" how-to to close the quick fix window
" :ccl
" F5 will find the next occurrence after vimgrep
map <F5> :cp!<CR>
" F6 will find the previous occurrence after vimgrep
map <F6> :cn!<CR>
" F8 search for word under the cursor recursively , :copen , to close -> :ccl
nnoremap <F8> :grep! "\<<cword>\>" . -r<CR>:copen 33<CR>
" omit a dir from all searches to perform globally
set wildignore+=**/node_modules/**
" use perl regexes - src: http://andrewradev.com/2011/05/08/vim-regexes/
noremap / /\v
"
" STOP search related configs and helps
Solution 11 - Search
I have written a piece of code for this. It actually avoids the problems in vimgrep
. It works even with unnamed files. And it is easier to use.
function! Matches(pat)
let buffer=bufnr("") "current buffer number
let b:lines=[]
execute ":%g/" . a:pat . "/let b:lines+=[{'bufnr':" . 'buffer' . ", 'lnum':" . "line('.')" . ", 'text': escape(getline('.'),'\"')}]"
call setloclist(0, [], ' ', {'items': b:lines})
lopen
endfunction
When you call it with a pattern, it opens the location windows with all the matches.
This could be a command
command! -nargs=1 Mat call Matches(<f-args>)
So all you need to do is to type :Mat pattern
I also use the following mapping to get the matches of the current visual selection.
vnoremap Y "xy:call Matches(@x)<CR>
Solution 12 - Search
Ctrl-f to list all search result:
nmap <C-f> :vimgrep /<C-r>//g %<CR> \| !:copen <Enter>