One of the most powerful text editors accessible from the command line is the vim editor. Built on the foundation of "vi", an editor dating back to 1976, vim adds additional functionality and power, while maintaining the editing style of its predecessor.
This guide will cover the installation and basic usage of vim.
Due to vim's wide-spread use on the Linux command line, it is available in almost every distribution's default repositories.
On Ubuntu and Debian, use apt-get to install:
sudo apt-get install vim
On Fedora and CentOS, install with yum:
sudo yum install vim
On Arch Linux, vim can be install through packman:
sudo pacman -S vim
Vim should now be installed correctly.
By default, when vim is called without any arguments, it opens with a blank document. Depending on your distribution, there may be an introduction and licensing message. This will disappear when you begin using the editor.
However, we are not going to open vim with a blank document. We will create a sample document called "newfile" to showcase vim's features.
echo "This is a new file. Here is the second line And here is the third line. Some more text is on this line. We will edit this file in vim. It will be great fun." >> newfile
Now, we will open the file we just created with vim.
The main difference between vim and most other editors is that vim is a "modal" editor. Most other editors have only one mode. In these editors, special editing functions, like copying text, are performed by holding one or more modifier keys and then hitting a regular key. Vim uses distinct modes to separate these functions from the task of normal text input.
Used for editing operations. Copying, pasting, moving, deleting, and changing text is accomplished from within this mode.
In vim, editing functions are performed by putting vim into "normal" mode. Normal mode is the mode that vim is in upon opening the program. This mode is used to navigate a text document quickly and to perform editing. It is not used for entering text.
Used for inserting new text in the document. It is possible to enter insert mode in a number of ways.
To enter text, vim must transition into "insert" mode. Insert mode is analogous to the typing interface of most other text entry programs. What you type appears on the screen in the document. All normal keys will produce the corresponding character at the current cursor position.
Used for visual selection. Many commands available in normal mode can be applied to a specific highlighted portion of the text.
A third mode that vim uses is "visual" mode. This is used for visual selection and manipulation of text. Areas of text are highlighted as a target of subsequent editing or formatting commands.
Used for issuing vim commands. Enter this mode with the colon key.
An additional mode that is used for complex edits, changing settings, and controlling vim itself is "command" mode. This mode is used for saving documents, quitting the program, performing complex searches and many other things.
It is always possible to navigate text with the arrow keys, but vim provides faster ways of moving around a document. In normal mode, you can use the h,j,k, and l keys to move left, down, up, and right, respectively.
These direction keys may seem confusing and counterintuitive at first, but they were chosen for a reason. They are in the home-row of a typical keyboard. This means that a user's hand moves from the resting position significantly less than with the traditional arrow keys.
There are also other navigation shortcuts. Here are a few of the most useful ones:
Editing text in vim is accomplished by issuing commands in normal mode.
It is important to realize that editing commands in vim are very powerful when combined with motion commands. Anything from the navigation section can be used as a direction. For instance, you can perform an editing command on a word following it with "w".
Here are a few of the different actions you can choose from:
As mentioned earlier, vim's power comes from its ability to chain different commands together. The easiest way to grasp this concept is to consider vim commands a language. Keys in normal mode can represent adjectives, verbs, and objects.
For instance, to yank (copy) 4 words, simply translate that phrase into commands that vim recognizes. In this case, it would be:
To delete from the current line until the end of the file, type:
It's also helpful to realize some of the conventions that vim uses for its editing commands. For instance, a doubled letter is usually applied to the entire line. We can copy an entire line with:
On the other hand, a capitalized version of an editing command often targets from the current cursor position to the end of the line. For instance, to change the text from here until the end of the line, you would use:
To do the same motion on more than one line, just add a numbered prefix to the command. This changes the text from the current position to the end of the following line:
There are many other powerful techniques to use with vim that are outside of the scope of this guide. If you would like to learn more, a good start is with vim's built in tutorial interface. It will not cover advanced topics, but it will give you a good idea of how to work with vim to do basic editing. You can start it with this:
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.Sign up