Beginners Guide to Editing Files in Linux with VIM

A key design principle of Linux is that information and configuration settings are commonly stored in text-based files. These files can be structured in various ways, as lists of settings, in INI-like formats, as structured XML or YAML, and so on. However, the advantage of text files is that they can be viewed and edited using any simple text editor.

Vim is an improved version of the vi editor distributed with Linux and UNIX systems. Vim is highly configurable and efficient for practiced users, including such features as split-screen editing, color formatting, and highlighting for editing text.

Why Learn Vim?

You should know how to use at least one text editor that can be used from a text-only shell prompt. If you do, you can edit text-based configuration files from a terminal window, or from remote logins through ssh or the Web Console. Then you do not need access to a graphical desktop in order to edit files on a server, and in fact, that server might not need to run a graphical desktop environment at all.

But then, why learn Vim instead of other possible options? The key reason is that Vim is almost always installed on a server if any text editor is present. This is because vi was specified by the POSIX standard that Linux and many other UNIX-like operating systems comply within large part.

In addition, Vim is often used as the vi implementation on other common operating systems or distributions. For example, macOS currently includes a lightweight installation of Vim by default. So Vim skills learned for Linux might also help you get things done elsewhere.

Starting Vim

Vim may be installed in Red Hat Enterprise Linux in two different ways. This can affect the features and Vim commands available to you. Your server might only have the vim-minimal package installed. This is a very lightweight installation that includes only the core feature set and the basic vi command. In this case, you can open a file for editing with vi filename, and all the core features discussed in this section will be available to you.

Alternatively, your server might have the vim-enhanced package installed. This provides a much more comprehensive set of features, an on-line help system, and a tutorial program. In order to start Vim in this enhanced mode, you use the vim command.

[[email protected] ~]$ vim filename

Either way, the core features that we will discuss in this section will work with both commands.

Vim Operating Modes

An unusual characteristic of Vim is that it has several modes of operation, including command mode, extended command mode, edit mode, and visual mode. Depending on the mode, you may be issuing commands, editing text, or working with blocks of text. As a new Vim user, you should always be aware of your current mode as keystrokes have different effects in different modes.

vim operating modes

When you first open Vim, it starts in command mode, which is used for navigation, cut and paste, and other text manipulation. Enter each of the other modes with single character keystrokes to access specific editing functionality:

  • An i keystroke enters insert mode, where all text typed becomes file content. Pressing Esc returns to command mode.
  • A v keystroke enters visual mode, where multiple characters may be selected for text manipulation. Use Shift+V for multiline and Ctrl+V for block selection. The same keystroke used to enter visual mode (v, Shift+V or Ctrl+V) is used to exit.
  • The : keystroke begins extended command mode for tasks such as writing the file (to save it), and quitting the Vim editor.

The Minimum, Basic Vim Workflow

Vim has efficient, coordinated keystrokes for advanced editing tasks. Although considered useful with practice, Vim’s capabilities can overwhelm new users. The i key puts Vim into insert mode. All text entered after this is treated as file contents until you exit insert mode. The Esc key exits insert mode and returns Vim to command mode. The u key will undo the most recent edit. Press the x key to delete a single character. The :w command writes (saves) the file and remains in command mode for more editing. The :wq command writes (saves) the file and quits Vim. The :q! command quits Vim, discarding all file changes since the last write. The Vim user must learn these commands to accomplish any editing task.

Rearranging Existing Text

In Vim, copy and paste is known as yank and put, using command characters Y and P. Begin by positioning the cursor on the first character to be selected, and then enter visual mode. Use the arrow keys to expand the visual selection. When ready, press y to yank the selection into memory. Position the cursor at the new location, and then press p to put the selection at the cursor.

Visual Mode in Vim

Visual mode is a great way to highlight and manipulate text. There are three keystrokes:

  • Character mode: v
  • Line mode: Shift+V
  • Block mode: Ctrl+V

Character mode highlights sentences in a block of text. The word VISUAL will appear at the bottom of the screen. Press v to enter visual character mode. Shift+V enters line mode. VISUAL LINE will appear at the bottom of the screen. Visual block mode is perfect for manipulating data files. From the cursor, press the Ctrl+V to enter visual block. VISUAL BLOCK will appear at the bottom of the screen. Use the arrow keys to highlight the section to change.