Vim Tips Wiki
(Move categories to tip template)
No edit summary
Tag: sourceedit
 
(5 intermediate revisions by 4 users not shown)
Line 3: Line 3:
 
|previous=886
 
|previous=886
 
|next=889
 
|next=889
|created=March 3, 2005
+
|created=2005
 
|complexity=basic
 
|complexity=basic
 
|author=Tye Z
 
|author=Tye Z
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
  +
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.
Load multiple files into a running Vim session with a single command. Put the following in your vimrc:
 
   
  +
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>
 
<pre>
com! -complete=file -nargs=* Edit silent! exec "!vim --servername " . v:servername . " --remote-silent &lt;args&gt;"
+
com! -complete=file -nargs=* Edit silent! exec "!vim --servername " . v:servername . " --remote-silent <args>"
 
</pre>
 
</pre>
   
Line 20: Line 23:
   
 
Then do something like this to edit multiple files:
 
Then do something like this to edit multiple files:
 
 
<pre>
 
<pre>
 
:Edit .vim/colors/*
 
:Edit .vim/colors/*
 
</pre>
 
</pre>
   
==Customizations==
+
'''Customizations'''
#If desired, substitute <tt>--remote-tab-silent</tt> in place of <tt>--remote-silent</tt> to load all the files in new tabs.
+
*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:
+
*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>
  +
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>
 
<pre>
com! -complete=file -nargs=* Edit silent! exec "!vim --servername " . v:servername . " --remote-silent ".escape(&lt;q-args&gt;,'\')
+
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>
 
</pre>
   
Line 38: Line 82:
   
 
==Comments==
 
==Comments==
  +
{{todo}}
  +
*Use fnameescape(), especially in the method that uses the shell.
   
  +
----
 
From [[VimTip1234]] comments section, an alternate method:
 
From [[VimTip1234]] comments section, an alternate method:
   

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.