Menu
How to Use the VIM Text Editor in Linux by Richard M. Stallman

Using a free version of vi is not a sin; it is a penance. So happy hacking.

Vim (vi iMproved) - one of the iconic text editors of UNIX systems of worldwide request. The original version was written in 1976 by Bill Joy, the cofounder of Sun Microsystems. Later, in 1988, the improved version, the so called vim, was released by Bram Moolenaar. The subject this article is concerned with is interesting for both beginners and experienced Linux users if they aspire to work intensively with the console to feel its flexibility and power. Here we provide the examples that will illustrate some basic operations with text as well as its extra features that make it unique and useful.

Vim can seem odd and unusual at first sight, however, its flexibility and omnitude make it the most popular editor for Linux from year to year. It is also ported to other platforms including Windows being able to work in a graphic mode (gVim program).

There're some facts which make knowledge of vim essential while working with Linux. Firstly, graphic mode is not always available, say, during work with the remote server over SSH or in case of system fatal failure. Though, as a rule, depending on operation system configuration, vim or another text editor (nano) remain accessible. Secondly, more or less constant work with Linux makes it clear that operations are easier and faster performed via console, so there's a need in a powerful text editor to be used without exiting console itself. Here comes vim and vim-based programs (for example, vimdiff - a utility for line-by-line text comparison on the basis of vim and diff utility). If vim seems to have lacking function, you'll for sure find it in one of the numerous vim plugins.

To start work, it is necessary to insert the command in terminal:

expert1@AE ~ $ vim

This will lead to the creation of new text document or create⁄open one already existing:

expert1@AE ~ $ vim myToDoList.txt

Create a new document or open an existing one

Vim has several modes, the most utilized of which are editing and command (it opens files on default, see the picture above). The first one, as states its name, edits the text of the file, while the second gives commands to the editor: save, exit, find the word, etc. To navigate, it is possible to use the cursor pad: Home⁄End and Page Up⁄Page Down. Apart from this, double click on g lowercase will move the cursor to the file's beginning (in other words, it is the gg command), while double click on Shift+g (GG command) will move the cursor to the end.

Vim has proper in-line documentation. To get help, you need to enter

:help COMMAND

Where COMMAND is the command you need (For instance, gg) or the subject (for example, encryption), and press Enter. To exit help, there's a command :q.

Starting work, let us turn to the editing mode by pressing i and enter something:

Editing mode

Now, let us go back from the edit mode to the command mode (press Esc) and save the file. After :w command, write the name of the file, if it hasn't been saved previously.

Save the file

Insert it and press Enter:

Command mode

Command mode allows to quickly delete and copy lines or group of lines. Let's delete the 4 th issue from our plan for tomorrow. Put the cursor on the corresponding line and insert dd command. Consider that commands without colon are not presented in the closing line of the file. If you change your mind and no longer want to insert several symbols command, it can be cancelled by Esc. Same command can be a bit more difficult. For example, we need to delete 2 lines at a time, starting from the one at which the cursor is placed. Insert the command d2d, where the figure refers to the number of lines to be deleted.

Command u can cancel your last actions as many times as you need. If you don't like one of your cancelling operations, the action can be renewed by pressing Ctrl+r. Why not simply r? After pressing lowercase r, vim is waiting for you to change the symbol under the cursor. Del key deletes the existing symbol as usual (similarly to its functional twin - x-key).

Command yy copies the current line to the clipboard, and its yNy variant copies several lines (in this case, syntax is analogous to "delete" command. You can paste the copied lines with the command p.

While finishing editing of the file, you can face thing which brings vim's beginners to the deadlock: how to exit. There're several possible ways. The simplest option is to close program with :q command (same as when exiting help mode). Vim enables you to open several files at a time. To close all of them, insert :qa. ZZ or :x stands for closing and saving. Close without saving can be performed by :q! or :qa!.

If being a paranoiac you're afraid that somebody can reveal your plans, code your file with the command :X.

Vim will suggest inserting a secret key twice,

Inserting a secret key

and later on it will require authentication:

Entering secret key

If the key is incorrect, you'll see abracadabra.

Change the quality of protection

By default, Pkzip is used here as one of the simplest methods of encryption. However, you can change the quality of protection by entering

:setlocal cm=blowfish2

Finally, let us consider the work with several files. They can be opened simultaneously while starting text editor.

expert1@AE ~ $ vim -p file1.txt file2.txt

or with the command in vim

: tabedit pathToFile

Work with several files

In the latter case, we'll open the file with settings for hard disk mounting.

Open file with settings for hard disk mounting

The work with multiple files is same as work with the one. You can change the tabs by command :tabn (next tab) and :tabp (previous tab). Command :tabclose will close the current tab.


Article's author: John Klein PhD in Engineering Sciences, expert in physics and programming at AssignmentExpert.com


Learn more at amazon.com

More Windows Administration Information:
• Understanding Linux Filesystems: Ext4 and Beyond
• How to Load or Unload a Linux kernel Module
• How Linux Works
• 12 Essential Linux Commands for Beginners
• Connecting Linux to the Internet
• An introduction to the Linux Boot and Startup Processes
• Set Up a Minimal Server on a Raspberry Pi
• Easy Way to Install Linux
• The Death of Windows
• Migrating to Linux: Installing Software