Help Docs Server Administration Linux Server Administration Introduction to Vim

Introduction to Vim

Discover Vim, a powerful text editor. Learn about its modes (Normal, Insert, Visual, Command), installation, and tips for efficient editing.

Working with Vim: A Powerful Text Editor

Vim is a free and open-source, screen-based text editor program. It is described as an improved clone of the original vi editor created by Bill Joy. Bram Moolenaar, the author of Vim, based it on a port of the Stevie editor for Amiga and released a version to the public in 1991. Vim is designed to be used both from the command-line interface and as a standalone application with a graphical user interface.

This document aims to provide you with an understanding of Vim, covering installation, its different operating modes, and some helpful tips, presented in a professional yet friendly tone.

Installation

Unlike the original VI, Vim does not come installed by default on most Linux systems. Therefore, if Vim is not already on your server, you will need to install it.

You can install the enhanced version of Vim using the command:

yum install vim-enhanced

.

Interactive Tutorial and Cheat Sheet

If you wish to practice your VIM skills without risking damage to your machine or server an interactive tutorial is available. https://openvim.com/sandbox.html

Additionally, a helpful cheat sheet showing commands and their uses is available as a desktop wallpaper, which is described as very handy to have.

https://github.com/LevelbossMike/vim_shortcut_wallpaper#readme

Understanding Vim Modes

Vim operates using different modes, which is a fundamental aspect that differentiates it from more standard text editors. You will likely spend the majority of your time working in Normal mode.

  • Normal Mode
    • By default, Vim starts in Normal mode. You can return to Normal mode from any other mode by pressing Esc or <C-[> (Ctrl + [).
    • In Normal mode, pressing keys does not insert text directly into your document. Instead, specific key presses perform actions or commands.
    • Moving the Cursor: There are multiple ways to move around a file in Normal mode. While cursor keys work, you can also use h for left, j for down, k for up, and l for right. Using these keys can be particularly helpful for touch typists who prefer not to leave the home row.
    • Row movement commands can be prefixed with a number to move multiple lines at once. For example, 4j moves the cursor down 4 rows, and 6k moves it up 6 rows.
    • Basic word movements include w to move to the beginning of the next word, b to move to the previous beginning of a word, and e to move to the end of a word. Uppercase variants W, B, and E move by words separated by whitespace.
    • Movements to the beginning or end of a line include 0 to move to the beginning of the line and $ to move to the end of the line.
    • Making Changes: You can make changes to single characters in Normal mode. To replace a character, move the cursor over it, press r, and then type the new character. To delete a single character, move the cursor over it and press x.
    • Undo/Redo: To undo changes, press u in Normal mode. This will undo changes back to the last time you were in Normal mode. To redo (undo an undo), press Ctrl+r in Normal mode.
  • Insert Mode
    • This is the second most frequently used mode and will feel most familiar to many users, as typing characters directly inserts them into the text, similar to a standard text editor.
    • You enter Insert mode from Normal mode using an “insert command”. Common commands are i for ‘insert’, which immediately switches to insert mode, a for ‘append’, which moves the cursor one position after the current character and enters insert mode, and o which inserts a new line below the current line and places the cursor there in insert mode.
    • These commands also have uppercase variants: I moves the cursor to the beginning of the current line and enters insert mode, A moves the cursor to the end of the current line and enters insert mode, and O inserts a new line above the current one and places the cursor there in insert mode.
    • While there are many ways to insert text in Vim, these are described as the simplest. It’s important to note that Vim is not designed for constant use in Insert mode, so avoid staying in this mode for extended periods.
    • To exit Insert mode and return to Normal mode, press Esc or <C-[>.
  • Visual Mode
    • Visual mode is used for selecting blocks of text. This functionality is comparable to clicking and dragging with a mouse. Selecting text in Visual mode allows subsequent commands to apply specifically to that selection, which is useful for actions like copying, deleting, or replacing the selected text.
    • To make a text selection: Press v to enter visual mode, which also marks the starting point of your selection. Then, move the cursor to the desired end point. Vim will visually highlight the text between the start and end points.
    • Visual mode has different variants or subtypes:
      • v: enters standard visual mode, selecting based on character movement.
      • V: enters linewise-visual mode, which always selects entire lines.
      • (Ctrl+v): enters block-visual mode, allowing you to select rectangular regions of text.
    • To leave Visual mode and return to Normal mode, press Esc or <C-[>.
  • Command Mode
    • Command mode provides access to a wide range of commands that are less easily performed in Normal mode.
    • To enter Command mode, type : (colon) from Normal mode. The cursor will move to the bottom of the window where you can type your command.
    • A common example is performing a global find and replace: Type :%s/foo/bar/g.
      • : Enters command mode.
      • % Indicates the command should apply across all lines.
      • s Stands for substitute.
      • /foo/ Is the regular expression pattern for the text to find.
      • /bar/ Is the regular expression pattern for the text to replace with.
      • /g Specifies that the substitution should be global on each line, meaning it will replace all occurrences on a line, not just the first.
    • You can find information about many other Vim commands by typing :h or :help in Command mode to access the built-in help documentation.
  • Replace Mode
    • Replace mode allows you to overwrite existing text directly by typing new characters.
    • To enter this mode, first ensure you are in Normal mode. Position your cursor over the first character you want to replace.
    • Press R (capital R) to enter Replace mode.
    • Now, anything you type will replace the characters currently under the cursor. Like in Insert mode, the cursor moves forward as you type, but each character typed overwrites an existing one instead of inserting new text.

Helpful Vim Tips

Here are some practical tips for leveraging Vim’s capabilities:

  • Starting Vim On A Specific Line Number You can open a file and immediately go to a particular line number using the command structure: vim +lineNumber filePath For instance, vim +8 file.txt opens file.txt and places the cursor on line 8.
  • Setting Line Numbering Within Vim To display line numbers next to your text within the Vim editor window, enter Command mode by typing : and then type: :set number
  • Turn Off Line Numbering Within Vim If you wish to hide the line numbers, go into Command mode (:) and type: :set nonumber
  • Prepend Character To The Beginning Of Several Lines This is a very useful technique for tasks like commenting out multiple lines simultaneously, such as a vhost entry. It is described as significantly reducing the need for repeated cursor movements and typing characters like ‘#’.
    1. Navigate your cursor to the first character of either the first or the last line you intend to modify.
    2. Press [CTRL]+v to enter visual block mode.
    3. Move your cursor to highlight the desired lines. You will see a rectangular block selection appear.
    4. Press [SHIFT]+I (this is a capital ‘i’).
    5. Type the character(s) you wish to prepend, for example, a ‘#’ and optionally a space for neatness.
    6. Press [ESC]. Vim will then apply the characters you typed to the beginning of every line within your block selection. This demonstrates how Vim’s capabilities can significantly improve your workflow.
  • Edit Multiple Files Vim allows you to open and work with several files within a single session.
    1. Begin by opening the first file as you normally would, for example: vim /document/i/want/to/edit.txt.
    2. You can then open another file in a split window using either :split for a horizontal division or :vsplit for a vertical division of the window. For instance:
      • :split /path/to/document/i/want/to/edit.txt.bak
      • :vsplit /path/to/document/i/want/to/edit.txt.bak
    3. To switch between these split sub-windows, press [ctrl]+ww. This functionality makes it easy to perform actions across files, such as yanking (copying) text from one and pasting it into another, or comparing their contents.

Was this article helpful?