History
Article Edit this page Discussion

Load multiple files with a single command

From Vim Tips Wiki

Jump to: navigation, search

Tip 888 Previous Next Created: March 3, 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>,'\')

[edit] 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

[edit] References

[edit] 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.


Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto Wiki
Doctor Who
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Mystery Science Theater 3000
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions
Send this article to a friend
"Load multiple files with a single command"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation