A key design principle of Linux is that information and conﬁguration settings are commonly stored in text-based ﬁles. These ﬁles 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 ﬁles 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 conﬁgurable and efﬁcient 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 conﬁguration ﬁles 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 ﬁles 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 speciﬁed 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.
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 ﬁle 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.
[user@host ~]$ vim filename
Either way, the core features that we will discuss in this section will work with both commands.
NOTE: If vim-enhanced is installed, regular users will have a shell alias set so that if they run the vi command, they will automatically get the vim command instead. This does not apply to root and other users with UIDs below 200 (which are used by system services). If you are editing ﬁles as the root user and you expect vi to run in enhanced mode, this can be a surprise. Likewise, if vim-enhanced is installed and a regular user wants the simple vi for some reason, they might need to use \vi to override the alias temporarily. Advanced users can use vi –version and vim –version to compare the feature sets of the two 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.
When you ﬁrst 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 speciﬁc editing functionality:
- An i keystroke enters insert mode, where all text typed becomes ﬁle 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 ﬁle (to save it), and quitting the Vim editor.
The Minimum, Basic Vim Workﬂow
Vim has efﬁcient, 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 ﬁle 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 ﬁle and remains in command mode for more editing. The :wq command writes (saves) the ﬁle and quits Vim. The :q! command quits Vim, discarding all ﬁle 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 ﬁrst 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 ﬁles. 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.