Skip to main content

Different methods to split the vim screen

Sanjeev Kumar Jaiswal's picture

Split the windows under vim effectively
When I wrote my first article on Basic commands for vim editor, then also I pointed out about it but with minimal points.
Here, I have tried to learn and share the splitting of vim screens using different methods. Thanks to internet and various resources that I came across while posting this simple article. Special thanks to StackOverflow :D
It will surely help when you are using on multiple files or you need to compare two files or you need to edit one file at two different place and wish to see both changes blah blah!

Here are the different methods that may help you to do so:
Method 1 (using command line in Linux)

I'm assuming you are using the command line. From vim --help, we got :

-o[N]                Open N windows (default: one for each file)
-O[N]                Like -o but split vertically
So type this to open files split horizontally, for example:
vim -o3 file1.txt file2.txt file3.txt
 
Method 2 (inside vim editor)
 
Ctrl-W s for horizontal splitting
Ctrl-W v for vertical splitting
Ctrl-w q to close one

Method 3 (inside vim editor)

  1. :sp filename (horizontal)
  2. :vsp filename (vertical)
Method 4 (inside vim editor)
  • vsplit and :vs splits the vim viewport
    :30vs splits the viewport and creates the current window to be 30 characters wide
  • For vertical screens: CTRL+W+ > or  CTRL+W +< (don’t type + while using it. It’s just to tell you that you need to press ctrl key, W (case-insensitive) and > altogether :D ) to make wider or narrower respectively.
  • And Ctrl+W+ = will make them equals
  • For  horizontal windows : Ctrl+W + increases the number of lines by one
  • :h for the list of all window commands (help command)
CTRL-W H           move current window to the far left
CTRL-W J           move current window to the very bottom
CTRL-W K           move current window to the very top
CTRL-W L           move current window to the far right

I hope it will help the developer in using vim screen effectively. Comments and critics are most welcome \m/
Anonymous (not verified)
Other way to move between split windows
CTRL-W CTRL-W     moves to next window (in sequence)
Howard Collier (not verified)
vim editor help article
Very COOL stuff on getting down and using the vim editor. Thanks for sharing this information through your Excellent article.
Shawn Dyer (not verified)
Random Comments
Observations:

Often, when I have multiple buffers open with not all of them showing, I find the command :sba useful (It splits the screen for all opened buffers, each in their own window). Also useful when I meant to type vim -o file1 file2 ... and forgot the -o.

Comments on Method 4: with Horizontal split windows, Ctrl-W _ makes the current Window the maximum height, leaving all the other Windows at height 1. With vertically split windows, the same can be done with Ctrl-W |

If you are wanting to diff files vim can diff up to 4 files with vimdiff file1 file2 ... or vim -d file1 file2 ...

You can also diff two open buffers by typing :diffthis in each window. I find that useful to diff two blocks of text I have copied and pasted from somewhere without ever saving them to temporary files.

I start vim, usually I type :set paste to avoid any autoindenting behavior I may have set up, go to insert mode and paste the first text. Then type :vnew to create a new vertically split buffer and paste the second block to be diffed in that buffer. Then type :diffthis in each buffer. That also works fine in gvim.

If you are editing a lot in files you have diffed, sometimes :diffupdate is useful.
sanjeev's picture
Offline
Joined: 21 Feb 2011
Thank you so much Shawn
It was really new to me also.
You have pointed some valid points which will enhance my vim skills.
Cheers

Follow us at :
Facebook | Twitter
########### Give me the right place to stand, I shall move the earth. #################

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.