tags:

views:

98

answers:

4

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?

+3  A: 

Use the bufdo command.

:bufdo command

And you can do similarly for tabs:

:tabdo command
Kaleb Pederson
how do you use it?
Nathan Fellman
A: 

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!

AHM
A: 

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.

too much php
A: 

I have the following mappings (inspired from 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 parameter when you stared 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 so 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 see what's happening.

... and if you have a bunch of buffers open, you can load up the files you want to work with in, 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! : )

RymdPung