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.
This will only start the editor. If you want to edit a text file with Vim, run the following command.
It can also include the path of the file.
Want to open multiple files? Use the following structure.
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.
If you made any change, then Vim won’t let you exit. Use the following command.
Editing file
Enter the editing mode by pressing “i”, basically toggling the “Insert mode”.
If you want to get out of the “Insert mode”, press Esc.
Here are some of the special ways of starting the “Insert mode”.
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.
Need the output of a command? Use this one.
Saving a file
The following command will write the buffer to the original file.
You can add it with the quit command.
If you want to append the buffer to an existing file, use the following one.
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,
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 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.
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”.
Searching
Vim offers a flexible and powerful way of searching. For example, the basic searches look something like this.
When you’re on search, you need to go from one match to another, right? Use the following keys.
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.
For example, to replace all the “the” occurrences, use the following one.
The following one asks for permission for each replacement.
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 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.
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.
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.
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!