Vim Tips Wiki
m (Exec cmd for each in a OPENBRACKET glob CLOSEBRACKET list in moved to Execute command on each file in a list: Page moved by JohnBot to improve title)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=1230
 
|id=1230
  +
|previous=1229
|title=exec cmd for each in a __OPENBRACKET__glob__CLOSEBRACKET__ list in
 
  +
|next=1231
|created=May 13, 2006 5:21
+
|created=2006
 
|complexity=basic
 
|complexity=basic
|author=eric_p_arnold--AT--yahoo.com
+
|author=Eric Arnold
 
|version=n/a
 
|version=n/a
 
|rating=0/0
 
|rating=0/0
  +
|category1=
|text=
 
  +
|category2=
 
 
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, which is probably re-inventing something, or just missing a Vim feature somewhere. The gears are just grinding tonight. If not, the function might be useful.
 
 
 
 
 
 
 
 
 
 
command! -nargs=* -complete=file New call Foreach( "new %% ", &lt;f-args&gt; )
 
 
command! -nargs=* -complete=file Vnew call Foreach( "vnew %% ", &lt;f-args&gt; )
 
 
command! -nargs=* -complete=file Diff call Foreach( "vnew %% | diffthis", &lt;f-args&gt; )
 
 
 
 
" %% 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 ) &lt; 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
 
 
 
 
 
 
 
 
}}
 
}}
 
I wanted to do
  +
<pre>
 
:new file*.[ch]
  +
</pre>
   
 
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.
== Comments ==
 
Is this to do:
 
   
  +
<pre>
:n file*.[ch] | sball
 
 
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
  +
</pre>
   
 
==Comments==
or with Vim 7
 
 
Is this to do <code>:n file*.[ch] | sball</code>
   
:n file*.[ch] | tab sball
+
or with Vim 7 <code>:n file*.[ch] | tab sball</code>
   
Or am I missing something?
 
 
Bheeshmar
 
, May 13, 2006 20:47
 
 
----
 
----
 
<code>sball</code> isn't workable if you already have a big buffer list of other stuff.
 
sball isn't workable if you already have a big buffer list of other stuff.
 
 
 
   
eric_p_arnold--AT--yahoo.com
 
, May 14, 2006 3:15
 
 
----
 
----
<!-- parsed by vimtips.py in 0.695977 seconds-->
 

Latest revision as of 06:14, 13 July 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

Comments[]

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.