Vim Basics: Navigation, Editing and Configuration
In this tutorial, you'll learn Vim fundamentals including modes, navigation, editing commands, visual mode, search and replace, and basic configuration for efficient text editing.
Why Vim Matters
Vim is everywhere. It is installed on virtually every Unix system. Learning Vim is not just about using an editor -- it is about editing text with the same speed as you think. Once the modal editing model clicks, editing file after file without touching a mouse becomes second nature.
By the end of this guide, you will navigate files, edit text, search and replace, and configure Vim for basic development tasks.
What is Vim?
Vim (Vi IMproved) is a modal text editor first released in 1991. It is the successor to Vi, which dates back to 1976. Vim's defining feature is its modal design: keys perform different actions depending on which mode you are in.
flowchart TD A[Normal Mode] -->|i| B[Insert Mode] B -->|Esc| A A -->|v| C[Visual Mode] C -->|Esc| A A -->|:| D[Command-Line Mode] D -->|Enter| A A -->|R| E[Replace Mode] E -->|Esc| A
Modes Explained
| Mode | Purpose | Enter | Exit |
|---|---|---|---|
| Normal | Navigate and manipulate text | Default | - |
| Insert | Type new text | i, a, o |
Esc |
| Visual | Select text | v, V, Ctrl-v |
Esc |
| Command | Save, quit, search, replace | : |
Enter or Esc |
| Replace | Overwrite existing text | R |
Esc |
Opening and Saving Files
# Open a file
vim myfile.txt
# Open at specific line number
vim +42 myfile.txt
Inside Vim:
| Command | Action |
|---|---|
:w |
Save the file |
:q |
Quit |
:wq |
Save and quit |
:q! |
Quit without saving |
:e filename |
Open another file |
Navigation
Basic Movement
h # Move left
j # Move down
k # Move up
l # Move right
Efficient Navigation
w # Jump word forward
b # Jump word backward
0 # Jump to beginning of line
$ # Jump to end of line
gg # Jump to first line
G # Jump to last line
:n # Jump to line n
Ctrl-d # Page down
Ctrl-u # Page up
Expected Behavior
Press w and the cursor jumps to the start of the next word. Press b and it jumps back. These motions can be preceded by a count: 3w jumps forward three words.
Editing Text
Insertion Commands
i # Insert before cursor
a # Append after cursor
o # Open new line below
O # Open new line above
I # Insert at beginning of line
A # Append at end of line
Deletion
x # Delete character under cursor
dw # Delete word
dd # Delete entire line
D # Delete to end of line
d$ # Delete to end of line
d0 # Delete to beginning of line
Change and Yank
cw # Change word (delete and enter insert mode)
cc # Change line
C # Change to end of line
yy # Yank (copy) line
yw # Yank word
p # Paste after cursor
P # Paste before cursor
Undo and Redo
u # Undo last change
Ctrl-r # Redo undone change
Example Editing Session
# Starting file: "hello world"
# Position cursor on 'h'
x # Delete 'h' → "ello world"
i # Enter insert mode
type "H" # → "Hello world"
Esc # Back to normal mode
ww # Move two words forward → cursor on 'w'
cw # Change word → delete 'world', enter insert
type "Vim" # → "Hello Vim"
Esc
:wq # Save and quit
Visual Mode
Visual mode lets you select text for operations.
v # Character-wise selection
V # Line-wise selection
Ctrl-v # Block selection (rectangular)
# After selection:
d # Delete selected
y # Yank selected
c # Change selected
> # Indent right
< # Indent left
~ # Toggle case
Example
# Select lines 5-8
:5,8d # Delete lines 5-8
# In visual mode:
V # Start line selection
jjj # Extend selection down 3 lines
> # Indent selected lines right
Search and Replace
/pattern # Search forward for pattern
?pattern # Search backward
n # Repeat search forward
N # Repeat search backward
:%s/old/new/g # Replace all occurrences in file
:%s/old/new/gc # Replace all with confirmation
Search Example
/function # Find the next 'function'
n # Go to next match
:%s/function/def/g # Replace all 'function' with 'def'
Basic Configuration
Vim reads ~/.vimrc on startup. Here is a starter configuration:
syntax on
set number
set relativenumber
set tabstop=4
set shiftwidth=4
set expandtab
set smartindent
set hlsearch
set incsearch
set ignorecase
set smartcase
set cursorline
set mouse=a
set clipboard=unnamedplus
What Each Setting Does
| Setting | Effect |
|---|---|
syntax on |
Enable syntax highlighting |
set number |
Show line numbers |
set relativenumber |
Show relative line numbers |
set tabstop=4 |
Tab width is 4 spaces |
set expandtab |
Convert tabs to spaces |
set hlsearch |
Highlight search results |
set incsearch |
Search as you type |
Common Errors
| Problem | Cause | Fix |
|---|---|---|
| File won't save | Read-only permission | Use :w !sudo tee % to save with sudo |
| Can't type normally | Stuck in insert mode or ex mode | Press Esc to return to normal mode |
| Backspace doesn't work in insert mode | Default Vim behavior | Add set backspace=indent,eol,start |
| Search highlighting won't clear | hlsearch is on | Run :nohlsearch or add nnoremap <Esc><Esc> :nohlsearch<CR> |
| Clipboard paste is weird | Vim indents pasted text | Use :set paste before pasting, then :set nopaste |
Practice Questions
1. What key do you press to enter insert mode?
i (or a, o for other forms of insertion).
2. How do you save and quit Vim in one command?
:wq.
3. What does dd do?
Deletes the current line.
4. How do you search for a pattern in Vim?
Press / then type the pattern and press Enter.
5. What does set relativenumber do in .vimrc?
Displays line numbers relative to the cursor position, making 5j etc. easier to count.
Challenge
Create a Vim macro that converts a CSV line to JSON format. For example, transform name,age,city into "name": "value", "age": "value", "city": "value". Record the macro and test it on a sample file.
Real-World Task
Use Vim to edit your shell configuration file (~/.zshrc or ~/.bashrc). Practice navigation: go to line 50 with :50, search for "export" with /export, delete three lines with 3dd, paste them elsewhere with p, and undo your changes with u. Save using :wq.
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro