Introduction to The Linux vi Editor

What is Linux vi

vi is a powerful text editor that runs on Linux and UNIX systems, commonly used for editing configuration files, programming, and general text manipulation. It is known for its efficiency and speed once users are familiar with its commands.

vi is my editor of choice while I am connected to a web server via the Secure Shell (SSH). I am a PHP programmer and have a home lab that consists of using VirtualBox to create virtual machines. These virtual machines are web servers created for PHP development and testing. They are Linux, Apache, MySQL, and PHP (LAMP).

I use Ubuntu servers to create these LAMP web servers. Vi is one of the tools in my toolbox.

Why use vi?

Pre-installed: vi is usually available by default on almost all Linux distributions, making it convenient when working on servers or systems with minimal software installed.

Efficiency: vi allows advanced users to perform complex text editing tasks quickly and with precision.

Lightweight: It is a lightweight editor that consumes minimal system resources, which is ideal for working on remote servers or in environments with limited resources.

The best way to learn Vi is to create a new file and try it out for yourself.

1) VI Operates in Three Main Modes:

    • Normal Mode: This is the default mode. You can navigate and manipulate text using various commands.
    • Insert Mode: Add, Edit, Delete, and Copy text. Press the i key to activate insert mode. In insert mode, one can add, edit, delete, copy text, and move text around.
    • Command Mode: You enter this mode by pressing : (colon key) while in normal mode, to run commands like saving, quitting, or searching.

2) I Only Use a Subset of What is Available.

    • Type “vi <file-name>” on the command line to edit or create a file.
    • Pressing the “i” key switches us into Insert mode.
    • Pressing the “ESC” key switches us out of Insert mode into normal mode.
    • Pressing the “:” (colon) switches us into command mode.
    • dd command – Remove a line.
    • :q To quit when no changes have occurred.
    • :q! To not save changes.
    • :w Save and continue editing.
    • :wq To write (save) and quit (exit editing of file)
    • :set nu — Display line numbers.
    • :set nonu – Stop displaying line numbers.

3) Commands I Rarely or Never Use:

    • yy — Copy the current line. Referred to as yank(ed)
    • p — Paste the yanked line.
    • o — New line under the current line.
    • O — New line above the current line.
    • A — Append to the end of the line.
    • a — Append after the cursor’s current position.
    • I — Insert text at the beginning of the current line.
    • b — Go to the beginning of the word.
    • e — Go to the end of the word.
    • x — Delete a single character. (where the cursor is blinking)
    • dd — Delete the current line.
    • [number]dd — Delete X number of lines.
    • [number]yy — Yank X number of lines.
    • G — Go to the end of the file.
    • XG — Go to line X in a file.
    • gg — Go to the first line in a file.
    • h — Move left one character.
    • j — Move down one line.
    • k — Move up one line.
    • l — Move right one character.

Conclusion

Vi is the editor I like to use when connected to a remote server. I manage virtual web hosts on VirtualBox. Vi makes it easy to manage all the configurations a LAMP server contains.

In this article we learned that vi has Three Main Mode, is always available on Linux and Unix servers, and is easy to use once some of the features are understood.