vi Editor in Practical DevSecOps Labs: Modes, Saving, and Exiting
The vi editor is one of the most commonly available terminal-based text editors in Linux environments. Understanding its modes of operation is essential for editing files in the Practical DevSecOps labs.
Table of Contents
Opening a File
vi security-report.json # opens existing file or creates new one
vi foo.txt # creates a new file if it doesn't exist
Modes of Operation
vi has three modes of operation. Knowing which mode you're in is the most important skill for using this editor.
Command Mode (default)
When you open vi, you are in Command Mode by default. In this mode:
- You can navigate with arrow keys or
h/j/k/l - You can enter commands like
:q,:wq,:q! - Typing characters does not insert text
Insert Mode
Press i to enter Insert Mode. In this mode:
- You can type and edit text normally
- The bottom of the screen shows
-- INSERT --
Line Mode (ex mode)
Press : (colon) from Command Mode to enter Line Mode. The cursor moves to the bottom of the screen, where you can type commands like:
:wq— save and exit:q!— exit without saving:w— save without exiting
Essential Commands
Workflow: Edit a file
- Open the file:
vi foo.txt - Press
ito enter Insert Mode - Type your text
- Press
Escto return to Command Mode - Type
:wqand pressEnterto save and exit
Workflow: Read-only, no changes
- Open the file:
vi filename - Navigate with arrow keys (you're already in Command Mode)
- Type
:qand pressEnterto exit
Pasting text without Insert Mode
You can paste clipboard content directly into the file using:
- macOS:
Cmd + V - Windows:
Ctrl + Shift + V - Linux:
Ctrl + Shift + V(GNOME Terminal) orShift + Insert
Common Questions
| Question | Answer |
|---|---|
| How do I exit vi? | Press Esc, then type :q (quit) or :q! (quit without saving). |
| How do I save my changes? | Press Esc to return to Command Mode, then :wq. |
| I'm stuck in vi and can't type — what's wrong? | You're in Command Mode. Press i to enter Insert Mode. |
| How do I undo a change? | In Command Mode, press u to undo. Press Ctrl + R to redo. |
| What's the difference between vi and vim? | vim is an enhanced version of vi with additional features. Both use the same core commands. |
Wrap-Up
vi is a powerful but initially intimidating editor. Remember the three modes: Command (default, for navigation), Insert (for typing), and Line (:, for commands). Master i to insert, Esc to return to command mode, and :wq to save and exit, and you'll handle any file editing task in the labs.