Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #70 - Running a command on all buffers

Created: May 30, 2001 12:09 Complexity: basic Author: Scott Johnston Version: 5.7 Karma: 59/21 Imported from: Tip#70

From Peter Bismuti on the vim list:


How to global search and replace in all buffers with one command?

You need the AllBuffers command:


call AllBuffers("%s/string1/string2/g")


"put this in a file and source it

function AllBuffers(cmnd)

let cmnd = a:cmnd 
let i = 1 
while (i <= bufnr("$")) 
if bufexists(i) 
execute "buffer" i 
execute cmnd 
endif 
let i = i+1 
endwhile 

endfun


":call AllBuffers("%s/foo/bar/ge

Comments

I like this one better as you know exactly what is getting changed and it doesn't require writing any buffers, it just modifies them.

" execute a command for all buffers ther are shown in windows fun! AllWindows(cmnd)

let cmnd = a:cmnd 
let origw = winnr() 
let i = 1 
while (i <= bufnr("$")) 
if bufexists(i) 
let w = bufwinnr(i) 
if w != -1 
echo "=== window: " . w . " file: " . bufname(i) 
execute "normal \<c-w>" . w . "w" 
execute cmnd 
endif 
endif 
let i = i+1 
endwhile 
execute "normal \<c-w>" . origw . "w" 

endfun


amackay--AT--s.gusnet.cx , June 4, 2001 19:07



From Janakiraman .S: 


Addiing those quotes is pretty boring. That is easy to fix. Just make a command like command! -nargs=+ -complete=command AllBuf call AllBuffers(<q-args>)

You could then replace across multiple files with

AllBuf %s/foo/bar/ge


peoter_veliki--AT--hotmail.com , June 4, 2001 22:43


What about the |argdo| and |bufdo| and |windo| commands?

Gergely Kontra

kgergely--AT--mcl.hu , January 9, 2002 6:33


bufdo works for me. Perhaps it's a new feature.

vincent.virgilio--AT--itt.com , September 13, 2002 13:47


bufdo disables syntax highlighting when moving between the buffers and hence is very fast.


akhil_thakur--AT--indiatimes.com , February 24, 2004 21:00


Advertisement