Vim Tips Wiki

Opening multiple files from a single command-line

Revision as of 23:15, March 20, 2014 by (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
1,614pages on
this wiki
Tip 146 Printable Monobook Previous Next

created 2001 · complexity basic · author salmanhalim · version 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)
    let i = a:0
    while(i > 0)
      execute 'let file = a:' . i
      execute 'sp ' . file
      let i = i - 1
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.


This is great! It saves the effort of typing multiple :sp under vim. I have two suggestions.

1. If I use

: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

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

Just before the endif, add this:

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

Empty windows will be closed (if unmodified).

I added an option to split also vertically and made filenames expand through glob:

function! Sp(dir, ...)
  let split = 'sp'
  if a:dir == '1'
    let split = 'vsp'
  if(a:0 == 0)
    execute split
    let i = a:0
    while(i > 0)
      execute 'let files = glob (a:' . i . ')'
      for f in split (files, "\n")
        execute split . ' ' . f
      let i = i - 1
    windo if expand('%') == '' | q | endif
com! -nargs=* -complete=file Sp call Sp(0, <f-args>)
com! -nargs=* -complete=file Vsp call Sp(1, <f-args>)

This tip is superseded by :argadd:

  :argadd *.html

Starting in splitsEdit

If you want to start vim with several files in a splitted window, just type

vim -o a b c

for the horizontal split, and

vim -O a b c

for the vertical split.

To change between the windows opened


For further information, you can consult:

Around Wikia's network

Random Wiki