Vim Tips Wiki
No edit summary
 
No edit summary
Tag: sourceedit
 
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
{{Tip
 
 
|id=888
 
|id=888
  +
|previous=886
|title=:e(dit) multiple files with a single command
 
  +
|next=889
|created=March 3, 2005 11:38
+
|created=2005
 
|complexity=basic
 
|complexity=basic
|author=Tye Z.
+
|author=Tye Z
 
|version=6.0
 
|version=6.0
 
|rating=4/6
 
|rating=4/6
  +
|category1=File Handling
|text=
 
  +
|category2=
Load multiple files into a running vim session with a single command! Put the following in your .vimrc:
 
 
}}
  +
When starting Vim, you can open multiple files, one to a window or tab, with the <code>-o</code>, <code>-O</code> or <code>-p</code> options. This tip discusses doing the same from within Vim.
   
  +
You can use a command like <code>:args *.c</code> to replace the argument list with all <code>.c</code> files, then display those files with a command like <code>:sall</code> (split window to show one file per window), or <code>:tab sall</code> (show one file per tab).
   
  +
Here is another method. Put the following in your [[vimrc]]:
  +
<pre>
 
com! -complete=file -nargs=* Edit silent! exec "!vim --servername " . v:servername . " --remote-silent <args>"
  +
</pre>
   
  +
This uses the shell to send remote commands to the current instance of Vim.
com! -complete=file -nargs=* Edit exec "!vim --servername " . v:servername . " --remote &lt;args&gt;"
 
   
 
Then do something like this to edit multiple files:
  +
<pre>
 
:Edit .vim/colors/*
  +
</pre>
   
  +
'''Customizations'''
  +
*If desired, substitute <code>--remote-tab-silent</code> in place of <code>--remote-silent</code> to load all the files in new tabs.
  +
*If running under Windows, you'll probably want to allow backslashes to occur in path names, like this:
  +
<pre>
 
com! -complete=file -nargs=* Edit silent! exec "!vim --servername " . v:servername . " --remote-silent ".escape(&lt;q-args>,'\')
  +
</pre>
   
  +
==Alternative==
  +
If you put the following code in your vimrc, you can simply do <code>:Etabs file list</code>, <code>:Ewindows file list</code> (horizontal windows), or <code>:Evwindows file list</code> (vertical windows).
   
  +
The commands can be abbreviated, and the function allows for file globbing, so doing something like <code>:Et *.html</code> should work. You may even find yourself using one of these to [[VimTip1285|replace the :edit command]]!
   
  +
<pre>
Then do something like this to 'e(dit)' multiple files:
 
  +
command! -complete=file -nargs=+ Etabs call s:ETW('tabnew', <f-args>)
  +
command! -complete=file -nargs=+ Ewindows call s:ETW('new', <f-args>)
  +
command! -complete=file -nargs=+ Evwindows call s:ETW('vnew', <f-args>)
  +
function! s:ETW(what, ...)
  +
for f1 in a:000
  +
let files = glob(f1)
  +
if files == ''
  +
execute a:what . ' ' . escape(f1, '\ "')
  +
else
  +
for f2 in split(files, "\n")
  +
execute a:what . ' ' . escape(f2, '\ "')
  +
endfor
  +
endif
  +
endfor
  +
endfunction
  +
</pre>
   
  +
== Alternative 2 ==
  +
I added this command to my [[vimrc]] to open a list of globbed files in tab pages. For example, the command <code>:Tabe *.py *.txt</code> opens a new tab page for each *.py and each *.txt file. When finished, the current tab page shows the first file added.
  +
<pre>
  +
command! -complete=file -nargs=* Tabe call Tabe(<f-args>)
  +
function! Tabe(...)
  +
let t = tabpagenr()
  +
let i = 0
  +
for f in a:000
  +
for g in glob(f, 0, 1)
  +
exe "tabe " . fnameescape(g)
  +
let i = i + 1
  +
endfor
  +
endfor
  +
if i
  +
exe "tabn " . (t + 1)
  +
endif
  +
endfunction
  +
</pre>
   
  +
==References==
  +
*{{help|--remote-silent}}
  +
*{{help|--remote-tab-silent}}
  +
*{{help|--servername}}
   
 
==Comments==
:Edit .vim/colors/*
 
  +
{{todo}}
 
  +
*Use fnameescape(), especially in the method that uses the shell.
 
 
 
}}
 
 
== Comments ==
 
On windows, an ugly window pops up with a message [press return to continue],
 
I assume this tip works well on unix.
 
 
-ND
 
 
 
http://cochin-kerala.blogspot.com
 
, March 3, 2005 16:47
 
----
 
 
Change the line to this to make the dialog box disappear automatically under MS Windows.
 
 
com! -complete=file -nargs=* Edit silent! exec "!vim --servername " . v:servername . " --remote &lt;args&gt;"
 
 
 
'''Anonymous'''
 
, March 3, 2005 17:39
 
----
 
It only seems to work for the gui version...
 
 
'''Anonymous'''
 
, March 4, 2005 15:52
 
----
 
Nope, works fine in console mode...
 
   
zdro--AT--yahoo.com
 
, March 7, 2005 6:37
 
 
----
 
----
  +
From [[VimTip1234]] comments section, an alternate method:
This hangs on window 2000pro + vim63 (32 bit console mode).
 
   
  +
You can load an arbitrary list of files with :args <pattern>, for instance:
The console vim63 is waiting for the spawned job to complete
 
while the spawned job is waiting for access to console?
 
   
  +
Open all .c or .h files in the directory (and it's subdirectories) two directories up from the current directory:
- Mohd at Onspec India
 
  +
:args ../../**/*.[ch]
http://automation-2004.blogspot.com
 
   
  +
