44lupw
Last Updated: January 12, 2018
·
3.16K
· lwheng

vim: Open multiple files into splits in 1 command

vim

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

7960

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 ·
7971

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

over 1 year ago ·
7973

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

over 1 year ago ·
7982

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 ·
8128

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

over 1 year ago ·
8129

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

https://coderwall.com/p/dfania

over 1 year ago ·
8189

Ctrl+w s splits a window horizontally too!

over 1 year ago ·
8190

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

over 1 year ago ·
8191

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

over 1 year ago ·