21

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):

|-------|-------|
|       |       |
|-------|       |
|       |       |
|-------|       |
|       |       |
|       |       |
|-------|-------|
1
  • I know how to do this once vim is open Could you explain how to do it once Vim is open? Once I open four windows I can only make one window go all the way to the top/left/right/bottom. Commented Feb 9, 2020 at 1:40

3 Answers 3

22

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.

8

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

1

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()'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .