Vim Tips Wiki
Advertisement

Notes to form draft of new tip

Often, you may need to run a command on several buffers that you are editing. However, if you do not need or want to apply the command on ALL the open buffers, it is not as simple as running a :bufdo command.

The quickest way to accomplish this, if you already know all the buffers you need, and all the buffers needed correspond to files on disk, would be to use the argument list. The argument list can be local to the current window, so doing a :split before you modify it will ensure that you don't mess up anything else you are currently working on. Doing :tabnew instead of :split will even prevent messing up your window layout; all you need to do when you are done is to close the tab.

:sp (or :tabnew)
:arglocal (list of files to apply the command to)
:argdo (command to apply to all buffers needed)

If you realize that you don't have all the required files in the file list, or can't readily list all of them out, you can use the :argadd command to aid you.

Unfortunately, this method will not work if the buffers you need to apply the command to are not yet saved, and it requires an extra step (like the :sall command) to view the modified buffers to make sure your change was executed correctly. Additionally, it is faster to open a new window for a buffer than it is to add it to the arg list. So, another method uses a tab page as a workspace, within which you will collect a window for each buffer you want to run the command on, and then simply use :windo.

:tabnew
(open a new window on each buffer you want to apply the command to)
:windo {command}

This can be an especially nice method if you can capture each desired buffer in the quickfix list, using :grep or :make, because the CTRL-W_<Enter> command will automatically open the quickfix item in a new window.

Possible expansion:

  • command to open a new tab page to use for this purpose, plus command to add current buffer to this new tab page (use tab-specific variable to uniquely identify the tab in case it gets moved)
" Collect the current buffer or listed buffers. 
command! -bar -nargs=* -complete=buffer CollectBuffer

" Start a buffer collection with current or listed buffers. Closes any active
" connection first, but doesn't close the tab, so can resume the old
" collection again later with BeBufCollection.
command! -bar -nargs=* -complete=buffer NewBufCollection

" Turn the current or specified tab into a buffer collection. Closes any
" active collection first (but doesn't close the tab, so you can resume again
" with this command in the old collection tab)
command! -bar -nargs=? BeBufCollection

" "Close" the collection by removing the tab-specific variable that marks it as
" a collection. Using ! will also close the tab containing the collection.
command! -bar -nargs=0 -bang CloseBufCollection

" run a command on the collection (pretty much just finds the tab and does a
" windo). Grab any :bar as part of the input (so lack of -bar is intentional)
command! -nargs=+ BufCollectionDo
  • mention how to open each file in the quickfix list, with splitbuf=useopen,split and :cnext<Enter>999999999@: ?
  • mention moving windows between tabs tip

Comments

Advertisement