Wikia

Vim Tips Wiki

Changes: Execute command on each file in a list

Edit

Back to page

(Remove html character entities)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 4: Line 4:
 
|previous=1229
 
|previous=1229
 
|next=1231
 
|next=1231
|created=May 13, 2006
+
|created=2006
 
|complexity=basic
 
|complexity=basic
 
|author=Eric Arnold
 
|author=Eric Arnold
Line 44: Line 44:
   
 
==Comments==
 
==Comments==
Is this to do <tt>:n file*.[ch] | sball</tt>
+
Is this to do <code>:n file*.[ch] | sball</code>
   
or with Vim 7 <tt>:n file*.[ch] | tab sball</tt>
+
or with Vim 7 <code>:n file*.[ch] | tab sball</code>
   
 
----
 
----
<tt>sball</tt> isn't workable if you already have a big buffer list of other stuff.
+
<code>sball</code> isn't workable if you already have a big buffer list of other stuff.
   
 
----
 
----

Latest revision as of 06:14, July 13, 2012

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