Table of Contents
Why vim?
vim is a powerful, keyboard‑driven text editor that comes preinstalled on almost every Linux system. Even if you prefer other editors, knowing basic vim is extremely useful because:
- It’s almost always available (including in minimal/rescue environments).
- Many tools open vim by default (e.g.,
git commitin some setups). - You can edit files over SSH without a graphical interface.
This chapter gives you just enough vim to open, edit, save, and quit files without panic.
Starting and Exiting vim
Opening files
- Open (or create) a file:
vim filename.txt- Open vim without a file (you can still start editing, then choose a name when saving):
vimInside vim, your whole terminal becomes the editor.
The three main modes (just the basics)
vim is “modal”: the same keys do different things depending on the mode.
For now, remember only these three:
- Normal mode: for commands (moving, deleting, saving, quitting).
- Insert mode: for typing text.
- Command-line mode: for commands that start with
:(like:wto save).
You’ll mostly switch between Normal and Insert.
How to know your mode
- If you see
-- INSERT --at the bottom: you’re in Insert mode. - If the bottom line is empty or shows file info, you’re usually in Normal mode.
- If you see
:at the bottom: you’re in Command-line mode.
When in doubt, press Esc once (or a couple of times). You’ll end up in Normal mode.
Quitting vim
These commands are typed from Normal mode, starting with ::
- Quit (no changes made):
:qthenEnter- Quit and discard all unsaved changes:
:q!thenEnter- Save (write) the file:
:wthenEnter- Save and quit:
:wqthenEnter- or
:xthenEnter(similar behavior)
If vim refuses to quit with :q it’s usually because there are unsaved changes; use :wq to save them or :q! to discard them.
Basic Editing Workflow
Entering Insert mode (to type text)
From Normal mode, use one of these:
i— insert before the cursor.a— append (insert) after the cursor.o— open a new line below the current one and start inserting.O— open a new line above the current one and start inserting.
After pressing one of these keys, you’re in Insert mode and can type normally.
Leaving Insert mode
- Press
Escto go back to Normal mode.
Develop the habit: type → Esc → move → i or a → type → Esc, etc.
Saving while editing
- From Normal mode:
:wthenEnterto save.:w filenamethenEnterto save to a new file name (Save As).- Combine with
:qwhen you’re done (:wq).
Moving Around in vim
Movement commands are given in Normal mode.
Arrow keys
Most terminals let you use arrow keys:
- Up, Down, Left, Right
These are fine when starting; classic vim users often prefer h j k l.
Basic vim movement keys
You can use these instead of arrow keys:
h— leftj— downk— upl— right
Try resting your right hand on the keyboard: index on j, middle on k, ring on l, pinky on ;. Then h is just to the left of j.
Moving by words and lines
All from Normal mode:
w— move to the start of the next word.b— move to the start of the previous word.e— move to the end of the current/next word.0(zero) — move to the start of the line.$— move to the end of the line.
Moving by lines and file
gg— go to the first line.G— go to the last line.:<number>thenEnter— go to a specific line (e.g.:10to line 10).
You can see line numbers by:
:set numberthenEnter(temporary for the current session).
Deleting and Undoing
Deleting characters and words
All in Normal mode:
x— delete the character under the cursor.dw— delete from the cursor to the start of the next word (like “delete word”).db— delete backwards to the start of the previous word.dd— delete the whole current line.
Deleted text is copied into a buffer (vim’s clipboard); basic paste is covered below.
Undo and redo
u— undo the last change.Ctrl+r— redo the last undone change.
This works for almost all edits: inserts, deletes, etc.
Copying and Pasting (Yank and Put)
vim uses its own terms:
- yank = copy
- put = paste
All commands here are in Normal mode:
yy— yank (copy) the current line.yw— yank from the cursor to the start of the next word.p— put (paste) after the cursor (or below current line for line yanks).P— put (paste) before the cursor (or above for line yanks).
For example:
- Move to a line.
- Press
yyto copy it. - Move somewhere else.
- Press
pto paste it below the current line.
Combining Commands with Counts
Many Normal mode commands can be repeated by prefixing them with a number.
Examples:
3w— move 3 words forward.5j— move 5 lines down.3x— delete 3 characters from the cursor to the right.4dd— delete 4 lines (current line + next 3).
Pattern: $N$<command>$ applies the command $N$ times.
Searching Inside a File
Search is done from Normal mode.
Basic search
/textthenEnter— search forward fortext.?textthenEnter— search backward fortext.
After a search:
n— go to the next match (same direction).N— go to the previous match (opposite direction).
Search is case‑sensitive by default. To make it case‑insensitive just for one search, you can use \c:
/Text\c— search forText,text,TEXT, etc.
Searching for the word under cursor
From Normal mode:
*— search forward for the word under the cursor.#— search backward for the word under the cursor.
Then use n / N to move between matches.
Opening and Saving Files from Inside vim
These commands assume you’re already in vim, in Normal mode.
Opening another file
:e otherfile.txtthenEnter— open another file in the same vim window.
If the current file has unsaved changes, vim may warn you. Save first (:w), or force the edit with :e! otherfile.txt (discarding unsaved changes).
Saving under a new name
:w newname.txtthenEnter— write the current contents to a new file.
You’ll still continue editing the original file unless you also :e newname.txt.
Basic Visual Selection (Optional but Useful)
Visual mode lets you select text before operating on it.
From Normal mode:
v— start character-wise visual selection.V— start line-wise visual selection.
Then:
- Move with arrow keys or
h j k lto expand/shrink the selection. - Use:
y— yank (copy) the selection.d— delete the selection.p— paste after the cursor (or replace the selection if in some setups).
Press Esc to exit Visual mode without changing anything.
Very Minimal Configuration for Beginners
vim is highly configurable but you don’t need much to start. For small quality-of-life improvements, you can create a simple configuration.
Creating a basic vim config
Create (or edit) ~/.vimrc using an editor you’re comfortable with (even vim itself):
vim ~/.vimrcAdd a few simple settings:
set number " show line numbers
set ignorecase " case-insensitive search by default
set smartcase " but case-sensitive if search contains capitals
set hlsearch " highlight search matches
set incsearch " show matches as you type the searchThese lines:
- Start with
setto change options. - Everything after
"on a line is a comment.
Save and quit (:wq). Next time you run vim, these options will be enabled.
Safe Practice Ideas
To build confidence:
- Create a test file:
vim practice.txt- Try this sequence a few times:
- Enter Insert mode:
i - Type some text.
- Exit Insert mode:
Esc - Move with
h j k l. - Delete a line with
dd. - Undo with
u. - Save with
:w. - Quit with
:qor:wq. - Practice searching:
- Add repeated words.
- Use
/wordthennandN.
The goal at this stage is not to master vim, but to be comfortable enough that you can open, edit, save, and quit without getting stuck.