Methods of payment Abuse

What You Need to Know to Work in Vi

10.01.2025, 19:36

VI is a powerful console-based text editor. It is deservedly one of the most popular tools in Unix systems, thanks to its speed and extensive capabilities for working with text. However, it can be a bit challenging to master. That’s why we’ve compiled the essential commands for the Vi editor in this article.

How Vi Differs from Vim

Vim includes additional features such as syntax highlighting, mouse support, and much more. The basic commands and key combinations remain the same in both VI and Vim. Therefore, by learning Vi, you’ll automatically grasp the basics of Vim and its derivatives.

Why Should You Learn Vi?

Here are five reasons why I recommend learning Vi and Vim:

  1. Vi is always available as it is required by the POSIX standard.

  2. Vi/Vim is well-documented. The editor has its own user manual—just enter the :h command in command mode.

  3. Vi/Vim supports numerous plugins. Most of them can be found onvimawesome.com, one of the most popular places for plugin downloads.

  4. The editor consumes minimal resources. This makes Vi ideal for a variety of tasks. Software development is just one of them. For instance, Vi can handle extremely long texts, even novels, without issues. Other text editors, especially GUI-based ones, may crash under such loads.

Launching Vi

To start the program, enter the command:

vi

You can also open a file by specifying its name. If the file exists, it will open for editing; if not, a new file will be created:

vi your_file.txt

Modes in Vi

Vi operates in two different modes:

  • Command mode: Used for navigation, copying, and pasting.

  • Insert mode: Used for direct text input.

Using Normal Mode

💡 This mode is enabled by default when you open Vi/Vim.
It is used for actions like moving through text, copying, pasting, deleting, or replacing text (but not editing it). To return to this mode, press <Esc>.

1. Navigation commands:

  • h — move left.

  • j — move down.

  • k — move up.

  • l — move right.

2. Deletion commands:

  • x — delete the character under the cursor.

  • dd — delete the current line.

3. Copy and paste:

  • y — copy the selected text.

  • yy — copy the current line.

  • p — paste the text after the cursor.

Command Mode

💡 This mode is activated in normal mode by pressing :.

Examples of commands:

  • :wq — save changes and exit.

  • :q! — exit without saving.

  • :h — display help.

  • :/string — search for a string.

  • :%s/foo/bar/g — replace all occurrences of "foo" with "bar" in the document.

Insert Mode

💡 This mode is used for editing text.
Switch to it by pressing:

  • i — insert text before the cursor.

  • I — insert text at the beginning of the line.

  • a — append text after the cursor.

  • A — append text at the end of the line.

Visual Mode (Vim Only)

💡 This mode allows you to select text, which is particularly useful for working with large paragraphs.

  • v — select characters.

  • V — select lines.

  • Ctrl+V — block mode.

Conclusion

Vi/Vim is available on almost all Unix-like systems. By investing a little time to master it, you’ll gain access to one of the best text editors out there.