Vi/Vim Complete Guide | "How to Exit" Practical Usage for Beginners
이 글의 핵심
Complete Vi/Vim editor guide. From exit methods to modes, navigation, editing, search, and replace - all practical commands.
Introduction: “Opened Vim but Cannot Exit!"
"Connected to server but no GUI editor”
When you SSH into a server, there’s no GUI editor. Vi/Vim is an essential text editor pre-installed on almost all Linux/Unix systems.
But when you first open Vim:
- Cannot type text
- Don’t know how to exit
- Mouse doesn’t work
- Commands are unusual
What This Guide Covers:
- Exit in 10 seconds (most searched question)
- Mode concept (Normal, Insert, Visual, Command)
- Practical commands (navigation, editing, search, replace)
- Essential settings (.vimrc)
- Cheat sheet
Table of Contents
- Emergency Exit: Quitting Vim
- Vi vs Vim Difference
- Understanding Modes
- Basic Navigation
- Text Editing
- Search and Replace
- Copy, Paste, Delete
- Save and Quit
- Split Windows
- Essential Settings (.vimrc)
- Cheat Sheet
1. Emergency Exit: Quitting Vim
Most Searched Question
# When you opened Vim and cannot exit
# Step 1: Press ESC key multiple times
ESC ESC ESC
# Step 2: Type one of these
:q # Quit without saving (no changes)
:q! # Force quit (discard changes)
:wq # Save and quit
:x # Save and quit (only if changed)
ZZ # Same as :x (faster)
Exit by Situation
| Situation | Command | Description |
|---|---|---|
| No changes made | :q | Just quit |
| Want to save changes | :wq or ZZ | Save and quit |
| Want to discard changes | :q! | Force quit |
| Save with filename | :w filename | Save as |
| Force save readonly | :w! | Only if you have permission |
2. Vi vs Vim Difference
Vi (Visual Editor)
- Developed in 1976 (almost 50 years ago!)
- Pre-installed on all Unix/Linux
- Minimal features
Vim (Vi IMproved)
- Developed in 1991 (improved Vi)
- Additional features:
- Syntax highlighting
- Multiple undo/redo
- Plugin system
- Split windows
- Auto-completion
- Visual mode
3. Understanding Modes
Vim’s Core: Modes
Vim is a modal editor. Same key behaves differently depending on mode.
1. Normal Mode (Default)
- Vim starts in this mode
- Execute commands (navigate, delete, copy)
- Cannot type text
- ESC returns here anytime
# Normal mode commands
dd # Delete current line
yy # Copy current line
p # Paste
u # Undo
2. Insert Mode (Input)
- Text input mode
- Type like normal editor
- ESC returns to Normal mode
# Enter Insert mode
i # Insert at cursor
I # Insert at line start
a # Append after cursor
A # Append at line end
o # Open line below
O # Open line above
3. Visual Mode (Selection)
- Text selection mode
- Select block then copy/delete
v # Character-wise selection
V # Line-wise selection
Ctrl+v # Block-wise selection (vertical)
4. Command Mode (Commands)
- Starts with
: - Save, quit, search, replace
:w # Save
:q # Quit
:wq # Save and quit
:%s/old/new/g # Replace
4. Basic Navigation
hjkl Instead of Arrow Keys
# Basic movement (arrow keys work but hjkl is faster)
h # ← Left
j # ↓ Down
k # ↑ Up
l # → Right
Why hjkl?: 1970s keyboards had no arrow keys when Vi was created!
Word Movement
w # Next word start
W # Next word start (space-separated)
e # Next word end
E # Next word end (space-separated)
b # Previous word start
B # Previous word start (space-separated)
Line Movement
0 # Line start
^ # Line start (excluding spaces)
$ # Line end
gg # File start
G # File end
:n # Go to line n (e.g., :42)
42G # Go to line 42
Screen Movement
Ctrl+f # Page down (forward)
Ctrl+b # Page up (backward)
Ctrl+d # Half page down
Ctrl+u # Half page up
H # Screen top (High)
M # Screen middle (Middle)
L # Screen bottom (Low)
5. Text Editing
Delete
x # Delete character at cursor
X # Delete character before cursor
dd # Delete current line
D # Delete from cursor to line end
dw # Delete word
d$ # Delete from cursor to line end
d0 # Delete from cursor to line start
dG # Delete from cursor to file end
dgg # Delete from cursor to file start
Change
r # Replace character (1 char)
R # Replace mode
cw # Change word
cc # Change line
C # Change from cursor to line end
s # Delete char and enter insert mode
S # Delete line and enter insert mode
Undo/Redo
u # Undo
Ctrl+r # Redo
. # Repeat last command
6. Search and Replace
Search
/pattern # Search forward
?pattern # Search backward
n # Next match
N # Previous match
* # Search word under cursor
# # Search word under cursor backward
Example:
/error # Search "error"
n # Next error
N # Previous error
Replace
:s/old/new/ # Replace first old with new in current line
:s/old/new/g # Replace all old with new in current line
:%s/old/new/g # Replace all old with new in entire file
:%s/old/new/gc # Replace all with confirmation
:5,10s/old/new/g # Replace in lines 5-10
Practical Examples:
# Change print to logging in Python
:%s/print(/logging.info(/g
# Replace tabs with 4 spaces
:%s/\t/ /g
# Remove trailing spaces
:%s/\s\+$//g
7. Copy, Paste, Delete
Copy (Yank)
yy # Copy current line
Y # Copy current line (same as yy)
yw # Copy word
y$ # Copy from cursor to line end
yG # Copy from cursor to file end
Paste
p # Paste after cursor
P # Paste before cursor
Copy with Visual Mode
# 1. Enter Visual mode with v
# 2. Select range with hjkl
# 3. Copy with y (or cut with d)
# 4. Paste with p
Example:
# Copy 5 lines
5yy # Copy 5 lines from current
p # Paste
# Block copy
Ctrl+v # Block selection mode
jjjj # Select 4 lines down
lll # Select 3 chars right
y # Copy
p # Paste
8. Save and Quit
Save
:w # Save
:w filename # Save as
:w! # Force save (readonly file)
:w >> file # Append to file
:5,10w file # Save lines 5-10
Quit
:q # Quit (no changes)
:q! # Force quit (discard changes)
:wq # Save and quit
:x # Save and quit (only if changed)
ZZ # Same as :x (faster)
ZQ # Same as :q!
9. Split Windows
Horizontal/Vertical Split
:split filename # Horizontal split
:sp filename # Horizontal split (short)
:vsplit filename # Vertical split
:vsp filename # Vertical split (short)
# Split empty window
:split # Horizontal
:vsplit # Vertical
Navigate Between Windows
Ctrl+w h # Left window
Ctrl+w j # Down window
Ctrl+w k # Up window
Ctrl+w l # Right window
Ctrl+w w # Next window
10. Essential Settings (.vimrc)
Create .vimrc File
# Create .vimrc in home directory
vim ~/.vimrc
Recommended Basic Settings
" Basic settings
set nocompatible " Disable Vi compatibility
syntax on " Syntax highlighting
set number " Line numbers
set relativenumber " Relative line numbers
set cursorline " Highlight cursor line
set ruler " Show cursor position
" Indentation
set autoindent " Auto indent
set smartindent " Smart indent
set tabstop=4 " Tab size
set shiftwidth=4 " Auto indent size
set expandtab " Convert tabs to spaces
" Search
set hlsearch " Highlight search results
set incsearch " Incremental search
set ignorecase " Ignore case
set smartcase " Case-sensitive if uppercase
" UI
set showmatch " Show matching brackets
set wildmenu " Command auto-completion
set laststatus=2 " Always show status bar
set showcmd " Show command
" Convenience
set mouse=a " Enable mouse
set clipboard=unnamed " Use system clipboard
set encoding=utf-8 " UTF-8 encoding
set backspace=indent,eol,start " Backspace behavior
" File type settings
filetype plugin indent on
" Color theme
colorscheme desert " Color theme (built-in)
11. Cheat Sheet
Mode Switching
| Command | Action |
|---|---|
ESC | To Normal mode |
i | Insert mode (at cursor) |
a | Insert mode (after cursor) |
o | Insert mode (new line below) |
v | Visual mode |
: | Command mode |
Top 20 Essential Commands
| Command | Action | Frequency |
|---|---|---|
:wq | Save and quit | ⭐⭐⭐⭐⭐ |
:q! | Force quit | ⭐⭐⭐⭐⭐ |
dd | Delete line | ⭐⭐⭐⭐⭐ |
yy | Copy line | ⭐⭐⭐⭐ |
p | Paste | ⭐⭐⭐⭐⭐ |
u | Undo | ⭐⭐⭐⭐⭐ |
/pattern | Search | ⭐⭐⭐⭐ |
:%s/old/new/g | Replace all | ⭐⭐⭐⭐ |
gg | File start | ⭐⭐⭐ |
G | File end | ⭐⭐⭐ |
0 | Line start | ⭐⭐⭐ |
$ | Line end | ⭐⭐⭐ |
dw | Delete word | ⭐⭐⭐ |
cw | Change word | ⭐⭐⭐ |
A | Insert at line end | ⭐⭐⭐ |
o | New line below | ⭐⭐⭐ |
Ctrl+f | Page down | ⭐⭐ |
Ctrl+b | Page up | ⭐⭐ |
. | Repeat last command | ⭐⭐⭐ |
* | Search word | ⭐⭐ |
Summary
Key Takeaways
- Exit:
ESC→:wq(save and quit) or:q!(force quit) - Modes: Normal (default) ↔ Insert (input) ↔ Visual (selection)
- Navigate:
hjkl,gg,G,0,$ - Edit:
dd(delete),yy(copy),p(paste) - Search:
/pattern,n(next),N(previous) - Replace:
:%s/old/new/g
Learning Roadmap
Day 1: Exit, mode switching, basic navigation
# Practice
vim practice.txt
i # Insert mode
(type)
ESC
:wq
Day 2: Editing commands
dd # Delete
yy # Copy
p # Paste
u # Undo
Day 3: Search and replace
/pattern
:%s/old/new/g
Week 1: .vimrc settings, plugin installation
Related Articles
- Shell/Bash/Zsh Complete Guide
- Linux Command Guide
- Git Complete Guide
Master Vim and boost productivity 10x! 🚀