Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim searching through all existing buffers

Tags:

vim

When dealing with a single file, I'm used to:

/blah do some work n do some work n do some work 

Suppose now I want to search for some pattern over all buffers loaded in Vim, do some work on them, and move on. What commands do I use for this work flow?

like image 325
anon Avatar asked Mar 15 '10 22:03

anon


People also ask

How do I navigate between buffers in Vim?

script, and place the following in your vimrc. Pressing Alt-F12 opens a window listing the buffers, and you can press Enter on a buffer name to go to that buffer. Or, press F12 (next) or Shift-F12 (previous) to cycle through the buffers.

How do I close all buffers in Vim?

Just put it to your . vim/plugin directory and then use :BufOnly command to close all buffers but the active one.

How do I save and quit all tabs in Vim?

:wa - save all tabs / unsaved buffers. :xa / :wqa - save all tabs / unsaved buffers and exit Vim.


2 Answers

Use the bufdo command.

:bufdo command 

:bufdo command is roughly equivalent to iterating over each buffer and executing command. For example, let's say you want to do a find and replace throughout all buffers:

:bufdo! %s/FIND/REPLACE/g 

Or let's say we want to delete all lines of text that match the regex "SQL" from all buffers:

:bufdo! g/SQL/del 

Or maybe we want to set the file encoding to UTF-8 on all the buffers:

:bufdo! set fenc=utf-8 

The above can be extrapolated for Windows (:windo), Tabs (:tabdo), and arguments (:argdo). See help on :bufdo for more information.

like image 77
Kaleb Pederson Avatar answered Sep 26 '22 07:09

Kaleb Pederson


We can do this using vimgrep and searching across the argslist. But first let's populate our argslist with all our buffers:

:bufdo :args ## % 

Now we can search in our argslist

:vimgrep /blah/ ## 

Where % == the current filepath and ## == the arglist.

I recommend watching these vimcasts if you want to learn more: Populate the arglist, Search multiple files with vimgrep

like image 27
Tarrasch Avatar answered Sep 22 '22 07:09

Tarrasch