The most important thing to remember
I want you to remember one mapping for practically all window operations in Vim: <C-w> (control W) in normal mode.
But before we continue, a small warning: if you’re on an operating system where <C-w> closes the current browser tab (like Windows), don’t press it — you’ll end up closing the current lesson! I would recommend setting up a new map to <C-w> inside the editor so that you can continue this lesson without disruption. Something like :nnoremap W <C-w> should do the trick, which will map the W (shift w) key to function the same way as <C-w>. It’s a bit of a kludgy work around… but I haven’t figured out a way to get it working just yet!
With that out of the way, let’s get on with the lesson.
Using <C-w> in normal mode, you can:
- create new windows both horizontally (
<C-w>s) and vertically (<C-w>v) - close windows you don’t need any more (
<C-w>c) - navigate between windows
- move windows around to create a different layout
- resize windows
… basically everything you’ll ever need!
Let’s try some <C-w> maps in the editor! We already know how to create new windows with the :split and :vsplit commands — and we also know that we can use :q and :close to close any windows that we no longer need. This time, instead of using the commands, lets use the <C-w> maps instead.
We’re going to create and close some windows as before, but this time:
- instead of using
:splitto open a new horizontal split, try using<C-w>s! - instead of using
:vsplitto open a new vertical split, try using<C-w>v! - instead of using
:q/:closeto close a window, try using<C-w>c!