5

I want to open three files file1.txt file2.txt file3.txt in vertical and horizontal split combination, like this:

+-------------------------------+
|               |               |
| file1.txt     |               |
|               |   file3.txt   |
|               |               |
|               |               |
+---------------+               |
|               |               |
| file2.txt     |               |
|               |               |
|               |               |
+-------------------------------+

(ascii art done with http://asciiflow.com/)

I know about -o and -O options from this question: How to open multiple files in split mode?
However as I read in help -o adding more than one flag overrides the other so nothing like

vim -o file1.txt file2.txt -O file3.txt

is going to work.

I was also thinking about saved sessions but I'm not sure if that's the right way to go.

Q: What's the best way to achieve this or similar custom layout (for example with three files on the left insted of two)?

Additional info about the use case: I'm writing script to generate a learning flashcard from a longer text and wanted to add an interactive step where user can edit prepared flashcard in vim. Question and answer part would be displayed on the left and the full text on the right.

2 Answers 2

8

You could try it like this:

vim -c "lefta vsplit file2.txt|split file1.txt" file3.txt

With the option -c you can specify commands that should be executed after the first file was loaded.

So here:

  1. The file file3.txt is loaded.
  2. The command lefta vsplit file2.txt|split file1.txt is executed. This are in fact two commandsseparated by |
    • lefta vsplit file2.txt opens a window with the file file2.txt to the left of file3.
    • split file1.txt opens the file1.txt above file2.txt

See :h lefta, :h vplit, :h split.

1
  • And opening more files on the left can be achieved with additional | split file4.txt. Thanks, this looks like what I need. Commented Jun 20, 2020 at 11:09
7

Actually, you don’t need saved sessions for this; vim -S will happily accept any file containing vimscript. So, write some commands for your custom layout (stolen from the other answer)

edit file3
leftabove vsplit file2
split file1

And then do vim -S layout.vim. This also helps avoid cluttering the command and makes it easier to edit the layout commands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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