Tips on window management in Vim

How to split windows and manage tabs.

Vim has a powerful window system that makes it easy to work with multiple files. This post is a short reference on how to open tabs and split windows quickly with the built-in commands, and with two popular plugins, CtrlP and NERDTree.

Commands

Opening a file in the current window

To open a file in the current window, use :e <path>. If you use the command without an argument, it will reload the current file in the current window, which is useful if the file is changed outside of your Vim session.

Tabs

To open a file in a new tab, use :tabedit <path>. Using it without arguments results in an empty tab. I usually close tabs with :q, but you can use :tabclose as well. The difference is that with :tabclose you can't accidentally close your last tab. Another nice command is :tabonly that kill all tabs, except the currently selected one. All opened tabs can be closed at once with :qa.

You can cycle between tabs with Ctrl+PgDown and Ctrl+PgUp. There are other ways to work with tabs effectively, but I don't use them because I usually have very few tabs open at a time. If you are a heavy tab user, I recommend the following posts:

Window splitting

Splitting a window in half is useful if you have to reference contents from other files, or that you have to jump between multiple files in rapid succession. On smaller displays, I rarely use them, but on a large screen, they can come handy.

To split the current window horizontally or vertically, use the :split or :vsplit commands. If issued without arguments, it opens the current file in the new window. At first, this might not sound interesting, but it can be handy if you have to edit a large file in multiple different places, or you have to refer to other parts of the same file while editing.

It's also good to know that Vim synchronizes the windows opened for the same file, so you can freely edit in any of them without accidentally overwriting something.

Ctrl+w s and Ctrl+w v are shortcuts for :split and :vsplit.

Note: The same thing can be achieved with tabs by :tabedit %, as the % refers to the current file in commands.

To open a new file in a split, you can just pass the path as an argument to :split or :vsplit.

To switch focus from one window to another, use Ctrl+w <direction>, where direction can be an arrow key or one of the h j k l characters.

To resize a window, first navigate to it by using the commands above, then use any of the following:

  • Ctrl+w <num> +: increase height
  • Ctrl+w <num> -: decrease height
  • Ctrl+w _: maximize height
  • Ctrl+w <num> >: increase width
  • Ctrl+w <num> <: decrease width
  • Ctrl+w <num> |: maximize width
  • Ctrl+w =: reset the size of all windows

For more examples, check this post.

Every window can be extracted from the layout, and be opened in a separate tab with Ctrl+w T.

Make it easier with plugins

I use two plugins for file-related operations: CtrlP and NERDTree. I use CtrlP to quickly find and open files, and NERDTree to explore and manipulate the folder/file structure.

With CtrlP, just select an entry from the list then press the following to open the file according to your needs:

  • enter: current window
  • Ctrl+t: new tab
  • Ctrl+x: split current window horizontally
  • Ctrl+v: split current window vertically

To open a file in the current window, you just need to simply select an entry from the list and press enter.

NERDTree also provides everything necessary to use tabs and splits. I find the default action keys a bit counter-intuitive, but the help menu (?) is always there to help:

  • enter: current window
  • Shift+t: new tab
  • i: split current window horizontally
  • s: split current window vertically

Mouse support

Most of the features covered in this post are accessible by a mouse as well. For example, you can easily switch between tabs by clicking on their title or resize window simply by moving the borders of the windows with drag and drop.

It's a popular opinion that you should use the keyboard exclusively when interacting with Vim. I find it to be true most of the time, but if you are just starting to work with multiple windows it might be useful to enable mouse support with :set mouse=a.

Summary

Vim has powerful mechanisms to support editing sessions that span through multiple files. If you know the commands and plugins, tabs and multiple windows can be opened in a blink. For more information, make sure to check out the built-in help, it contains everything you need. For example, check :help :split or :help windows.

May 15, 2018
In this article