46

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?

8 Answers 8

58

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.

4
  • 1
    I've added some examples and clarifications that I hope are useful. Let me know if you still think it's missing something. Oct 1, 2014 at 18:43
  • 15
    The question was specifically how to / search across all buffers. This answer explains how to issue commands across all buffers, but / is not a command. Would this work with /?
    – Novaterata
    Aug 11, 2017 at 21:11
  • 1
    I really wanted this to work, but when I tried ":bufdo /TODO.*batch", it spit out error after error about files that had no match. That's not what I want: I wanted it to find the next one of the files I had opened with vim... that had the regex "TODO.*batch" in it.
    – jorgbrown
    Oct 9, 2018 at 21:48
  • 1
    @Novaterata For those of y'all looking specifically for searching functionality, see the second answer Oct 29, 2022 at 18:45
49

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

6
  • 4
    Thank you! This is exactly what I've been looking for, for ages. (Was just missing the ## vimgrep argument.) I suppose we can also add arguments to the list using :arga[dd], as an alternative. Jan 28, 2016 at 19:19
  • 2
    In vim ## is the current arglist and % is the current file name. We first clear out the current arglist with :%argd, then use :bufdo to add the buffers to the arglist one at a time. Finally, we use :vimgrep with ## to search the arglist.
    – JPaget
    Sep 1, 2016 at 20:43
  • 1
    NB: There's no need to perform :bufdo :args ## % if you haven't opened any additional files (e.g. by using the :e command) since you started Vim. In this case your buffers should already match your argslist. Jul 15, 2019 at 13:12
  • 1
    Excellent. Now, a few questions. If I get a hit, it is restricting my view to the function block. How do I switch to the normal buffer? How do I go to the next hit? Any way to see all the hits in one list?
    – John
    May 27, 2020 at 23:08
  • 2
    Vimgrep loads all matches into the quicklist. :copen should show you all the matching lines in the quicklist. Jul 7, 2021 at 4:51
13

I have the following mappings (inspired by Vimperator) that make switching previous/next buffer easier.

nmap <C-P> :bp<CR>
nmap <C-N> :bn<CR>

This works really well with 'n'. When you're done working with your file, just hit CTRL-n before hitting n again and you're searching in the next buffer. Redo until you're through all buffers.


Another way of working with many files is the argument list.

It contains any files passed as parameters when you started vim (e.g: vim someFile.txt someOtherFile.py). The file within [brackets] is the current file.

:args
[someFile.txt] someOtherFile.py

:n will bring you to the next file in the list and :N will bring you back. You can also add to the argslist with :argadd, or create a new args list with

:n some.py files.py you.py want.py to.py work.py with.py
or to open all *.py files recursively from some project.
:n ~/MyProjects/TimeMachine/**/*.py

The args list work well with macros too (see :help q), if you have similar changes to your files. Just record your macro on the first file, finish with :n to move to the next file, and stop recording.

qq/searchForSomethingAndDoStuffOrWhatever:nq

Then run your macro through all files (6@q), have a look to make sure everything went well, and finish with a :wall.


It kinda depends on what you want to do. If you just have one change that is exactly the same across many files (and those are the only ones you have loaded), I also like the :ba (:tabdo sp) command. It's very quick and you can see what's happening.

And if you have a bunch of buffers open, you can load up the files you want to work within, each in a window, and do a regexp on all of them.

CTRL-w v :b someFile
:sp anotherFile
...
:windo :%s/foo/bar/g

I really recommend FuzzyFinder, it makes your life a lot easier when opening files.

http://www.vim.org/scripts/script.php?script_id=1984

MMmMmmmm VIM IS NICE! SO SEXY! : )

1
  • In addition to these mappings, I find it most helpful to set autowrite so the buffers are written automatically when I change to a new buffer. Dec 24, 2019 at 19:19
4

Anytime you want to switch to another buffer try this.

:b + <any part of file in buffer> + tab

For an example. I have this in my buffer

77 "/var/www/html/TopMenuAlertAlert.vue" line 65
78 "/var/www/html/MainSidebar.vue" line 29
79 "/var/www/html/FullScreenSearch.vue" line 26
80 "/var/www/html/Menu.vue" line 93
81 "/var/www/html/layouts/RightSidebar.vue" line 195

As I want to change to another buffer, I probably remember some detail about the file like 'Alert'

So I just go

:b Alert + tab

if the file given is not the one I want, I just keep on pressing tab.

Vim will keep on giving the next file close to it.

Once you got it. Press Enter.

2
  • That looks like a search of the filename, not the content of the file, which the OP asked about. Jul 12, 2022 at 17:27
  • I just figured this out myself the other day, and it simplifies working with buffers considerably. However, I agree with @AlexisWilke that this answer is tagential to the OP's query. Jan 9 at 14:54
2

Here is your gospel: https://github.com/jeetsukumaran/vim-buffersaurus

This lovely plugin shows you all the files that match your query in a separate window, from which you can choose. It support regex.

1

I don't believe it's possible to extend the 'n' functionanly across files or buffers. You could use

:grep blah *

And then do

:cn

To move to the next line with blah on it. That will switch between files but it's not quite as neat. It's more cumbersome to type the colon all the time, and it will only take you to the line, not the word.

What I usually do is either to open the files I want to searched in tabs and then use 'n' and 'gt' to jump to next tab when I reach the end of the file, or list the files on the command line to I can skip to the next file with ':wn' when I'm done editing it.

Hope it helps!

1

Another approach:

:call setqflist([])           " clear quickfix list
:silent bufdo grepadd! foo %  " edit foo in command-line history window
:cw                           " view search results

Or mapped:

cmap bbb call setqflist([]) \| silent bufdo grepadd!  %<C-F>$hha
0

I would open all the buffers in a new tab using the following two commands:

:tab sp
:bufdo sp

Then search through each file one by one and close its window when you are done (use :q or :close). Use CTRL+W_ to maximize each window as you are working in it. When you're finished and you close the last window, the tab page will close and you'll be dropped back wherever you were before you decided to do the search.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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