VI Editor Basics in Linux
Overview and context
- In the DevOps and cloud world, you’ll work with Linux servers, often accessed remotely via a terminal, and you’ll modify content in many files, most of which are configuration files.
- Using simple redirection with cat is basic and has limited features; it is not suitable for adding or manipulating large amounts of text or for writing code.
- Mastering a console-based editor like VI is highly valuable for quickly editing files on remote servers.
Editors in Linux
- There are several text editors available in Linux, such as VI, VIM, Nano, etc.
- This course focuses on VI editor, which happens to be the most popular one.
- VI editor comes installed by default with most operating systems.
- To open a file, run the VI command and specify the filename:
vi <filename>. The terminal opens the file and you are now inside the VI editor.
Modes of VI editor
- VI has two modes of operation: command mode and insert mode.
- When you open a file in VI editor, you are by default in the command mode.
- In command mode, you can issue commands to the editor (copy/paste lines, delete a line or a word, navigate between lines, etc.), but you cannot write contents to the file.
- To write contents to the file, you must switch to the insert mode.
- To switch to the insert mode, type lowercase
i. - Once you are in insert mode, you may modify the file contents as you would normally.
- To switch back from insert mode to the command mode, press the Escape key.
Navigation and basic editing in command mode
- Move around the editor using the arrow keys or the keys
k, j, h, l on your keyboard:K to go uph to leftj to downl to the right
- To delete a character, press
x. - Typing
dd deletes the entire line. - To copy a line, type
yy. - To paste it, type
p. - To scroll the page up or down, press
Ctrl+u or Ctrl+d.
Saving, quitting, and file operations
- Typing colon (
:) takes you to the command prompt where you can type commands. - Save the changes made to the file and write the file to disk:
:w. You can optionally specify a file name as well, e.g., :w newname.txt. - Discard any unsaved changes and quit VI:
:q. - Save changes and quit:
:wq (or :x, which is similar in practice).
Searching and locating text
- To find a word, e.g., the word
off, type /off. - All occurrences of the word are highlighted, and the cursor is positioned at the first occurrence.
- To move to the next occurrence, press
n. - You can continue to press the
n key to move to subsequent occurrences.
Practical usage and workflow
- Default mode is command mode; to edit text, switch to insert mode with
i and return to command mode with Esc. - These capabilities are essential when editing files on remote Linux servers via SSH.
- Practical considerations:
- Be mindful of file permissions and ownership when editing system or configuration files.
- It’s prudent to make a backup before making large changes.
- Practice in non-critical files to gain familiarity with the commands.
- Quick hypothetical workflow:
- Open a config file with
vi /etc/example.conf. - Search for a parameter with
/parameter and navigate with n to the relevant line. - Enter insert mode with
i to modify the value. - Save with
:w and exit with :q once changes are validated.
Why modal editing matters and real-world relevance
- VI’s two-mode design separates navigation/editing actions, reducing accidental edits and enabling efficient editing, which is crucial when working across many files on remote servers.
- In practice, knowing how to quickly search, copy/paste lines, and manage simple edits can save significant time during system administration and DevOps tasks.
Final recap and tips
- Open:
vi <filename> — default mode is command mode. - Edit: switch to insert mode with
i; return with Esc. - Move: use arrow keys or
k (up), j (down), h (left), l (right); note the transcription uses K for up. - Edit commands:
x (delete char), dd (delete line), yy (copy line), p (paste). - Scroll:
Ctrl+u (up), Ctrl+d (down). - Save/quit:
:w, :q, :wq (optionally with a filename for :w). - Search:
/word; navigate with n. - Final takeaway: VI is a foundational tool for efficient remote editing, with practical implications for reliability, speed, and consistency in DevOps workflows.