Vim windows and window-splitting

A lifechanging feature of Vim for me was the window-splitting feature. In MacVim, you can even access a tiny slice of this through the Apple menu (File -> Split-Open). So you can view multiple files in other "windows" in the same Vim session. In fact, you can view the same file at different lines in different windows. And you can split windows both vertically (side by side) and horizontally (default). You can split already split windows. And you can change split points through ex commands, control commands, and by using your mouse.

  1. Split window with :sp or C-w s. As always, C- means to press control and the given key. All the 'window' commands will use C-w.

  2. :sp splits the current window into two above each other, creating a horizontal separator. But you can split vertically (creating a new vertical border in the middle) with :vsp or C-W v.

  3. You can use your mouse cursor (in graphical / MacVim) to change focus (moving the blinking cursor) to another window. But you can also use your keyboard (of course).

Recall that vi/vim navigation usees the hjkl keys. Likewise, you can use those keys to navigate to windows above (k), below (j), left (h), right (l) of the current window, without cycling through when reaching the end. You can also use the arrow keys. Combine with the window command C-w, e.g. C-w j to go down 1 window.

  1. You can also teleport directly to any window by knowing which absolute number / index the window is. To go to window #2: C-w 2 C-w.

  2. Besides thinking which window you want to go to and which direction it is, you can quickly cycle through windows: C-w C-w

You may also want to just go back to the previous window: C-w C-p

  1. Mentioned above, in graphical or MacVim you can use the mouse to change window borders, thus changing window sizes. You can do it with the keyboard too. Increase the height of the current window by 5 lines: 5 C-w +. Decrease the height by 10 lines: 10 C-w -.

Change width using the window command and < or >. E.g. 20 C-w > increases window width by 20 columns.

You can also maximize (thus shrinking all other windows) the window with C-w _. Some people bind a key to both switch to a window and maximize it at the same time.

  1. Closing windows. Closing a window doesn't close the file (or buffer). Buffers are another subject altogether, which go together with windows. Quick note, you can view all buffers with :ls and use :b and type some text to switch to another buffer, which will change the contents of the current window to that buffer.

But to close a window, you can either :q (you should be familiar with this as a way to close vim) or C-w q.

So now you should be able to have a single Vim "window" or session with many vim-windows within it to get a custom view over a single long file or maybe a project of many source files.