This article will cover some of the most important vim commands you should master to make the most out of this text editor. Priority is placed on simple, rather than complex, commands that are useful to use with this program. As the title suggests, this article includes 25 of the most important vim editor commands.
vim on Linux
As mentioned earlier, Vim is an improved version of the vi editor. The vi editor comes pre-installed with any Linux distro, as vi is part of UNIX. However, the vim editor does not come pre-installed, so you will have to install vim separately. Vim is a super popular editor and should be available directly from the package server(s) of your distro.
Below, I have listed the commands to install Vim for some of the popular Linux distros out there. If you do not see your distro down here, please do your own research to perform this step.
Debian/Ubuntu and derivatives:
Arch Linux and derivatives:
CentOS/RHEL and derivatives:
openSUSE and derivatives:
Important vim Commands
Vim offers a number of commands by default. Mastering these commands can guarantee a consistent Vim experience wherever you go. However, Vim also allows you to create your own commands. This article will discuss mostly the built-in vim commands.
First, launch Vim by running the following command:
If you have ever been in the vim editor, then you might be familiar with this meme. Anyone using Vim for the first time has probably seen this issue, including myself. Upon opening the vim text editor, many do not know how to exit the editor. It may sound stupid, but I believe this to be a common experience among Vim users. When you launch the editor, vim can show you how to quit the editor.
To exit the vim editor, run the following command:
However, the recommended way is to use the abbreviation of the quit command. This will save some time.
Say you want to discard the current changes and exit vim without saving any changes you made. Use the following command instead to quit vim and discard changes:
Navigating through the text is a necessary function. With vim, there are two ways you can navigate through the text you are editing: by using the arrow keys, or by using the hjkl keys.
- H – left
- L – right
- K – up
- J – down
All of these keys are located at the home row, so you can access these commands quickly and easily. It takes a little bit of practice to get used to this navigation system, though.
Vim is a modal editor, behaving differently in whatever mode you might be on. For most, the “Insert” mode should be familiar. In fact, most other text editors work in this manner.
To enable “Insert” mode, press i.
To return to normal mode, press Esc, and you will return to the “Normal” mode.
Need to delete something you just typed? Depending on the mode that Vim is in, there are two ways to delete characters.
If you are on “Normal” mode, press x to delete characters. The “x” button behaves like the “Delete” key in a normal text editor. If you require backspace-like behavior, then use X (uppercase).
If you are in “Insert” mode, using the “backspace” or “Delete” key will delete text.
Copy, Cut, and Paste
Copy/paste is one of the most common functions of a text editor. In the case of Vim, this function becomes a bit more complicated.
To select the text you want to copy or cut, move your cursor to the desired location and press v (lowercase). If you want to select the whole line, then press V (uppercase).
To copy the selected text, press y. To copy the entire line, press yy or Y (uppercase). To cut the selected text, press d.
To paste, press P (uppercase). If you want to paste after the cursor, press p (lowercase).
Saving a File
Once you have performed all the text edits that you require, it is now time to save the file.
To save the currently open file, enter the following command:
To delete the current line, move your cursor to the desired line and enter the following command:
Undo and Redo
If you have entered a typo or wish to undo an action, quickly press the undo/redo button(s). Chances are that you may need to use the undo function more frequently when using vim.
To perform an undo, press u.
To perform a redo, press “Ctrl + R”.
Moving to the Start/End of a Line
To bring your cursor back to the beginning of the current line, press ^. The “Home” key will also perform this function.
Search and Replace
Vim offers a robust search feature. To perform a basic search, use the following command structure:
The above command will search forward. To search backward, use the following command instead.
To go to the next search result, press n (lowercase). To go to the previous search result, press N (uppercase).
Replace in vim is also an interesting feature. To perform a replacement, you must first understand the command structure. To do so, enter the following command:
There are two flags you should focus on: g (replace the current line only) and gc (replace for all lines). If you use the flag gc, vim will ask whether you want to replace the selected text.
In some situations, you may have to work with multiple text files at once. In this case, you can split the vim screen into multiple sections.
There are two types of splits that vim offers: vertical and horizontal splits.
To perform a vertical split, run the following command:
To perform a horizontal split, run the following command:
You can also have multiple splits at the same time.
To move from one split-screen to another, you can use “Ctrl + W” (press twice to switch). If you want to manually navigate to a window, press “Ctrl + W + H/J/K/L”.
Vim offers many options for customization. You can customize almost any feature with the help of vimrc and plugins. You may find it strange, but vim has plenty of plugins, thanks to the awesome community surrounding this text editor program.
The first step of customizing vim is mastering the vimrc file, a configuration file that vim loads every time it starts. Vim uses this script to configure behavior, load plugins, and perform various actions. The vimrc file is written in vimscript language. Check out the beginner’s guide to vimrc here.
Vim is a powerful editor with infinite possibilities. While it is easy to learn certain features and commands of vim, mastering this text editor will take more effort and time. However, once mastered, you will definitely feel the difference using this program. Hopefully, this guide was able to help you in some way.
Vim also comes with vimtutor, a fun vim tutorial app to teach yourself vim.
Here are some interesting tutorials for you. Check out common vim shortcuts, vim syntax highlighting, vim plugins, and Vim Awesome, a major repo of vim plugins. If you are interested in mastering every single aspect of Vim (along with all its advanced features), I highly recommend checking these tutorials out.