Vim Tips Wiki

Execute command on each file in a list

Redirected from VimTip1230

1,624pages on
this wiki
Add New Page
Talk0 Share
Tip 1230 Printable Monobook Previous Next

created 2006 · complexity basic · author Eric Arnold · version n/a

I wanted to do

:new file*.[ch]

and have each file show up in a new window, without having to mess up the args list or whatever. I came up with the following script.

command! -nargs=* -complete=file New call Foreach( "new %% ", <f-args> )
command! -nargs=* -complete=file Vnew call Foreach( "vnew %% ", <f-args> )
command! -nargs=* -complete=file Diff call Foreach( "vnew %% | diffthis", <f-args> )
" %% symbols are replaced with file/arg names
function! Foreach( ... )
  let cmd = a:1
  "echomsg 'cmd='.cmd . ',0:'.a:0.',all:'.string(a:000)
  let fnames = []
  for i in range( 1, a:0 - 1 )
    let l = split( glob( a:000[i] ), "\n" )
    if len( l ) < 1
      let l = [ a:000[i] ]
    call extend( fnames, l )
  for fname in fnames
    let cmd1 = substitute( cmd, '%%', fname, 'g' )
    exe cmd1
    "echomsg cmd1


Is this to do :n file*.[ch] | sball

or with Vim 7 :n file*.[ch] | tab sball

sball isn't workable if you already have a big buffer list of other stuff.

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Also on Fandom

Random Wiki