Bookmark this page

Chapter 3. Creating and Editing Text Files with vim

Abstract

Overview
Goal Introduce the vim text editor.
Objectives

  • Explain the three main modes of vim.

  • Open, edit, and save text files.

  • Use editor shortcuts.

Sections
  • The vim Text Editor (and Practice)

  • Basic vim Workflow (and Practice)

  • Editing with vim (and Practice)

Lab
  • Edit a system file with vim

The vim Text Editor

  • vim has three main modes.

    • Command mode for file navigation and simple commands.

    • Insert mode for normal text editing.

    • Ex mode for saving, quitting, and performing more complex commands.

Objectives

After completing this section, students should be able to:

  • Explain the three main modes of vim.

Introduction to vim

Editing text files is one of the most common tasks a system administrator will perform on a Linux system. As such, there is a wide variety of text editors available. One of the older, but most widely used, editors is vi. vi stands for Visual Interface, as it was one of the first text editors to actually display the working document while it was being edited. Before that, most editors were line-based (such as ed, and the still widely used ex). In regular use, vi and vim are normally referred to as v.i. (two letters) and vim.

Introduction to vim

VI IMproved

The version of vi that is shipped with Red Hat Enterprise Linux 7 is called vim. vim stands for VI IMproved, as vim comes with many features not found in the original vi, while still remaining (mostly) backward-compatible. Among the new features are popular options, such as syntax highlighting, completion modes, and spell-checking.

vim is highly extensible. It supports scripting in multiple languages, file-type plug-ins, different text-completion modes, and many other options. It can be adapted to almost any role, and has been. There are extensions and macros available on the Internet for almost any purpose, from helping to edit a certain type of file (such as DocBook), completion plus introspection for almost all programming languages in existence, to more mundane tasks such as managing ToDo lists.

Note

When an unprivileged user invokes the command vi on a Red Hat Enterprise Linux 7 machine, the command that is executed will be vim. This is done with an alias that is set from /etc/profile.d/vim.sh when the shell starts.

This alias is not set for users with a UID less than or equal to 200. These users will execute vi, which is vim in vi compatible mode. This means that any features not found in classic vi will be disabled.

It is recommended to always execute the vim command whenever the newer features are wanted, and to not rely on an alias that might not be available. This is recommended especially when users also regularly have to work as root.

Why learn vim?

Every system administrator will have a preference for a text editor. Some will prefer gedit, others like nano, and there even are people who prefer emacs. Even if one already has an editor of choice, it is important to be familiar with the basics of vim or vi for one simple reason: It's the editor that one can count on to be installed on whatever system is being worked on.

Different versions of vim

Three distinct variations of vim can be installed on an Red Hat Enterprise Linux system. Each version has its own use case, and variations can be installed side by side. The variations come in these three packages:

  • vim-minimal: This package only provides vi and related commands (like rvi, the restricted version that cannot spawn commands or a shell). This is the version included in a minimal installation of Red Hat Enterprise Linux 7.

  • vim-enhanced: This package provides the vim command (and friends), providing features such as syntax highlighting, file-type plug-ins, and spell checking.

  • vim-X11: This package provides gvim, a version of vim that runs in its own graphical window instead of in a terminal. One of the big features of gvim is the menu bar, useful when one is learning vim or can't remember a specific command. (Note: Depending on the terminal type and vim per-user configuration, it can be possible to use a mouse inside a regular vim session as well.)

A modal editor

vim is not the easiest editor to learn. This is partly because all commands in vim are geared toward speed and efficiency, and not ease of remembrance, and partly because vim is a modal editor. Modal editor means that the function of certain commands and key presses changes based on what mode is active.

vim has three primary modes:

FunctionMode
Command mode This mode is used for file navigation, cut and paste, and simple commands. Undo, redo, and others are also performed from this mode.
Insert mode This mode is used for normal text editing. Replace mode is a variation on insert mode that replaces text instead of inserting it.
Ex mode This mode is used to save, quit, and open files, as well as search & replace and other more complex operations. From this mode, it is possible to insert the output of programs into the current file, configure vim, and much more. Everything that is possible using ex can be done from this mode.

References

vim(1) man page

vim built-in help

Revision: rh134-7-c643331