Introduction to Vim

Basics

You installed Vim on one of your systems? Good. You may or can not install software on your system? No problem! There is an online demo of Vim. Now lets get started.

Open up a terminal and open Vim by typing vim. You should be greeted by a message in the middle of the terminal. If you already know which file to edit, you can specify it like this: vim path/to/my.file

The modes

Before you go ahead and can write, you should know that there are six modes in Vim. The most important ones will be explained in this section. There is the Normal mode, which is Vim is started in by default. This mode is used to issue commands. The next mode is Visual mode. It is a lot like Normal mode, but instead of moving around, you highlight sections of your document. Commands issued in this mode will be applied only to this section. The last mode is the Insert mode, in which you can edit the file.

The current mode is shown in the lower left corner, if it is nothing there, you are in the Normal mode. Most important in the beginning are the Normal mode and the Insert mode.

To enter the Normal mode from any mode, just press <ESC> twice. Pressing <ESC> once might work, but there are situations where it does not work. So to be on the save side, press it twice. From the Normal mode to the Insert you switch by simply pressing i or <INS>. To go to Visual mode instead, use v. Now you can select characters, if you want to select whole lines, you can use V instead.

Open, save, exit

Lets open a file for editing. To edit a file in Vim you can either specify the file name when starting Vim, as suggested above, or you can use the :edit command. All commands in Vim start with a colon (:) and are issued in Command mode. Enter the command mode by pressing <ESC> and write :e FILENAME. If the file does not exist, Vim will open an empty file for editing. Now go ahead, and edit the file in Insert mode.

When you are done, you want to save the file. For that you can use the :write command. If Vim does not know the file name of the file (because you forgot to specify one with the :e command or you just started Vim with the vim command), or you want to save the file into another location, you can just specify where you want to save like this: :write FILENAME.

Lets assume you are finished with editing the file, have saved your work, and just want to exit Vim. The command for exiting Vim is :quit. The :exit command can be used to exit Vim as well, it will save the changes before exiting. Exiting Vim in unintended ways (like killing the process or its terminal) will leave the swap file in place. Swap files are used to save the changes which are not saved yet. If a swap file exists and you edit the corresponding file, Vim will produce a warning.

Shorthands

If you think there basic commands are to long to be typed, you are absolutely right, and you should therefore memorize their shorthands as shown in table 1.

Table 1: Vim commands and their shorthands
Command Shorthand
:edit :e
:write :w
:quit :q
:exit :x

Use the force!

The exclamation mark (!) is used to force Vim to do certain actions. For instance :q! will exit Vim, even when there changes to the files which are not saved yet. In the save fashion :e! will open a file, and thereby closing the old one, discarding all changes which are not saved. Sometimes a file can not be opened for writing, but Vim can overwrite it. This is done with :w!.

The exclamation mark is quite useful in some situations, but it should be used with caution. It is recommended to try the command without the exclamation mark first, if Vim warns about something, read the warning. If you still want to do the action, you can try forcing it by adding an exclamation mark.