sponsor Vim development Vim logo Vim Book Ad

basic Tip #146: opening multiple files from a single command-line

 tip karma   Rating 175/48, Viewed by 7044 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   October 23, 2001 13:29      complexity:   basic
author:   [email protected]      as of Vim:   5.7

i use the :split command a lot -- both to open a second window containing the currently edited file and to edit a new file altogether (with the :split <filename> option).  however, i also like to be able to edit more than one file and calling :sp multiple times is inconvenient.  so, i created the following command, function and abbreviation:

function! Sp(...)
  if(a:0 == 0)
    sp
  else
    let i = a:0
    while(i > 0)
      execute 'let file = a:' . i
      execute 'sp ' . file

      let i = i - 1
    endwhile
  endif
endfunction
com! -nargs=* -complete=file Sp call Sp(<f-args>)
cab sp Sp


this retains the behaviour of :sp in that i can still type :sp (the abbreviation takes care of that).  :Sp takes any number of files and opens them all up, one after the other.

the things i have noticed are that this causes 'sp' to be expanded to 'Sp' everywhere, even in search patterns.  also, prepending 'vert' doesn't work.  if there is interest, i'll do that.

 rate this tip  Life Changing Helpful Unfulfilling 

<< Changing DOS style end of line to UNIX, or vise-versa | How to write a plugin >>

Additional Notes

[email protected], June 26, 2003 0:50
This is great! It saves the effort of typing multiple :sp under vim. I have 2 suggestions

1. If I use

   vim
   :Sp a b c

    It results in 4 windows in vim. One is empty. Is this could be improved?

2. Is there a way to make a new_cmd that splits a window to 3, each one for a file specified in the command line?
    For example,

    vim a b c
    :new_cmd

   then I could see 3 windows in vim. One for a, one for b, and one for c.

Anyway, still thanks for your :Sp.
[email protected], May 6, 2004 11:38
Just before the endif, add this:

  windo if expand('%') == '' | q | endif

Empty windows will be closed (if unmodified).
[email protected], January 31, 2006 10:16
That tip/function work great. I have this in my .vimrc to load ".vimrc and the file that has functions etc...:

nmap ,v :Sp ~/.vimrc ~/.vim/my_vim/vimfuncs.vim<CR>

Thanks for the tip!

TonyB
[email protected], January 31, 2006 10:41
One more question:
   If I prefer verticle split windows, how is this done? Now, the windows are horizontal.

Thank you

TonyB
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo