Vi/Vim Complete Guide | "How to Exit" Practical Usage for Beginners

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

  1. Emergency Exit: Quitting Vim
  2. Vi vs Vim Difference
  3. Understanding Modes
  4. Basic Navigation
  5. Text Editing
  6. Search and Replace
  7. Copy, Paste, Delete
  8. Save and Quit
  9. Split Windows
  10. Essential Settings (.vimrc)
  11. 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

SituationCommandDescription
No changes made:qJust quit
Want to save changes:wq or ZZSave and quit
Want to discard changes:q!Force quit
Save with filename:w filenameSave 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

/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
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
" 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

CommandAction
ESCTo Normal mode
iInsert mode (at cursor)
aInsert mode (after cursor)
oInsert mode (new line below)
vVisual mode
:Command mode

Top 20 Essential Commands

CommandActionFrequency
:wqSave and quit⭐⭐⭐⭐⭐
:q!Force quit⭐⭐⭐⭐⭐
ddDelete line⭐⭐⭐⭐⭐
yyCopy line⭐⭐⭐⭐
pPaste⭐⭐⭐⭐⭐
uUndo⭐⭐⭐⭐⭐
/patternSearch⭐⭐⭐⭐
:%s/old/new/gReplace all⭐⭐⭐⭐
ggFile start⭐⭐⭐
GFile end⭐⭐⭐
0Line start⭐⭐⭐
$Line end⭐⭐⭐
dwDelete word⭐⭐⭐
cwChange word⭐⭐⭐
AInsert at line end⭐⭐⭐
oNew line below⭐⭐⭐
Ctrl+fPage down⭐⭐
Ctrl+bPage up⭐⭐
.Repeat last command⭐⭐⭐
*Search word⭐⭐

Summary

Key Takeaways

  1. Exit: ESC:wq (save and quit) or :q! (force quit)
  2. Modes: Normal (default) ↔ Insert (input) ↔ Visual (selection)
  3. Navigate: hjkl, gg, G, 0, $
  4. Edit: dd (delete), yy (copy), p (paste)
  5. Search: /pattern, n (next), N (previous)
  6. 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


  • Shell/Bash/Zsh Complete Guide
  • Linux Command Guide
  • Git Complete Guide

Master Vim and boost productivity 10x! 🚀

... 996 lines not shown ... Token usage: 63706/1000000; 936294 remaining Start-Sleep -Seconds 3