Introduction / Why This Matters
Vim (Vi IMproved) is a powerful text editor that runs directly in the terminal. Knowing it is critically important for administering Linux servers, where a graphical interface is often unavailable. This guide will dispel the myth of Vim's complexity and give you practical skills for daily work: editing configuration files, viewing logs, making quick code edits. You'll stop fearing the black screen and learn to work efficiently without a mouse.
Requirements / Preparation
- Installed Vim. It comes pre-installed on most distributions. Check with:
vim --version. If it's missing, install it:sudo apt install vim(Debian/Ubuntu) orsudo yum install vim(RHEL/CentOS). - A terminal (konsole, gnome-terminal, xterm, etc.).
- Basic command-line knowledge (cd, ls).
- Write permissions in the directory where you'll create/edit files.
Step 1: Starting Vim and Understanding the Interface
Open your terminal and create a test file:
vim test.txt
You'll see a screen similar to this:
"test.txt" [New File]
~
~
~
~
~
~
~
~
What this means:
- The top line (
"test.txt" [New File]) is the status. - Tilde (
~) marks empty lines after the end of the file. - Most importantly: you are in Normal mode (sometimes called Command mode). In this mode, every key you press is a Vim command, not a character to input. If you start typing, nothing will appear. This is normal.
Step 2: Switching to Insert Mode
To start editing text, you need to enter Insert mode.
- Press the
ikey (Latini). - The prompt
-- INSERT --will appear at the bottom of the screen. - Now type text as you would in a regular editor. For example, enter:
Hello, world! This is my first file in Vim.
Quick ways to enter Insert mode:
i— insert before the cursor (most common).I— insert at the beginning of the current line.a— insert after the cursor.A— insert at the end of the current line.o— create a new line below the current one and enter Insert mode.O— create a new line above the current one.
Step 3: Navigating the File Without a Mouse
When you finish typing, press Esc — you'll return to Normal mode. Now control the cursor:
Basic movements (single keys):
h — left
j — down
k — up
l — right
Fast navigation:
w— jump to the beginning of the next word.b— jump to the beginning of the previous word.0(zero) — to the start of the line.$— to the end of the line.gg— to the very beginning of the file.G(Shift+g) — to the very end of the file.:nornG— go to line numbern(e.g.,:15or15G)./text+Enter— search forward.n— next match,N— previous.?text+Enter— search backward.
💡 Tip: Practice the
hjklmovements for 5 minutes a day. This is the foundation of speed in Vim.
Step 4: Editing: Delete, Undo, Copy, Paste
All actions are performed in Normal mode.
Deletion:
x— delete the character under the cursor.dd— delete (cut) the entire current line.dw— delete the word starting from the cursor.d$orD— delete from the cursor to the end of the line.dgg— delete from the current line to the beginning of the file.
Undo and redo:
u— undo the last change.Ctrl + r— redo an undone change.
Copying (yank) and pasting (put):
yy— copy (yank) the current line.yw— copy a word.y$— copy from the cursor to the end of the line.p— paste (put) the copied content after the cursor/line.P(Shift+p) — paste before the cursor/line.
Visual mode for selection:
Press v (character-wise visual mode) or V (line-wise visual mode). Move the cursor (hjkl) to select an area. Then:
y— copy the selection.d— delete (cut) the selection.>— indent the selection to the right.<— indent to the left.
Step 5: Saving and Exiting
All save/exit commands start with a colon : and are executed in Normal mode.
- Ensure you are in Normal mode (press
Esc). - Enter:
:w+Enter— save (write) the file.:q+Enter— quit, if there are no unsaved changes.:wqor:xorZZ— save and quit.:q!— quit without saving changes (forcefully).:w filename— save the current buffer under a different name ("Save As").
Step 6: Working with Multiple Files and Buffers
Vim can handle multiple files simultaneously.
:e filename— open (edit) another file in the current window. The current buffer is saved automatically if there were changes? No, you must save explicitly with:wor use:enewfor a new file.:bn— go to the next buffer (buffer next).:bp— go to the previous buffer.:ls— list all open buffers.:b n— go to buffer numbern(from the:lslist).
Verification
- Open your test file:
vim test.txt. - Enter Insert mode (
i), add a new line. - Return to Normal mode (
Esc). - Copy the added line (
yy), move to the end of the file (G), paste (p). - Save and exit (
:wq). - View the file with
cat test.txt— you should see two identical lines.
Common Issues
⚠️ "Frozen" in Vim, nothing happens? You are in Normal mode. Press
ito insert text orEsc, then:q!to exit.
⚠️ Can't exit, message "No write since last change (add ! to override)"? You have unsaved changes. Use
:wq(save and quit) or:q!(quit without saving).
⚠️ Cursor doesn't move with
hjklkeys? You might be using a non-standard layout or have remapped keys. Ensure you are in Normal mode (Esc). Some terminal emulators may have conflicts. Try using the arrow keys.
⚠️ Search (
/) doesn't find text that's there? Ensure you are searching for the exact spelling (case-sensitive by default). Use\cat the end of the pattern to ignore case:/text\c. Also, check if old search highlighting remains — clear it with:nohlsearch.
⚠️ Accidentally pressed
Ctrl + sand the terminal "froze"? This is standard terminal behavior (XON/XOFF flow control). Unlock it: pressCtrl + q. In Vim, you can disable this by addingset t_ts= t_fs=to your~/.vimrc(but this is for advanced users).