Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have started learning Vim word-search using * and # while the cursor is over the current word. But this search is limited to the current file buffer.

Is there a command or a shortcut to extend this search to:

  1. all opened tabs?
  2. all opened buffers?
share|improve this question

3 Answers 3

up vote 3 down vote accepted

I don't have an exact solution for your problem, hopefully a better answer than mine will come up. But this is how I tackled the problem of finding a word in all buffers.

" enables to search in all open buffers with :Search <pattern>
command! -nargs=1 Search call setqflist([]) | silent bufdo grepadd! <args> %

nnoremap <left>  :cprev<cr>zvzz
nnoremap <right> :cnext<cr>zvzz

The first line creates a command Search with the search pattern as argument, which writes the results in a quickfix list. The two other lines map the (at least for me) useless arrow keys to something useful; they are mapped to jump to the next/previous Search or to the next/previous compile error, etc., they simply step throu the quickfix list. You can use this as follows:

:Search foobar
<right>
<right>
…
share|improve this answer
    
I love this command, but added a few things so it will escape search terms better, and force a redraw. (using silent with splits can cause vim ui to bug out). command! -nargs=1 Search call setqflist([]) | silent execute "bufdo grepadd! '<args>' %" | redraw! –  Ross Charette Jul 21 at 4:30

It's actually the default behaviour although it may be hard to notice: try * then change to another tab and use n ans N in command mode to jump forward and backward between search hits.

This may make more sense if first you turn highlighting on for all hits:

:set hlsearch
share|improve this answer
    
+1 only because of hlsearch that I didn't know, and which I would have search for one day or another :-). However, by default I have tried * #, n and N, and it doesn't jump to other file buffers... –  Stephane Rolland Jan 3 '13 at 15:10
    
No, n and N do not jump buffers (they wrap around), but the term they target is searched for in all tabs; hit * with the highlighting on then cycle thru your tabs -- they will all be highlighted with the same term, so you can use n and N locally there without a fresh search. –  goldilocks Jan 3 '13 at 15:28
    
The whole point is NOT to have to cycle through your tabs to find all the matches. –  Magnus Jun 26 at 18:16
    
@Magnus While that may be preferable, it's actually not explicitly stated in the question, which asks how to "extend this search to...all buffers" -> it is extended across all buffers. The point of my answer was to make this clear, since it might not be, especially if you do not have hlsearch set. –  goldilocks Jun 26 at 18:33
 :bufdo vimgrepadd yoursearchterm % | copen
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.