Split windows and tabs in Vim
Mon, Feb 14, 2011Many 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:
<C-w>n
- :new horizontal split (editing a new empty buffer)<C-w>s
- :split window horizontally (editing current buffer)<C-w>v
- :vsplit window vertically (editing current buffer)<C-w>c
- :close window<C-w>o
- close all windows, leaving :only the current window open<C-w>w
- go to next window<C-w>p
- go to previous window<C-w><Up>
- go to window above<C-w><Down>
- go to window below<C-w><Left>
- go to window on left<C-w><Right>
- go to window on right
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
:tabn
- next tab:tabp
- previous tab:tabc
- close current tab:tabo
- close all other tabs leaving ONLY the current tab open
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.