Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display all results of a search in vim

Tags:

vim

When searching string with notepad++, new window opens and shows find results. I want to use this feature in vim. After googling I found out some suggestions:

vimgrep /<pattern>/ %
copen

It is possible to create mapping which do those two commands. Pattern should be the current word: may be cword keyword in vim?

like image 487
Vardan Hovhannisyan Avatar asked Feb 16 '23 21:02

Vardan Hovhannisyan


2 Answers

The requirement is actually easy. but to get user inputted pattern, you need a function.

function! FindAll()
    call inputsave()
    let p = input('Enter pattern:')
    call inputrestore()
    execute 'vimgrep "'.p.'" % |copen'
endfunction

if you want to have a mapping, add this line:

nnoremap <F8> :call FindAll()<cr>

but as I commented under your question. % may not work for unamed buffer.

like image 148
Kent Avatar answered Feb 27 '23 15:02

Kent


I suggest lvimgrep (so you can use quickfix for :make)

:nnoremap <F6> :lvimgrep /\M\<<C-R><C-W>\m\>/ **/*.[ch]pp **/Makefile | lopen<CR>

Also, if you just wanted to find in the current file:

:g/<pattern>/

will invoke 'print' (default command) on each matching line.

:v//               " non-matching lines
:g//-1             " lines preceding the matching line
:g//-1,+1          " lines around the matching line

etc.

:global is far more useful:

 :g/foo/ join       " join all lines containing foo

etc.

like image 29
sehe Avatar answered Feb 27 '23 14:02

sehe