A key point that one should always remember when it comes to UNIX: Everything in UNIX (and Linux as well) is a file. That is an important fact. Your system configurations are controlled by a large set of regular text (ASCII) files. To change, modify, or tune part of the system configuration, you need to edit the relevant text file(s). To achieve this, you need first to know how to edit files. The most powerful (and the most difficult as well) text editor tool is the vi/vim editor. Learning how to use the vim editor will be the subject of this article. That is an important topic for any UNIX/Linux admin. Hopefully you’ll find this useful.
Installing vim Editor
By default, the vi editor is included in any UNIX/Linux installation. Unfortunately, in the minimal installation, what we have done together (See Article #2 – Installing Red Hat 6), the vim (vi improved) is not installed; it comes with the normal vi only. To install the improved vi (vim), the easiest way is to use the YUM tool. The YUM tool depends on what is called “Software Repository”.
Luckily, the Red Hat installation media contains a ready-to-use software repository. To use it, we need to mount the installation DVD first, and then configure YUM to use the DVD repository.
Note: Don’t be bothered by these terms (YUM, software repository, mount, etc.). You will know everything in time, when we talk about Software Package Management.
To install vim, simply follow these steps:
1. Insert the Red Hat installation DVD, and mount it using the mount command:
2. Copy the following commands and paste them in sequence into your shell.
echo '[dvd]' >> /etc/yum.repo.d/dvd.repo
echo 'baseurl=file:///mnt/Server' >> /etc/yum.repo.d/dvd.repo
echo 'enabled=1' >> /etc/yum.repo.d/dvd.repo
echo 'gpgcheck=0' >> /etc/yum.repo.d/dvd.repo
This will create a new file called /etc/yum.repo.d/dvd.repo, and insert the necessary configurations in it.
3. Enter the following command to install vim:
yum install –y vim
4. To check that vim has been installed successfully, the easiest way is to type vi in your shell, and press Tab twice. This should display all suggestions for commands that start with vi. Of those commands, you should see vim.
Using the vim Editor
The vim editor can be used to create and edit text files. To create a new file named data01.txt, type the following command in your shell:
You should get a screen like the following:
The vi/vim editor has three working modes:
- Command Mode: this is the default mode. When you open vim, you are initially put into this mode. In Command mode, you could move the cursor horizontally or vertically, delete characters one by one, delete complete words, delete (cut) entire line, delete (cut) multiple lines, copy line(s), paste previously cut or copied line(s), replace single or multiple characters, and change to one of the two other modes. From any mode, pressing Esc takes you back to the Command mode.
- Insert Mode: this is where you could insert text. There are several ways to enter this mode.
- Ex Mode: in this mode, a user can type short commands that save, discard, and quit vim.
In the following sections, we will cover each mode in details. Unlike, the order above, we are going to start by discussing the Insert mode first.
From the Command mode, use any of the following to enter the Insert mode:
- The Insert key.
This starts the Insert mode at the position of the cursor. (note the word — INSERT — at the left bottom corner).
- Press i
This has the same effect of pressing the Insert key.
- Press I
Pressing I has two effects: first, it transfers the cursor to the beginning of the line. Second, it starts the Insert mode at this point.
- Press A
Pressing A appends text at the end of the line.
- Press a
Pressing a appends text after the current position.
- Press O
When you press O, the current line is shifted down, and an empty line is inserted in its place. The cursor is transferred to the beginning of the empty line.
- Press o
Pressing o inserts an empty line below the current line, and moves the cursor to the beginning of the newly inserted line.
In this article, we have started discussing the vim Editor. Mastering vi is essential for any UNIX/Linux admin.
- There are two versions of vi: the normal vi that comes with any UNIX/Linux default installation, and the vim (vi improved).
- vim could easily be installed using the YUM package manager.
- vim can be used to create new files, or edit existing files.
- vim has three main working modes: Command mode, Insert mode, and Ex mode.
In the next article, we will continue with the vi modes, discussing the remaining two modes. See you then.