How do I split the Vim window to view two files at once?
Answer
:split
Explanation
The :split command (or <C-w>s) splits the current window horizontally, creating a new window above with the same file. Use :split filename to open a different file in the new split.
How it works
:splitor<C-w>screates a horizontal split (one window above, one below):vsplitor<C-w>vcreates a vertical split (side by side):split filenameopens a specific file in the new split
Navigating between splits
<C-w>wcycles between windows<C-w>h/j/k/lmoves to the left/below/above/right window<C-w>ocloses all windows except the current one
Example
:split header.h " open header.h in a horizontal split
:vsplit main.c " open main.c in a vertical split
Tips
- Resize horizontally with
<C-w>+and<C-w>- - Resize vertically with
<C-w>>and<C-w>< <C-w>=makes all splits equal size:onlycloses all splits except the current one