Chapter 5: Splitting Windows and Tabs
First separate three concepts that are easy to confuse:
- a buffer is text loaded into memory for editing;
- a window is a viewport displaying a buffer;
- a tab page is a layout containing one or more windows.
One buffer can appear in two windows, and one tab page can hold several windows. Vim tabs are therefore not a “one tab, one file” model.
Splitting Windows and Tabs
You will often encounter situations where you need to edit the current document with reference to another document (scenario: translation), or copy code from another document to the current document (scenario: copying html element class names to a css document). copy code from another document to the current document (scenario: copying html element class names to a css document), this is when you need to split the screen the most.
Splitting
:splitabbreviation:sporCtrl-w ssplit up and down.:vsplitAbbreviation:vsorCtrl-w vSplit left and right.:diffsplitAbbreviation:diffsDiff mode opens a split screen and can be followed by {filename}.
Window jumping
Ctrl-w wActivate next window.Ctrl-w jActivates the next window.Ctrl-w kActivate the upper window.Ctrl-w hActivate left windowCtrl-w lactivates the right window
Move split screen
Ctrl-w LMoves to rightmost windowCtrl-w HMoves to the leftmost side.Ctrl-w KMove to the topCtrl-w JMove to the bottom
_Note: Case sensitive. Alternatively, you can move the bottom screen to the right side for a top/bottom to left/right split screen conversion. _
Screen zoom
Ctrl-w =Average window sizeCtrl-w +Increase heightCtrl-w -Reduce heightCtrl-w _Maximum heightCtrl-w >Increase widthCtrl-w <Reduce widthCtrl-w |Maximum width
Practice! Practice! Practice!
Run
:splitand scroll each window independently. They are two views of the same buffer: edit in one and the other updates too. Undo the edit withu, then close the current window withCtrl-w c.
Tagged pages
Tab pages are useful for keeping different window layouts: code and tests in one tab, documentation and logs in another. For switching files alone, buffers from Chapter 2 are usually more direct.
Create a tab
:tabnewor:tabeditabbreviation:tabeopens a new tab.Ctrl-w gfopens a new tab with the name of the file at the current cursor position
Note: Both :tabnew and :tabedit can be followed by a <space><filename> to open the specified file in a new tab. to open the specified file in a new tab, and : can be followed by a number indicating the position of the new tab in the list (starting from 0). _
Switch tabs
gtor:tabnextAbbreviation:tabnNext tab (last one loops to first)gTor:tabpreviousAbbreviation:tabpPrevious tab (first one loops to last):tabrewindabbreviation:tabror:tabfirstabbreviation:tabfirto the first one:tablastabbreviation:tablto the last tab
Close a tab.
:tabcloseAbbreviation:tabcto close the current tab:-tabcClose previous tab:+tabcClose the next tab:tabonlyabbreviation:taboclose other tabs
Run
:tabnew chapter01.md, then:vsplit chapter02.md. Usegtto switch tab pages andCtrl-w wto switch windows inside the current tab. Finally, run:closeand:tabcloseto return to this chapter.
The next chapter will cover block operations.