Split windows and tabs in Vim

Mon, Feb 14, 2011
Embracing Vim

Many times, you want to view 2 or more files at once, and for that you will want to learn about Vim’s ability to split its window into multiple panes using the :split or :vsplit commands.

To open a different file in a new split you can specify the filename as part of the command

 :vsplit test.rb

Once you have multiple windows open, there are many window commands available all starting with the <C-w> key:

If you use any of these commands frequently, you might want to think about mapping them to more suitable keys for your environment, for example, I like to map the <Tab> key to allow me to switch panes, along with the | and - keys for splitting the window (same shortcuts I use in tmux or screen) by adding to my .vimrc file:

 map <Tab> <C-W>w
 map <Bar> <C-W>v<C-W><Right>
 map -     <C-W>s<C-W><Down>

I will talk a little more about customizing Vim and my .vimrc file in a future article.

Using Tabs

In addition to split window panes, Vim also provides the ability to manage buffers in multiple tabs, just like your browser, and most traditional windows file editors.

To open a new empty tab

 :tabnew

To open an existing file in a new tab

 :tabedit [FILENAME]

Navigating between tabs can be done with the mouse, or with commands

You can also navigate to next/previous tabs using the <C-PageDown> and <C-PageUp> keys.

If you use the gvim GUI then you get real GUI tabs, if you use the traditional terminal vim this feature is still available, but you get simple character tabs which look a little odd to me. I have a hard time distinguishing the shortened filenames in these ’tabs’, so I have not used this feature in any depth yet.