Vim Split Screen

29/12/2020
The controls of Vim circulate around keyboard and the keyboard only. This is especially useful when you’re dealing with code and tasks that require full focus. The “keyboard-only” nature allows more focus headroom for your code. Well, you’re free to interact using the mouse.

Say, you’re working with a file that requires keeping an eye on multiple parts of the file. For programming, that’s a common scenario. There are also other scenarios when a split view can be useful. Yes, let’s check out how to split view and edit on Vim.

Demo file

Let’s create a demo file. I grabbed the code of bubble sort from Geeksforgeeks.

Now, it’s time to split the file into two sections. Hit “Ctrl + w”, then press “v”. Take a close look at the cases; they’re all in lowercase.

Voila! Pretty simple, right? Let’s make another split!

This function is also available from the command.

:vsplit

There’s also a short form of this command.

:vsp

Awesome, right?

Let’s begin from the beginning. This time, we’re going to do horizontal splits. Run the following command.

:split

For short, you can use the following one.

:sp

This function is also available through keyboard key combo. For the horizontal split, press “Ctrl + w” then “s”. Again, all of them are in lowercase.

Moving window to window

Split view is extremely helpful for lots of scenarios, sure. However, without the ability from jumping one window to another, this split view is completely useless. Let’s see how we can jump from one split to another.

Here, I’ve set 4 split views of the same file.

As you can see, the cursor is currently on the first window. To switch to the right window, press “Ctrl + w”, then “l”.

To go to the left window, it’s “Ctrl + w”, then “h”.

If you did a horizontal split, then going up and down is necessary. For going up, press “Ctrl + w”, then “k”.

To go down, press “Ctrl + w”, then “j”.

Editing in split view

Splitting is quite simple, right? Now, it’s time to edit them. Each of the splits is actually a full-fledged Vim window, so all the editing keys will work.

Copy & paste

Similar to before, you can copy and paste from one window to another using default copy and paste hotkeys. Just navigate through a different window and do your task.

Changing split window size

Sometimes, the splitting is not beneficial because of the size. By default, Vim splits all the windows with similar width/height. It’s possible to minimize/maximize the splits. You can also tell Vim the size of the split.

For widening the current window to the max size, press “Ctrl + W”, then “|” (not lowercase L). Use this when you’re using a vertical split window.

If you’re willing to expand a horizontal split window, use “Ctrl + W”, then “_”.

If you want to reset the size of all split windows, use “Ctrl + W”, then “=”.

It’s also possible to tell the size of the split. By default, Vim offers equal width/height of each split. If you want to custom size the split screen, use the following structure.

:<width>vsp

For horizontal split, the similar structure applies.

:<height>sp

Opening multiple files in split windows

Until now, all the split windows were the copy of the same file, right? There are many situations when you have to work with multiple files. With split windows, it’s easy to overload your Vim workload.

If you didn’t know, here’s how to open a new file in Vim.

:e <path_to_file>/filename.extension

In this example, check out the vimrc file. Note that I’m opening vimrc in a completely new Vim instance.

:e ~/.vimrc

Now, let’s go back to the split screen example. Change the active window and open a new file.

Simple, right? You can also tell Vim to open a new file when you’re splitting. The command structure is quite similar, just add the file path.

:sp <file_path>

For vertical split, use a similar structure.

:vsp <file_path>

Vimrc tricks

Splitting is useful, no doubt. However, there are some vimrc tweaks that you can apply right now for making your life with split views much easier.

The following commands will ensure that whenever you split vertically, it’s going to appear on the right. Moreover, for a horizontal split, the new split is going to appear at the bottom. Naturally, that feels more comfortable rather than Vim’s default splitting strategy.

set splitbelow
set splitright

Remember the navigation? For navigating from one split to another, we had to use “Ctrl + w/W” + “h”, “l”, “k” or “j”. Everything is fine except the “W/w” interference in-between “Ctrl” and other keys. That doesn’t feel so natural. Of course, you can get used to it. However, here’s how I prefer to set them directly “Ctrl + h,l,k,j” format.

nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>

Final thoughts

Vim is definitely fun to use. It’s true that the learning curve of Vim is challenging but not so much. That makes Vim really attractive to me as I’m always learning new ways of manipulating the editor and extracting the maximum benefit out of this legend.

Still confused about splits? Why not just consult the Vim documentation? Run the following command.

:help split

Enjoy!

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Thuê mua proxy V6 uy tín, chất lượng tại đâu?

Thuê mua proxy V6 là nhu cầu tất yếu của người dùng internet nếu muốn tăng khả năng truy cập website,...
11/05/2024

Vim Install Plugins

Ever heard the name of Vim? I bet you did! It’s one hell of a text editor. In fact, Vi/Vim is one of the longest-living...
29/12/2020

Install TeamSpeak 3 Client 3.1.0 on Linux – Improved echo cancellation

TeamSpeak 3 is a secured voice enabled software that allows users to communicate between each other over the Internet or...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024