Introduction / Why This Matters
Vim is a powerful, console-based text editor that comes preinstalled on virtually every Linux system. Learning it is a critically important skill for system administrators, developers, and anyone who works with servers via SSH. Even if you prefer graphical editors, knowing Vim saves the day when a graphical interface is unavailable. This guide will give you a solid foundation: you'll stop fearing Vim and will be able to comfortably edit configs, scripts, and text files right in the terminal.
Requirements / Preparation
- System: Any Linux distribution (Ubuntu, Debian, CentOS, Fedora, etc.).
- Access: Permissions to run the terminal and edit target files (
sudomay be required for system configs). - Package: Vim is usually already installed. If not, install it:
- Debian/Ubuntu:
sudo apt update && sudo apt install vim - RHEL/CentOS/Fedora:
sudo yum install vimorsudo dnf install vim
- Debian/Ubuntu:
Step-by-Step Guide
Step 1: Launching Vim and Understanding the Interface
Open your terminal (Ctrl+Alt+T in most distributions). To edit a specific file, run:
vim /path/to/file.conf
To create a new file:
vim new_file.txt
After launching, you will see:
- The title line (at the top) — file information.
- The main area — the file's content (or empty space).
- The status line (at the bottom) — where messages and commands appear.
Key point: Right after launch, you are in Normal mode. In this mode, every key is a command for navigation, deletion, copying, etc. You cannot just start typing text. You need to switch modes first.
Step 2: Basic Cursor Movement (Normal Mode)
Practice these commands while in Normal mode. Do not press other keys until you've mastered these.
| Command | Action |
|---|---|
h | Left |
j | Down |
k | Up |
l | Right |
These four keys are the absolute basics. Remember them: h — left, j — down (like the down arrow), k — up, l — right.
Fast movements:
w— to the beginning of the next word.b— to the beginning of the previous word.0(zero) — to the very start of the line.$— to the very end of the line.gg— to the very start of the file.G(Shift+g) — to the very end of the file.
💡 Tip: If you press a key and something goes wrong — just press Esc to cancel any incomplete command and return to a clean Normal mode.
Step 3: Insert and Edit Modes
To start typing or editing text, you need to switch to Insert mode.
| Command | When to Use | Cursor Will Move To |
|---|---|---|
i | Insert | Before the current character |
a | Append | After the current character |
o | Open | In a new line below the current one |
O (Shift+o) | In a new line above the current one |
Example:
- While in Normal mode, place the cursor on the letter
ein the wordtext. - Press
i. The status line at the bottom will show-- INSERT --. - Start typing. Text will be inserted before the
echaracter. - Press
Esc. You'll return to Normal mode, and you can control the editor again.
Step 4: Deletion, Replacement, and Undo
All these actions are performed in Normal mode.
- Deletion:
x— delete the character under the cursor (likeDelete).dd— delete the entire line the cursor is on.dw— delete the word starting from the cursor.d$— delete from the cursor to the end of the line.d0— delete from the cursor to the start of the line.
- Replacement:
r+character— replace the single character under the cursor with the specified one (e.g.,rxreplaces it withx). You will remain in Normal mode afterward.
- Undo and Redo:
u— undo the last change.Ctrl + r— redo (reverse an undo).
Step 5: Saving and Exiting
Always check which mode you are in! To save and exit, you must be in Normal mode (Esc).
- Press
:(colon). The cursor will move down to the command line. - Enter one of the commands:
:w— write (save).:q— quit (exit). Works only if the file hasn't been changed!:wq— save and exit.ZZ(Shift+zz) — quick alternative to:wq(doesn't require pressing:).:q!— exit without saving, discarding all changes since the last save.:w filename— save with a different name.
Verification
- Create a test file:
vim test_vim.txt. - Press
i, type a few lines of text. - Press
Esc. - Try moving around the file with
j,k,w,b. - Delete a couple of words (
dw) or a line (dd). - Press
u— the changes should be undone. - Press
:and enter:wq. The filetest_vim.txtshould be saved in the current directory. Check it:cat test_vim.txt.
If everything succeeded — you've mastered the vital minimum.
Common Issues
- Issue: When trying to exit (
:q), Vim saysE37: No write since last change (add ! to override).- Cause: The file was changed but not saved.
- Solution: Either save (
:wq) or exit without saving (:q!).
- Issue: I accidentally pressed a key, and a strange line appeared at the bottom or the text started behaving unpredictably.
- Solution: Press
Esca few times. This guarantees a return to clean Normal mode.
- Solution: Press
- Issue: I can't type text, only move around.
- Cause: You are in Normal mode.
- Solution: Press
i,a, oroto switch to Insert mode. The status line will show-- INSERT --.
- Issue: I don't know which mode I'm in.
- Solution: Just press
Esc. If you were in Insert mode, you'll return to Normal. If you were already in Normal — nothing will change. This is a safe "reset".
- Solution: Just press
Next Steps
Having mastered these basics, you can already work. To become faster, learn commands gradually:
- Copy and paste:
yy(yank/copy line),p(paste after cursor),P(paste before). - Search:
/text+Enter(search forward),n— next match,N— previous. - Multiple files:
:e filename(open),:bn/:bp(next/previous buffer). - Window splitting:
:split(horizontal),:vsplit(vertical).
A useful cheat sheet is always at hand: inside Vim, type :help or search online for "vim cheat sheet". Most importantly — practice. Open any configuration file (/etc/fstab, ~/.bashrc) and try to make changes with Vim. You've got this!