Bookmark this page

Basic vim Workflow

  • Both the cursor keys and hjkl can be used to move the cursor.

  • Escape exits the current command or mode, press twice to always end in command mode.

  • :w saves, :q quites, :wq saves and quits.

Objectives

After completing this section, students should be able to:

  • Open text files.

  • Move the cursor.

  • Insert and replace text.

  • Save files.

  • Get help.

Editor basics

No matter what editor you use, you should always be able to perform the following three tasks:

  • Open a new or existing file.

  • Make changes and/or insert new text.

  • Save the file and exit the editor.

Basic editing with vim

Opening files

The easiest way to open a file in vim is to specify it as an argument on the command line. For example, to open the file called /etc/hosts, you could execute the following command:

[root@serverX ~]# vim /etc/hosts

Note

If you try to open a file that does not exist, but the directory you specify is available, vim will inform you that you are editing a [New File], and create the file when you first save it.

After opening a file, vim will start in command mode. At the bottom left of the screen, you will see information about the opened file (filename, number of lines, number of characters). At the bottom right, you will see the current cursor position (line, character), and what part of the file is being displayed (All for all, Top for the first lines of a file, Bot for the bottom of a file, or a percentage to indicate where in the file you are). The bottom line is called the Ruler in vim terms.

Figure 3.1:

vim displaying a freshly opened file

Editing text

If you have ever used vi or vim before, you might have noticed that in command mode, most keys don't exactly do what you would expect. This is because in command mode, keys are not mapped to insert the characters you press, but rather to perform commands like cursor movements, copy-and-paste actions, and more.

To switch to insert mode, there are commands available to you, each assigned to a different key on your keyboard:

KeyResult
i Switch to insert mode, and start inserting before the current cursor position (insert).
a Switch to insert mode, and start inserting after the current cursor position (append).
I Move the cursor to the start of the current line and switch to insert mode.
A Move the cursor to the end of the current line and switch to insert mode.
R Switch to replace mode, starting at the character under your cursor. In replace mode, text is not inserted, but each character you enter replaces a character in the current document. (vim and vi also come with more powerful replacement commands; these are discussed in another section.)
o Open a new line below the current one, and switch to insert mode.
O Open a new line above the current one, and switch to insert mode.

Whenever you are in insert or replace mode, the ruler will display --INSERT-- or --REPLACE--. To return to command mode, you can press Esc.

The version of vi and vim that ships with Red Hat Enterprise Linux is configured to recognize and use the normal cursor keys, as well as keys like PgUp and End while in both insert and command mode. This is not the default behavior on all installations of vi. In fact, older versions of vi did not recognize cursor keys at all, and only allowed you to move the cursor from within command mode using keys like hjkl.

In the following table, you will find some of the keys you can use from command mode to move your cursor:

KeyResult
h Cursor left one position
l Cursor right one position
j Cursor down one line
k Cursor up one line
^ Move to the beginning of the current line.
$ Move to the end of the current line.
gg Move to the first line of the document.
G Move to the last line of the document.

Note

Pressing Esc will always cancel the current command, or return to command mode. It is an often-seen practice to press Esc twice (or even more) to ensure a return to command mode.

Saving files

Saving files in vim is done from ex mode. You can enter ex mode by pressing : (a colon) from within command mode. After entering ex mode, the ruler will display a colon (:) and wait for a command to be typed. Commands are completed by pressing Enter.

The following is a short list of commands to save and quit your current file from ex mode. This is by no means a full list of commands that can be used.

CommandResult
:wq Save and quit the current file.
:x Save the current file if there are unsaved changes, then quit.
:w Save the current file and remain in editor.
:w <filename> Save the current file under a different file name.
:q Quit the current file (only if there are no unsaved changes).
:q! Quit the current file, ignoring any unsaved changes.

A short summary of the previous table is that w saves (writes), q quits, and ! forces an action (do-what-I-say-not-what-I-want).

Getting help

vim comes with extensive online help, available in the editor itself. Typing :help from command mode will launch the first screen, which includes the help needed to navigate the help.

Help for a specific subject can be obtained by typing :help subject from command mode.

Help screens open in a new split window, and can be closed with :q. To learn more about split windows, use :help windows.

There is also a semi-interactive tutor available. Starting the command vimtutor from the command line will launch a guided tour of vim that takes a new user through the basics in about an hour.

References

vim(1) man page

vim built-in help

Revision: rh134-7-63a207e