How does VI work
Enjoying this content? Subscribe to the Channel!
vi Editor Basics: The Essential Linux Guide for Beginners (Create, Edit, Save, and Quit!)
Hi there! Darren here from Darren’s Tech Tutorials.
When you first start working in the Linux environment, especially with system administration tasks, you quickly run into the powerful but often intimidating vi editor. Many beginners struggle with it because it doesn’t work like standard Windows or macOS text editors—it uses specific modes for editing.
But here’s the secret: you only need a handful of commands to handle 90% of basic editing tasks, like tweaking config files or writing simple scripts.
In this guide, we are breaking down the basics of using vi (or its enhanced cousin, vim) on systems like CentOS 7. We’ll show you how to create a file, add text, and crucially, how to save your work and quit the program. Let’s get started!
Getting Started: Launching vi and Creating Your File
The vi editor is launched directly from your terminal. When you launch it, you specify the name of the file you want to edit. If the file doesn’t exist, vi will create it for you when you save your changes.
Command:
vi text.txt
In this example, we use text.txt. Once you hit Enter, you will be inside the vi editor window, ready to begin.
Step 1: Understanding vi Modes
The biggest hurdle for beginners is that when you first open vi, you are in Command Mode. In Command Mode, keystrokes are interpreted as instructions (like copy, paste, or delete) and not as actual characters to be typed.
To actually start typing text, you must enter Insert Mode.
Entering Insert Mode
To switch modes, simply press the letter i.
Command:
i
(Press the key ‘i’ once while in Command Mode.)
Once you press i, you are in Insert Mode, and you can type normally.
Pro Tip: In many modern
viminstallations, you will see the word-- INSERT --appear at the bottom of the screen, confirming you are ready to type.
Now you can type your content. For example:
Hi, my name is Darren. I am using vi.
Step 2: Saving Your Work and Quitting vi
Once you have finished adding or editing text, you need to save your changes and exit the editor. This is a three-part process that requires you to exit Insert Mode, enter the save command, and execute it.
1. Exit Insert Mode
Before issuing a command, you must return to Command Mode.
Command:
ESC
(Press the Escape key once.)
2. Enter the Save and Quit Command
Once you are in Command Mode, you signal that you are about to enter a command by pressing the colon (:). This moves you to the “Ex Command-line Mode” at the bottom of the screen.
The most important command you will learn is wq!:
| Command | Action | Description |
|---|---|---|
: |
Command Start | Signals the start of an execution command. |
w |
Write | Writes (saves) the content to the file. |
q |
Quit | Closes the editor. |
! |
Force | Forces the operation (helpful if permissions are tricky). |
Full Command:
:wq!
(Type this sequence after pressing ESC.)
After you type :wq!, hit Enter to execute the command. You should be returned immediately to your terminal prompt, and your file is saved!
Verification (Optional)
To verify that your file was created and saved correctly, you can use the standard Linux commands:
ls # Lists the files in the directory
cat text.txt # Displays the contents of the file
You should see: “Hi, my name is Darren. I am using vi.”
Step 3: Quitting Without Saving (The Emergency Exit)
What if you opened a file, made a mess of the configuration, and realized you absolutely do not want to save those disastrous changes? You need a way to exit the editor and abandon everything you’ve done since the file was last opened or saved.
This is done using the quit-only command.
The Abandon Command
- Press
ESCto ensure you are out of Insert Mode. - Press
:to start the command line. - Type
q!
Command:
:q!
(Type this sequence after pressing ESC.)
This command forces vi to quit (q) without writing or saving any of your recent edits (!).
Use Case: When I edited a file and decided I didn’t want to save my changes, I used
:q!to ensure the file remained exactly as it was before I opened it. When I checked the file again usingcat, the old content was still intact. Perfect!
Summary of Essential vi Commands
You can master the basics of vi just by memorizing these five essential keys and commands:
| Key/Command | Mode Required | Purpose |
|---|---|---|
vi filename |
Terminal | Opens or creates a file in vi. |
i |
Command Mode | Switches to Insert Mode (allows typing). |
ESC |
Insert Mode | Switches back to Command Mode (allows execution). |
:wq! |
Command Mode | Write/Save the file and Quit vi. |
:q! |
Command Mode | Quit vi without saving changes. |
Next Steps
See? That wasn’t so bad! While vi has hundreds of advanced functions, knowing how to open, insert text, and use :wq! and :q! is all you need to navigate configuration files and perform simple editing tasks on any Linux machine. This little bit of information will absolutely get you through managing editor and config files.
I encourage you to open up your terminal right now and give these commands a try!
If you have any issues or queries, be sure to pop them in the comments below, and if I can help, I certainly will!
Did this guide make vi less scary? Be sure to hit that Like button, subscribe to Darren’s Tech Tutorials for more clear and accessible guides, and tell us what Linux tool we should break down next! Thanks for watching!