Open Vim and split files

4
(80)

When Vim is open already, you can open another file in a split with :split file/path.rb or :vsplit file/path.js. But you can also open Vim from the command line with files already split:

  • vim -o file1.txt file2.txt – Open both files in Vim, split horizontally
  • vim -O file1.txt file2.text – Open both files in Vim, split vertically

Since the command line takes any number of arguments, you can also open multiple files with a * glob or even {} braces from your shell:

  • vim -o file/path/*.rb – Open all the .rb files at the path in horizontal splits
  • vim -O file/path/{one,two}.txt – Open one.txt and two.text in vertical splits

Why would you want to do this? A big reason to open specific files from the command line is to have them in your shell history. Since you can reverse autocomplete from your history with ctrl-r, having specific file names in your history allows you to quickly get back to editing the same files in a snap. (See also: Open to a pattern.)

In the video demo below, I open both the README files in the project in vertical splits with vim -O README*. Then I open all the markdown files in horizontal splits with vim -o *.md. You can see that if you use a glob that selects too many files, it can get unwieldy quickly.

How useful was this tip?

Average rating 4 / 5. Vote count: 80

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

105 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts