Vim Shortcuts

29/12/2020
One of the main reasons why Vim is good is because everything Vim does circulate around the keyboard. Yes, you don’t need the help of mouse to use Vim. That’s excellent because whenever you’re using two devices together (mouse and keyboard), you need to invest more brainpower into operating them.

Of course, a number of applications, especially all the greatest gaming titles use mouse and keyboard simultaneously. It makes the games difficult and enjoyable. However, when you’re working with a text file like a program, you need to invest more brainpower and focus on your code and logic, not the devices.

Because Vim allows the user to solely focus on one single input device, it’s extremely popular in the pro community, especially programmers. As the title suggests, this guide will shed light on basic and advanced usage of various Vim shortcuts. We’ll also discover how to set your custom shortcuts and become the ultimate Vim champion!

Vim shortcuts

Here are all the popular Vim shortcuts you need to know for improving your experience.

Starting Vim

Fire up the terminal and run the following command.

vim

This will only start the editor. If you want to edit a text file with Vim, run the following command.

vim <filename>

It can also include the path of the file.

vim /path/to/file

Want to open multiple files? Use the following structure.

vim <file_1> <file_2><file_n>

Quitting Vim

At first, I would get stuck with Vim with no known way of getting out. Sometimes, it’s better to start editing the file from the beginning, right? Vim doesn’t quit in the traditional way. “Ctrl + C” doesn’t work but “Ctrl + Z” does!

Type the following command in Vim.

:q

If you made any change, then Vim won’t let you exit. Use the following command.

:q!

Editing file

Enter the editing mode by pressing “i”, basically toggling the “Insert mode”.

i

If you want to get out of the “Insert mode”, press Esc.

Here are some of the special ways of starting the “Insert mode”.

a – Insert text right after the cursor

A – Insert text at the end of the current line

o – New line below the cursor

O – New line above the cursor

Now, did you want to include data from some other source? For example, the output of a command or the content of another text file? Vim allows you to do that with these built-in shortcuts.

Use the following command to inject the content of other text files into the current position of the cursor.

:r <filename>

Need the output of a command? Use this one.

:r! <command>

Saving a file

The following command will write the buffer to the original file.

:w

You can add it with the quit command.

:wq

If you want to append the buffer to an existing file, use the following one.

:w >> /path/to/file

Navigation

When you’re working with a text file, it’s necessary to navigate to the needed place. For example, when you’re working with the visudo, there needs to be only a couple of things to edit at certain parts of the file.

For moving around, Vim allows the following hotkeys. Note that the arrow keys are not included. In addition,

h – Go one character to the left
l – Go one character to the right
j, Ctrl + J – Go one line down
k, Ctrl + P – Go one line up
0 – Go to the beginning of the line
$ – Go to the end of the line
w – Go to the next alphanumeric word
W – Go to the next word (delimited by space)
5w – Go forward 5 words
b – Go backward one alphanumeric word
B – Go back one word (delimited by space)
5b – Go back 5 words
G – End of the file
gg – Beginning of the file

Next up, we got the big jumping shortcuts. These are still navigation shortcuts but quite interesting for faster navigation throughout the file.

( – Go to the previous sentence
) – Go to next sentence
{ – Go to the previous paragraph
} – Go to the next paragraph
]] – Go to the next section
[[ – Go to the previous section

Copy & paste

This is another important function you MUST master for any text editor. We’re always copying and pasting things everywhere, no exception.

yy – Copy the current line
p – Paste after the current line
P – Paste before the current line

Undo & redo

It’s another vital feature you need to know. We’ve always been on the position when we just messed up for a couple steps and all we need to fix is just undoing a couple steps. The same goes for redoing. Unfortunate for you, Vim doesn’t handle those with traditional “Ctrl + Z” or “Ctrl + Y”.

u – Undo last operation

Ctrl + r – Redo the last undo

Searching

Vim offers a flexible and powerful way of searching. For example, the basic searches look something like this.

/<search_text >

?<search_text>

When you’re on search, you need to go from one match to another, right? Use the following keys.

n – Go to the next match
N – Go to the previous match

Replacing content

Sometimes, you might need to change certain parts with similar pattern to a different one. For example, changing the name of a variable (when you’re programming) all over the file. In such scenarios, the replace feature comes really handy. It’s simple but just enough complicated to perfectly do the job.

:<range>/<search_pattern>/<replace>/g

For example, to replace all the “the” occurrences, use the following one.

:%s/the/the-replaced/g

The following one asks for permission for each replacement.

:%s/the /the-replaced/gc

Visual mode

By default, Vim doesn’t allow any interaction with the mouse. However, there’s the “visual” mode that allows an easy way for selecting a piece of text. In fact, this is the ONLY way Vim allows selecting the texts without any keyboard shortcut.

Note: This feature is available to Vim, not Vi.

For entering “visual” mode, use the following hotkeys.

v – Enter “visual” mode per character
V – Enter “visual” mode per line

Just like the “insert” mode, if you want to get out, just press Esc.

Custom shortcuts

This is my favorite part. You can set your custom keyboard shortcuts and commands for performing certain actions. You can bind various actions to simple key combos for faster access to those functions. Personally, I’d suggest binding only the actions that you use very frequently.

For the custom key combos, Vim uses the vimrc file.

The structure looks something like this.

<map_command> <map_argument> {lhs} {rhs}

Let’s explore what these mean.

  • <map_command> – Defines whether you’re adding/removing/listing a map, whether the mapping will be recursive/non-recursive, and in which “mode” it’ll be applied.
  • <map_argument> – It’s optional. It allows combining one or more arguments in combo with your custom mapping.
  • {lhs} – Define the shortcut or key(s) you’re going to use.
  • {rhs} – Define the shortcut/command that’s going to be replaced/executed when the {lhs} keys are pressed.

In this example, I’ll be binding “:nohlsearch” command with the spacebar. For your reminder, “:nohlsearch” is used whenever you want to remove highlight for the previous search result.

nnoremap ,<space> :nohlsearch<CR>

This tells Vim to type the “:nohlsearch” and finish with Enter (<CR>) every time you press <space> in the “normal” mode. The command won’t be echoed in the command line.

For the in-depth documentation of Vim’s custom mapping, run the following command in Vim.

:help mapping

Final thoughts

Depending on your mastery, Vim can be the most efficient and fastest way of your way around your work. I believe that by mastering these shortcuts, you can easily make Vim more productive than any other text editors out there. Maybe even flex your skills in front of colleagues/friends?

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

Lý do tại sao bạn nên sử dụng nhiều proxy đối với nhu cầu của mình.

Khi sử dụng proxy để lướt web, bạn có thể trải nghiệm nhiều lợi ích hữu ích như bảo vệ thông...
27/02/2023

VIM Spell Check

Spell checkers are nothing more than a software that keeps on checking the spelling of whatever word you’re typing against...
29/12/2020

How to Use CentOS Unzip

Zip is a file compression utility. It is the default file compression utility on Windows and widely used in Windows operating...
29/12/2020
Bài Viết

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

Thuê mua proxy Facebook chất lượng, GIÁ TỐT NHẤT
09/05/2024

Dịch vụ thuê mua proxy Zalo giá rẻ, tốc độ cao
09/05/2024

SỰ KHÁC BIỆT GIỮA RESIDENTIAL PROXY VÀ PROXY DATACENTER
17/02/2024

Mua Proxy v6 US Private chạy PRE, Face, Insta, Gmail
07/01/2024

Mua shadowsocks và hướng dẫn sữ dụng trên window
05/01/2024