×

Search anything:

Splitting VIM windows

Internship at OpenGenus

Get this book -> Problems on Array: For Interviews and Competitive Programming

VIM is a powerful, lightweight open-source text editor. In this article, we learn how to split VIM screens, switch between them, change sizes and close them.

Table of contents.

  1. Introduction.
  2. Splitting vim vertically.
  3. Splitting vim horizontally.
  4. Switching between windows.
  5. Closing windows.
  6. Changing window sizes.
  7. Closing windows.
  8. Summary.
  9. References.

Introduction.

VIM is a powerful lightweight text editor that comes pre-installed in Linux systems. We can use it to edit simple files or even manage multiple files at once very easily using built-in commands. It is highly configurable and programmable, that is, we can configure it to suit our own specific needs.

Efficiency is a very important part of development and switching between mouse and keyboard every time we want to open files, edit them, switch between them, move cursors, etc becomes a time-consuming task. With VIM's shortcuts and capabilities we can handle everything from the keyboard.

In this article at OpenGenus, we discuss how to split VIM into multiple windows both vertically and horizontally, switch between them, change their sizes according to our needs and close open windows.

Splitting vim vertically.

To split the screen into two sides vertically, we press CTRL+w + v. For this to work we must be in normal mode. We activate this mode by pressing the escape key.

tty-3

To switch between screens we use CTRL+w followed by either r or l for right or left respectively. This moves the cursor to the left or right side depending on what we want.

Another way to split multiple files between multiple windows is to execute the following command:

vim -O file1.txt file2.txt file3.txt

tty1-3

In this example, we open three files in three different vim windows.

Another way to split vim windows vertically is to execute the command while in an open file in VIM:

:vsplit

The above command splits the currently open window into two, We can close one by using the :q command to quit.

Furthermore, we can open a file into a newly split window by passing the file name as an argument. For example, to open a file, file2.txt into a new window we write:

:vsplit file2.txt

Now we will have a screen that is vertically split between two files, file1.txt and file2.txt.
tty2-3

Splitting vim horizontally.

On the other hand, to split vim horizontally, we use the same method, except in this case instead of pressing v after CTRL+w we press s.
tty3-3

We can also split vim screens by opening multiple files using the -o option. For example to open three files, file1.txt, file2.txt and file3.txt we write:

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

tty4-2
We can also split vim windows horizontally by executing the command while in an open file in VIM:

:split

The above command splits the currently open window into two, the upper and the lower windows. We can close one by using the :q command to quit.

Furthermore, we can open a file into a newly split window by passing the file as an argument. For example, to open a file, file1.txt into a new window we write:

:split file1.txt

Now we will have a screen that is vertically split between two files.
tty5-2

We discuss shortcuts used to switch between split windows in VIM.

Switching between windows.

To switch between the windows, we use the following shortcuts:

  • CTRL + w + w - to switch between the currently open windows.
  • CTRL + w + l - to switch to the left windows.
  • CTRL + w + h - to switch to the right windows.
  • CTRL + w + k - move up the windows.
  • CTRL + w + j - move down the windows.

Notice that l, h, k and j are the same keys we use to traverse a file with VIM.

We can also switch between the windows using the arrows keys. That is;

  • CTRL + w + left arrow key - to switch to the left windows.
  • CTRL + w + right arrow key - to switch to the right windows.
  • CTRL + w + up arrow key - move up the windows.
  • CTRL + w + down arrow key - move down the windows.

Changing screen size.

Apart from splitting VIM windows, we can also change the sizes of different screens depending on our needs.

For example, if we split the screen vertically we can increase the left or right pane by pressing CTRL+w followed by Shift + > or < for either left or right.
To increase the size of the left pane, we press CTRL+w, Shift+> and to Increase the size of the right pane, we press CTRL+w, Shift+<
tty6-2

On the other hand, if we split the screen horizontally, we can either increase or decrease the height of either screen by pressing CTRL+w followed by Shift+'+' to increase the current pane or Shift+'-' to decrease the height of the current pane.
tty7-2

We can equalize screens divided horizontally or vertically by pressing CTRL+w followed by '=' sign.

Closing windows.

To close the current window we write:

:close

The above command closes the current window we are on.

We can also close the current window by using the shortcut, CTRL + w + c.

If we want to close all windows except the current window we write:

:only

Or use the shortcut; CTRL + w + o.

tty8-1

To close all windows we can quit vim by writing:

:qall

Note that the above does not save the changes made. To save the changes we need to write the changes by executing the command:

:wqall

Summary.

Splitting screens allows us to operate on a file more effectively, we can make changes to different parts of the same file simultaneously, especially for programmers.

In this article at OpenGenus, we have learned how to split VIM into multiple windows both vertically and horizontally, switch between them, change their sizes according to our needs and close open windows.

References.

For more information about VIM usage, we can execute the command $ man vim in a Linux terminal.

Splitting VIM windows
Share this