vim

Split windows

Syntax#

  • :split <file>
  • :vsplit <file>
  • :sp <- shorthand for split
  • :vsp <- shorthand for vsplit

Remarks#

When called from the command line, multiple files can be provided in the argument and vim will create one split for each file. When called from ex mode, only one file can be opened per invocation of the command.

Opening multiple files in splits from the command line

Horizontally

vim -o file1.txt file2.txt

Vertically

vim -O file1.txt file2.txt

You may optionally specify the number of splits to open. The following example opens two horizontal splits and loads file3.txt in a buffer:

vim -o2 file1.txt file2.txt file3.txt

Opening a new split window

You can open a new split within Vim with the following commands, in normal mode:

Horizontally:

:split <file name>
:new

Vertically:

:vsplit <file name> 
:vnew 

split will open the file in a new split at the top or left of your screen (or current split.) :sp and :vs are convenient shortcuts.

new will open an empty split

Changing the size of a split or vsplit

You may sometimes want to change the size of a split or vsplit.

To change the size of the currently active split, use :resize <new size>. :resize 30 for example would make the split 30 lines tall.

To change the size of the currently active vsplit, use :vertical resize <new size>. :vertical resize 80 for example would make the vsplit 80 characters wide.

Shortcuts

  • Ctrl + w and + increase the size of the splited window
  • Ctrl + w and - decrease the size of the splited window
  • Ctrl + w and = set an equal size to the splited windows

Close all splits but the current one

Normal mode
Ctrl-wo

Ex mode

:only

or short

:on

Managing Open Split Windows (Keyboard Shortcuts)

After you have opened a split window in vim (as demonstrated by many examples under this tag) then you will likely want to control windows quickly. Here is how to control split windows using keyboard shortcuts.

Move to split Above/Below:

  • Ctrl + w and k
  • Ctrl + w and j

Move to split Left/Right:

  • Ctrl + w and h
  • Ctrl + w and l

Move to split Above/Below (wrap):

  • Ctrl + w and w

Create new empty window:

  • Ctrl + w and n -or- :new

Create new split horizontal/vertical:

  • Ctrl+W, S (upper case)
  • Ctrl+W, v (lower case)

Make the currently active split the one on screen:

  • Ctrl + w and o -or- :on

Move between splits

To move to split on left, use <C-w><C-h>
To move to split below, use <C-w><C-j>
To move to split on right, use <C-w><C-k>
To move to split above, use <C-w><C-l>

Sane split opening

It’s a better experience to open split below and on right

set it using

set splitbelow
set splitright

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow