How does VI work
Enjoying this content? Subscribe to the Channel!
Vi Editor for Beginners: Essential Commands to Create, Edit, and Save Files in Linux
Welcome to Darren’s Tech Tutorials! If you’re working in the Linux world, eventually you’re going to run into the legendary (and sometimes intimidating) Vi editor. Vi is a powerful, ubiquitous text editor found on virtually every Linux distribution—from CentOS 7 to Ubuntu.
While it can seem confusing at first because it uses ‘modes,’ mastering the basics is crucial for any serious Linux user.
In this tutorial, we are stripping away the complexity and focusing only on the four essential commands you need to create a file, add text, and successfully save your work. By the time you finish, you’ll be ready to take Vi for a test drive!
Why Learn Vi?
Unlike graphical editors, Vi is a terminal-based editor, making it incredibly lightweight and fast. It’s perfect for quick configuration file edits, system administration tasks, and scripting when you don’t have a full desktop environment available. It’s a tool that pays dividends the moment you truly understand its simple core logic.
Let’s dive into the practical steps!
Step 1: Opening or Creating a New File
The first step is always to invoke the Vi editor and specify the file you want to work on.
To create or open a file named text.txt, use the following command in your terminal:
vi text.txt
What Happens Next?
- If
text.txtalready exists: Vi will load the existing content into the buffer. - If
text.txtdoes not exist: Vi will open a blank screen, and the file will be created on the disk only when you save it for the first time.
Crucial Concept: Command Mode When Vi first opens, you are immediately placed into Command Mode. In this mode, anything you type is interpreted as an instruction (like moving the cursor, deleting lines, or saving the file), not text that will be inserted into the document.
Step 2: Entering Insert Mode (Adding Text)
Since you are initially in Command Mode, if you try to type the contents of your file, nothing will appear—or worse, you might accidentally trigger unwanted commands! To actually type text into your file, you must switch modes.
The key to starting your editing session is the Insert command.
Command to Enter Insert Mode:
Type the lowercase letter i
i
Once you type i, Vi switches to Insert Mode. You will often see the word -- INSERT -- displayed at the bottom of the screen. Now you can type, add new lines, and edit your document just like any other standard text editor.
Step 3: Exiting Insert Mode (Returning to Command Mode)
When you are done typing your text and are ready to save or execute any other editor functions, you must return to Command Mode. This is a critical step before saving!
Command to Exit Insert Mode:
Press the ESC key (Escape)
ESC
When you press ESC, the -- INSERT -- indicator at the bottom of the screen will disappear, confirming you are back in Command Mode. You are now ready to issue instructions to Vi.
Step 4: Saving Your File and Quitting Vi
Now that you are back in Command Mode (having pressed ESC), you can use the colon commands to manage the file and exit the editor.
These commands all start with a colon (:), which tells Vi you are about to issue a final instruction.
Option A: Save Changes and Quit
This is the most common command you will use. It tells Vi to Write (save) the file and then Quit the editor.
Command:
Type :, followed by wq!, and press Enter.
:wq!
(Note: The exclamation point ! is optional but generally good practice for ensuring the operation executes, especially when dealing with file permissions.)
Option B: Quit Without Saving
If you made some changes but realize you don’t want to keep them, you can exit Vi and discard the edits you made since the last save.
Command:
Type :, followed by q!, and press Enter.
:q!
(The ! is crucial here, as it forces Vi to quit and discard unsaved changes. Without the !, Vi will often warn you that the file has not been saved.)
Vi Basic Commands Summary
Here are the essential commands we covered, all you need to get started!
| Action | Command | Mode Requirement | Description |
|---|---|---|---|
| Open File | vi filename.txt |
Terminal | Opens Vi and prepares to create or edit the file. |
| Insert Text | i |
Command Mode | Switches to Insert Mode so you can type text. |
| Stop Typing | ESC |
Insert Mode | Returns you to Command Mode (required for saving). |
| Save & Quit | :wq! |
Command Mode | Writes (saves) the file and quits the editor. |
| Quit (No Save) | :q! |
Command Mode | Quits the editor and discards any unsaved changes. |
Get Started with Vi!
You’ve just learned the absolute backbone of using the Vi editor! While Vi has hundreds of advanced commands for navigation, deletion, and search-and-replace, these four foundational actions—open, insert (i), command (ESC), and save/quit (:wq!)—are the key to becoming functional in any Linux environment.
Ready to try it out? Open your terminal right now and create your first file!
If you found this beginner guide helpful, please hit the like button and subscribe to Darren’s Tech Tutorials for more clear, actionable guides on Linux, cybersecurity, and essential programming skills! Happy editing!