The only caveat (and it's a major one) is that it's very slow.
Mohd at Onspec India
 
, March 8, 2005 12:49
 
 
----
 
----
<!-- parsed by vimtips.py in 0.471284 seconds-->
 

Latest revision as of 04:34, 27 January 2016

Tip 888 Printable Monobook Previous Next

created 2005 · complexity basic · author Tye Z · version 6.0


When starting Vim, you can open multiple files, one to a window or tab, with the -o, -O or -p options. This tip discusses doing the same from within Vim.

You can use a command like :args *.c to replace the argument list with all .c files, then display those files with a command like :sall (split window to show one file per window), or :tab sall (show one file per tab).

Here is another method. Put the following in your vimrc:

com! -complete=file -nargs=* Edit silent! exec "!vim --servername " . v:servername . " --remote-silent <args>"

This uses the shell to send remote commands to the current instance of Vim.

Then do something like this to edit multiple files:

:Edit .vim/colors/*

Customizations

  • If desired, substitute --remote-tab-silent in place of --remote-silent to load all the files in new tabs.
  • If running under Windows, you'll probably want to allow backslashes to occur in path names, like this:
com! -complete=file -nargs=* Edit silent! exec "!vim --servername " . v:servername . " --remote-silent ".escape(<q-args>,'\')

Alternative[]

If you put the following code in your vimrc, you can simply do :Etabs file list, :Ewindows file list (horizontal windows), or :Evwindows file list (vertical windows).

The commands can be abbreviated, and the function allows for file globbing, so doing something like :Et *.html should work. You may even find yourself using one of these to replace the :edit command!

command! -complete=file -nargs=+ Etabs call s:ETW('tabnew', <f-args>)
command! -complete=file -nargs=+ Ewindows call s:ETW('new', <f-args>)
command! -complete=file -nargs=+ Evwindows call s:ETW('vnew', <f-args>)
function! s:ETW(what, ...)
  for f1 in a:000
    let files = glob(f1)
    if files == ''
      execute a:what . ' ' . escape(f1, '\ "')
    else
      for f2 in split(files, "\n")
        execute a:what . ' ' . escape(f2, '\ "')
      endfor
    endif
  endfor
endfunction

Alternative 2[]

I added this command to my vimrc to open a list of globbed files in tab pages. For example, the command :Tabe *.py *.txt opens a new tab page for each *.py and each *.txt file. When finished, the current tab page shows the first file added.

command! -complete=file -nargs=* Tabe call Tabe(<f-args>)
function! Tabe(...)
  let t = tabpagenr()
  let i = 0
  for f in a:000
    for g in glob(f, 0, 1)
      exe "tabe " . fnameescape(g)
      let i = i + 1
    endfor
  endfor
  if i
    exe "tabn " . (t + 1)
  endif
endfunction

References[]

Comments[]

 TO DO 

  • Use fnameescape(), especially in the method that uses the shell.

From VimTip1234 comments section, an alternate method:

You can load an arbitrary list of files with :args <pattern>, for instance:

Open all .c or .h files in the directory (and it's subdirectories) two directories up from the current directory:

args ../../**/*.[ch]

The only caveat (and it's a major one) is that it's very slow.