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)
-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)
- :sp filename (horizontal)
- :vsp filename (vertical)
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 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/
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.