Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to open four vim files on the command line:

vim file1 file2 file3

But I would like each file to be opened in a separate split:

vim -c "split file1" -c "split file2" -c "split file3" file4

(The above splits the screen horizontally 4 times)

Ideally what I would like to do is split the screen into 4 squares like:

|-------|-------|
|       |       |
|       |       |
|-------|-------|
|       |       |
|       |       |
|-------|-------|

I know how to do this once vim is open but I am unable to do this from the command line (using vs). Any ideas? Everything I try ends up looking like this (or a different variation):

|-------|-------|
|       |       |
|-------|       |
|       |       |
|-------|       |
|       |       |
|       |       |
|-------|-------|
share|improve this question
add comment

3 Answers

up vote 16 down vote accepted

You can use the 'wincmd' command to move to different windows as if you're pressing CTRL+W.

vim file4 -c 'split file2' -c 'vsplit file1' -c 'wincmd j' -c 'vsplit file3'

This will arrange the files as:

file1   file2
file3   file4

How it works: opens file4. Splits horizontally so file2 is above it. Splits vertically so file1 is to the left, moves to the next window (file1) and vertically splits again.

share|improve this answer
add comment

I wrote a script using this information that automatically splits the screen as I wish:

vimsp.py file1 file2 / file3

Results in

-----------
|f1  |f2  |
|    |    |
-----------
|file 3   |
|         |
-----------

Also, putting / in front of all files makes them all split vertically instead:

vimsp.py / file1 file2 file3

-------------
|file 1     |
-------------
|file 2     |
-------------
|file 3     |
-------------

https://gist.github.com/1376956

share|improve this answer
add comment

Strictly from the command-line:

vim -o3 <list of 9 files> -c:{vsp,vsp,wincmd\ j,vsp,vsp,wincmd\ j,vsp,vsp} \
  -c "windo execute 'argument ' . winnr()"

... will open 9 files in a 3x3 grid.

You could also write a function and add it to your .vimrc, something like the following might work. Hopefully an experienced vim scripter could weigh-in because I know this isn't correct:

function! mysplit(...)
  execute sp #1
  execute sp #1
  execute vsp
  execute vsp
  execute wincmd j
  execute vsp
  execute vsp
  execute wincmd j
  execute vsp
  execute vsp
  % I'm not at all experienced with writing vim scripts, so
  % the syntax on the following line is almost certainly not
  % correct; this is conceptual only.
  execute windo execute 'argument ' . winnr()
endfunction

... then use it from the command-line:

vim <list of 9 files> -c ':execute mysplit()'
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.