How to open files in vertically/horizontal split windows in Vim from the command line

412

155

There must be a way, something like this:

vim -[option] <file-list>

to open files from command prompt and not from within Vim.

  • split windows vertically or/and horizontally
  • in separate tabs

Andrei Chikatilo

Posted 2009-10-05T18:01:06.470

Reputation: 4 351

Answers

398

I'm assuming you mean from the command line. From vim --help:

-o[N]                Open N windows (default: one for each file)
-O[N]                Like -o but split vertically

So type this to open files split horizontally, for example:

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

Laurence Gonsalves

Posted 2009-10-05T18:01:06.470

Reputation: 5 021

@Cascabel -whatever[N] means that N is optional and can be omitted, I believe it derives from standard ebnf syntax

– user3338098 – 2018-07-24T17:21:25.787

@user3338098 I think there may have been a deleted comment here; Laurence's "Indeed..." comment implies that it was causing problems when omitted. – Cascabel – 2018-07-24T17:32:07.740

@Cascabel -o without N works as documented. so omitting N solved the problem. I suspect they were literally doing something like vim -o[2] file1 file2 which is invalid syntax. – user3338098 – 2018-07-27T17:11:01.857

14Really? I've never had to provide an N. – Cascabel – 2009-10-05T20:03:13.463

3Indeed, it seems to be something in my .vimrc causing the trouble. If I move it aside, -o without N works as documented. – Laurence Gonsalves – 2009-10-06T03:35:07.210

23-o is like :split, -O is like :vsplit – Evgeni Sergeev – 2014-01-21T10:06:24.960

445

Ctrl+W, S (upper case) for horizontal splitting

Ctrl+W, v (lower case) for vertical splitting

Ctrl+W, Q to close one

Ctrl+W, Ctrl+W to switch between windows

Ctrl+W, J (xor K, H, L) to switch to adjacent window (intuitively up, down, left, right)

LB40

Posted 2009-10-05T18:01:06.470

Reputation: 4 729

4:q also closes window – Alexandre Bourlier – 2014-12-29T16:52:50.930

1Ctrl+w to switch windows doesn't seem to work on mac terminal. Ctrl+w,w does though – slashdottir – 2015-06-26T21:23:47.307

@slashdottir Ctrl+w works on Mac terminal too! I'm on Yosemite. ZZ also closes a focused window. – Majid – 2015-08-13T20:45:12.740

should be Ctrl + W, v <= lower case – zx1986 – 2015-09-30T07:09:30.387

swapped order of down and up for Ctrl + W J(down) and Ctrl + W K(up) – pseyfert – 2016-11-30T17:02:17.807

For horizontal splitting, it doesn't need to be capital S. – shampoo – 2017-04-05T04:04:05.187

Thanks this helped me figure out how to move backwards, Ctrl + w left arrow. I always use ctrl + ww to cycle forward. If your on the bottom and need to access a file on the top quickly, and don't want to go forward, you need to move up first with ctrl + w up arrow before moving left(or right). Thanks. – Brian Thomas – 2017-05-11T20:09:02.340

@EricLeschinski I can't get that to work. What's a screen? – None – 2017-11-30T13:17:24.477

@DrEval strictly speaking, we're talking about Vim splits, which are views into buffers. Here's a couple resources to get you started, 1) https://www.sourceallies.com/2009/11/vim-splits-an-introduction/, 2) https://robots.thoughtbot.com/vim-splits-move-faster-and-more-naturally

– Atav32 – 2018-05-07T23:37:21.700

@Atav32 I know about splits. I was asking about screens to force the issue because that term is not used in the context of splits, tabs, views, windows, buffers... there are enough terms without inventing new ones. – None – 2018-05-08T08:43:35.993

@DrEval I generally assume people asking questions on SO are asking in good faith – Atav32 – 2018-05-08T20:58:22.367

@Atav32 I was responding to a comment, not a question, and this is Superuser, not stack overflow. The comment used the wrong term and I asked a simple question in response in an attempt to politely point this out as you can't moderate comments and it's not wrong enough to warrant a flag. Plus it didn't work anyway, no matter what you call it. – None – 2018-05-09T08:12:06.200

11To switch screens, Press Ctrl-w and then up arrow or down arrow to switch screens. – Eric Leschinski – 2012-10-17T01:20:58.260

7@EricLeschinski, I prefer ctrl+w ctrl+w to cycle through windows as arrow keys feel a little anti-vi – Lucas – 2013-01-27T20:39:05.817

7Ah but you can use regular vim movements, e.g. ctrl+w j to jump to the buffer below the current one. – mitjak – 2013-10-04T21:00:45.490

331

While running vim:

  1. :sp filename for a horizontal split
  2. :vsp filename or :vs filename for a vertical split

Taylor Leese

Posted 2009-10-05T18:01:06.470

Reputation: 3 615

:vsplit filename is the same as :vsp filename and :vs filename, but perhaps a tiny bit easier to remember for some people – b_dev – 2016-09-13T17:14:59.497

64This doesn't answer the question raised...but it SURE helped me out :D Thanks! – Abel – 2011-03-24T18:45:03.060

1Many thanks for it. It's exactly what I need it! – mapcuk – 2012-03-16T08:43:33.403

7thanks, just what I was looking for. I also :set splitright – zack – 2012-08-07T23:44:16.093

4ctrl-ww for switching between splits – Nerrve – 2014-02-17T09:29:34.020

22

another interested trick is the CLI -p argument - which opens them in separate tabs for recent versions of vim and gvim.

gvim -p file1.txt file2.txt

dls

Posted 2009-10-05T18:01:06.470

Reputation: 321

1

Another useful trick that I just found out, is that you can use wildcards in the filelist to open multiple files. Say you want to open file1.txt, file2.txt, and file3.txt all in separate tabs but don't feel like typing that all out you can just do:

vim -p file*

I frequently find myself needing to open a lot of files with a similar prefix, and this has been quite helpful

Brent

Posted 2009-10-05T18:01:06.470

Reputation: 127

12That does not have to do with Vim itself but with the shell you are using. It is the shell that expands globs. – Kazark – 2012-08-24T19:16:13.330