Wikia

Vim Tips Wiki

Execute command on each file in a list

Talk0
1,610pages on
this wiki
Revision as of 06:14, July 13, 2012 by JohnBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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] ]
    endif
    call extend( fnames, l )
  endfor
  for fname in fnames
    let cmd1 = substitute( cmd, '%%', fname, 'g' )
    exe cmd1
    "echomsg cmd1
  endfor
endfunction

CommentsEdit

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.


Around Wikia's network

Random Wiki