While Vim has a reputation as a simple editor for Linux, it has a lot of advanced features. One of them is the ability to work with multiple windows. Here’s how you can split and manage windows in Vim.

Opening New Windows in Vim

It’s easy to open a new window in Vim. If you’re already working in a file, you can split your existing window. This will allow you to view a different area of the file. You'd need to know the basics of how to use Vim to do so.

First, go to command mode by pressing Escape. Then type this command to split the window:

        :split
Vertically and horizontal split windows in Vim

You can also split a window vertically by typing:

        :vsplit
    

The result will resemble a GNU Screen or Tmux session, except that you can't detach it to keep it running.

To open multiple files when you start Vim, you can use the -o option followed by the names of the files you want to edit:

        vim -o file1 file2 file3
    

The -O (uppercase O) option will split the screen vertically.

You can also start Vim with just the list of files and use the :all command to open windows for all of them. To open vertical windows, use this command:

        :vertical all
    

Scrolling Windows in Vim

You’ll now have two different windows showing documents that you can scroll independently. To scroll the window forward, use Ctrl + F, and to scroll the window backward, press Ctrl + B. You can also switch to showing the windows side-by-side with Ctrl + v.

It's possible to split windows indefinitely. You can also create a brand new window with a new file with Ctrl + n. Ctrl + N, with a capital N, will create a new split window vertically.

You can also split a window with a new file with the :new command.

Switching Between Split Windows in Vim

Switching between Vim windows is also easy. Use the Ctrl + W prefix plus the direction of the window you want to move to. The movement keys are similar to the existing Vim movement key commands.

For example, to move down, press Ctrl + W + j, and to move up, press Ctrl + W + k. To move to the window on the left, press Ctrl + W + h, and to move right, press Ctrl + W + l.

To go to the top window, use Ctrl + W + t, and for the bottom window, type Ctrl + W + b.

You can swap the positions of the windows with the Ctrl + K and Ctrl + J keystrokes. These are capital letters, as Vim commands are case-sensitive. The former will move the current window to the top, and the latter will move it to the bottom. Ctrl + H and Ctrl + J will do the same to vertically split windows.

Opening Terminal Windows in Vim

Vim terminal window

If you want to run shell commands or even an entire terminal session, you don't have to leave Vim. You can run commands right within the editor.

To run a command, just type a colon (:), as you would with any other Vim command, an exclamation point (!), and the command. For example, to run an ls command in Vim:

        :!ls
    

This will show a new screen with the output from the command and prompt you to press Enter to return to Vim.

You can also run a full terminal session with the :terminal command. This will split the window and start a terminal session within Vim. You can run multiple commands and run full-screen text programs like the man command. You can even run another Vim session if you want to.

Resizing Split Windows in Vim

It's easy to change the size of your Vim windows. The Ctrl + W + Plus and Ctrl + W + Minus keys will increase and decrease the size of the current window by one line.

You can also specify the size of a window ahead of time by prefixing the :split command with the number of lines you want the window to be. For example, to open a window three lines high, use this command:

        :3split
    

Closing Split Windows in Vim

Vim showing error message when trying to close unsaved windows

When you've finished working in a window, you can close it with the :q command. If you have unsaved changes in a window and want to close it anyway, append an exclamation point:

        :q!
    

To close every other window but the one you're working on, use the :only command. Again, you can append an exclamation point to close any unsaved windows.

To close every window and quit Vim entirely, use :qall. To do so without saving, use :qall!.

To save every window, use :wall.

You can also combine the save and quit operations:

        :wqall
    

Vim Lets You Edit Multiple Files at the Same Time

With Vim, you can easily edit multiple files and even run terminal commands by splitting windows and moving between them. Of the classic Unix text editors, this was a feature most associated with GNU Emacs.

While multiple windows have been part of Vim for a long time, you can add features from other text editors to Vim as well.