Last Updated: September 27, 2021
·
7.358K
· lwheng

vim: Open multiple files into splits in 1 command

In the situation where you want to open multiple files in vim using splits (or vsplits), here's what I did before:

vim <file1>
:split <file2>
:split <file3>

And so on...

Here's the tip:

vim -o <file1> <file2> ... <fileN>

This command opens n files into horizontal splits. If you prefer vertical splits, use -O instead.

Suppose you want to get to Line 15 of every single split:

:windo 15

In fact, in general:

:windo execute [your command]

But wait, there's more! Say now that you're done editing the files and you want to save all of them at once:

:wa

Yes, it is that simple. The usual :w plus a.
I bet you can guess how to close/quit all files at one go.

Cheers

(Still getting used to markdown. Pardon me)

(This tip is similar to https://coderwall.com/p/n1abyq. Just giving credits to Alexander, and that I learned about -p from his tip)

9 Responses
Add your response

My guess this tip is very similar to https://coderwall.com/p/n1abyq

Actually you can also use use -O and -p params. Let's check man vim:

-o[N]       Open N windows stacked.  When N is omitted, open one window for each file.
-O[N]       Open N windows side by side.  When N is omitted, open one window for each file.
-p[N]       Open N tab pages.  When N is omitted, open one tab page for each file.
over 1 year ago ·

Thanks Alexander! Yes you are right, it is very similar to your tip.

over 1 year ago ·

This is awesome man. Will use this today and try it out.

over 1 year ago ·

You can type :sp and split the same file. So you can keep looking at one method in one pane, and then move about in the same file in the other pane

orgin: http://therealadam.com/2012/03/15/how-i-use-vim-splits/

over 1 year ago ·

Thx. Need to add how to switch between these splitted windows - Ctrl+W

over 1 year ago ·

Thanks Sergey for your suggestion. I've added another tip for that.

https://coderwall.com/p/dfania

over 1 year ago ·

Ctrl+w s splits a window horizontally too!

over 1 year ago ·

Great tip! Is there a way to do this but with vertical splits instead?

over 1 year ago ·

Yes! use capital O instead.
vim -O file1 file2

over 1 year ago ·