Hello again. In the previous lesson, you learned to inspect files safely with tools such as less, head, and tail. Inspection comes first; editing comes only after you have confirmed the correct file and understood its current contents.
This final lesson in the command-line foundations module introduces Vim, the terminal editor found on virtually every enterprise Linux system in some form. By the end, you will be able to create a text file, make controlled changes, search within it, recover from basic mistakes, and save or discard your work deliberately.
Vim’s central idea: modes
Most graphical editors let you type as soon as they open. Vim behaves differently: it is a modal editor. A key’s meaning depends on the current mode.
Vim starts in normal mode. In normal mode, keys are editing instructions rather than text. For example, j moves down a line, x deletes a character, and dd deletes a line. To type ordinary text, you first enter insert mode. To save, quit, or issue an editor command, you use Vim’s command-line mode, entered with : from normal mode.
The practical cycle is:
- Open a file; Vim begins in normal mode.
- Enter insert mode when you need to type.
- Press
Escto return to normal mode. - Use a colon command to save, quit, or perform another editor action.
When uncertain about your current state, press Esc once or twice. It does not discard your work; it simply returns you to normal mode.

An introduction to the vi editor - Red Hat
Read Red Hat’s concise overview to establish the mode model and the small set of movement and editing commands used throughout this lesson. It discusses vi, the predecessor of Vim; the essential commands here work in both.
In the “Understanding vi modes” subsection, read the mode explanation. Notice why typing is impossible in the default mode. Then, in “vi editor navigation,” review the movement keys. Under “Entering and appending text,” read the insertion commands. Finish with the “Editing, deleting, and managing text” subsection, following the core editing operations.
A vocabulary note: some guides call Vim’s default state command mode, while others call it normal mode. In this lesson, normal mode means the default state for navigation and editing keystrokes. The state reached by pressing : will be called command-line mode.
Vim Essentials: Linux Editing Made Simple (Episode 1 - Introduction)
In “Vim Essentials: Linux Editing Made Simple,” Learn Linux TV demonstrates the complete beginner workflow: opening Vim, understanding modes, and saving a file. Watch it once for the visual behavior of the editor before practicing.
Watch launch and exit to see both an empty Vim session and a file opened by path. Then watch the mode model, focusing on the bottom-of-screen INSERT indicator and the use of Esc to return to normal mode. Finish with saving changes, paying attention to the difference between :w, :q, and :wq.
Open a safe practice file
For now, work only in a directory under your home directory. Do not practice on files in /etc, service files, or other system configuration. A single unwanted save to a system file can change system behavior.
First create a dedicated workspace at the shell prompt:
mkdir -p ~/vim-lab
cd ~/vim-lab
pwd
The final command should show a path ending in vim-lab. Check whether the full vim command is available:
command -v vim
If it prints a path such as /usr/bin/vim, use vim throughout this lesson. If it produces no output but the following command prints a path, you can use vi instead:
command -v vi
On RHEL-compatible systems, vi may be a minimal Vim-compatible editor. The commands practiced here remain applicable.
Open a file that does not yet exist:
vim notes.txt
At this moment, Vim has opened an empty buffer associated with the name notes.txt. The actual file is created only when you successfully write it to disk.
A useful distinction will prevent many beginner errors:
| What you enter | Where you enter it | Meaning |
|---|---|---|
vim notes.txt | Shell prompt | Start Vim and open or create a file |
i, o, dd, u | Vim normal mode | Single-key or short editing commands |
:w, :q, :wq | Vim command-line mode | Commands that start with : and finish with Enter |
| Ordinary text | Vim insert mode | Content that becomes part of the file |
Create the file: insert, escape, write
With the empty file open, press lowercase i. The lower part of the Vim screen should show -- INSERT -- or an equivalent insert-mode indicator.
Type the following three lines exactly:
# Basic Vim lab
host=lab01
environment=training
Press Enter after the first and second lines. When done, press Esc. The insert indicator disappears: you are back in normal mode.
Now save the file:
- Press
:. - Type
w, so the bottom line reads:w. - Press Enter.
The w means write. Vim should report that notes.txt was written. You remain inside the editor, which is useful when you want to save progress without stopping work.
Quit only after the write completes:
- Press
:. - Type
q. - Press Enter.
Back at the shell, verify that the file exists and contains what you intended:
cat ~/vim-lab/notes.txt
This is the inspect-then-edit discipline from the previous lesson in reverse: after editing, inspect the resulting file to confirm the saved content.
Put text in the right place
Reopen your lab file:
vim ~/vim-lab/notes.txt
Vim opens in normal mode. Arrow keys generally work, but learn a few normal-mode navigation keys as well. They work reliably over an SSH connection and avoid moving your hands away from the typing position.
| Normal-mode key | Result |
|---|---|
h, j, k, l | Move left, down, up, and right |
0 | Move to the first character of the current line |
$ | Move to the end of the current line |
w | Move forward by a word |
b | Move backward by a word |
gg | Go to the start of the file |
G | Go to the end of the file |
The essential insertion commands choose where typing begins:
| Key in normal mode | Result |
|---|---|
i | Insert before the cursor |
a | Append after the cursor |
I | Insert at the start of the current line |
A | Append at the end of the current line |
o | Open a new line below and enter insert mode |
O | Open a new line above and enter insert mode |
For an administrator editing a configuration file, A and o are particularly convenient. A adds a value to an existing line; o creates a separate directive immediately below the current line.
Try this controlled modification:
-
Use
Gto move to the final line. -
Press
A. -
Add this text at the end of the existing line:
-updatedThe complete line becomes
environment=training-updated. -
Press
Esc. -
Press
oto create a new line below it. -
Type:
owner=student -
Press
Esc. -
Save with
:w.
At this point, your file has four lines. Press :q to leave it, then inspect it with:
cat ~/vim-lab/notes.txt
Correct mistakes without panic
Vim is safest when you treat normal mode as the place for precise operations rather than holding Backspace until the text looks right. Start with these commands:
| Normal-mode command | Result |
|---|---|
x | Delete the character under the cursor |
dw | Delete from the cursor through the rest of a word |
dd | Delete the current line |
u | Undo the most recent change |
Ctrl+r | Redo a change that was undone |
yy | Copy, or “yank,” the current line |
p | Paste copied or deleted text below the current line |
To see why undo matters, position the cursor on the owner=student line and press dd. The line disappears. Press u immediately and it returns.
Deleting a line also stores it in Vim’s temporary register, so p can paste it below the current line. That makes dd followed by p a quick way to move a line downward one position. For your first few sessions, use u freely. It is usually better to undo promptly than to make several compensating edits that are difficult to review.
A small but important point: commands such as dd and dw work only in normal mode. If you accidentally type dd while -- INSERT -- is visible, Vim will insert the letters dd into the file. Press Esc, use u if necessary, and continue.
Find a setting before changing it
Configuration files often contain comments, repeated options, and settings far from the current cursor position. Searching is more reliable than scrolling.
From normal mode:
- Type
/environment. - Press Enter.
Vim searches forward for environment and places the cursor on a match. Press n to visit the next match, or N to go to the previous match.
For example, edit notes.txt again and search for host:
vim ~/vim-lab/notes.txt
Use /host, press Enter, then press A to move directly to the line’s end and enter insert mode. Add a descriptive comment:
# training system
The full line becomes:
host=lab01 # training system
Press Esc and use :w to save. Search first, then make the local change. This pattern will transfer directly to larger configuration files later in the course.
Save, quit, and discard with intent
The commands below are all entered from normal mode. Press Esc first if needed, then type the colon command and press Enter.
| Command | Meaning | Appropriate use |
|---|---|---|
:w | Write the current file and continue editing | Save progress and keep working |
:q | Quit | Leave after saving, or when no changes were made |
:wq | Write, then quit | Save the completed edit and return to the shell |
:q! | Quit and discard unsaved changes | Abandon an incorrect experiment deliberately |
The exclamation mark in :q! means “force.” It is valuable when you realize an edit is wrong, but it removes all unsaved changes in the current file. Do not use it as a response to every Vim message. First read the message: a failure to write might mean that you opened the wrong path, the directory does not exist, or you lack permission.
For system configuration later in the course, a failed save is a signal to stop and verify the file, ownership, permissions, and approved privilege method. Do not turn a practice habit into a risky one by casually forcing writes or editing protected files as root.
A short independent Vim routine
Use this sequence once without looking back at the command tables:
-
At the shell, open a new file:
vim ~/vim-lab/service-notes.txt -
Enter insert mode and create these lines:
service=httpd state=enabled port=80 -
Escape to normal mode and write the file with
:w. -
Search for
statewith/state. -
Use
Ato append# desired at bootto that line. -
Press
Esc, save and quit with:wq. -
Confirm the final result without opening Vim:
cat ~/vim-lab/service-notes.txt
This workflow is intentionally small, but it contains the core of routine configuration editing: open a known file, find the target setting, make one local change, save, and verify the resulting text.
Key takeaways
Vim becomes manageable once you keep its modes distinct:
- Vim starts in normal mode, where keys perform actions rather than insert text.
- Use
i,a,A,o, andOto enter insert mode at the right location. - Press
Escto return to normal mode before navigating, deleting, searching, saving, or quitting. - Use
uto undo,Ctrl+rto redo, and/textfollowed bynorNto search. - Use
:wto save,:qto quit,:wqto save and quit, and:q!only when you intentionally want to discard unsaved changes. - Practice in your home directory, verify saved files with read-only commands, and avoid experimenting on live system configuration.
Next, the course moves into filesystem operations: creating, copying, moving, renaming, and removing files and directories safely. Vim will remain useful whenever those operations require you to create a note, modify a configuration file, or document an administration decision.
Can't find a good explanation? Sign up and we'll make it for you
Sign up