Vi/Vim Command Guide
Certainly! Here's your documentation expanded and structured systematically, incorporating
your existing information clearly and adding more than 50 additional commands and
features. Structured in an easy-to-follow format with proper tables and highlights:
Comprehensive Guide to Vi/Vim/gVim
Editor Commands & Configuration
1. Opening & Saving Files
Command Description
vi <filename> Open a file
vi *.c Open all .c files in current directory
:w Save
:q Quit
:wq Save and quit
:w! Force save (even read-only files, e.g., /etc/shadow )
:q! Force quit without saving
:wq! Force save and quit
2. Editing Commands
Command Description
i Insert before cursor
I Insert at line beginning
a Append after cursor
A Append at line end
1/8
Command Description
o Open new line below
O Open new line above
x Delete character (e.g., nx for multiple chars)
dw Delete word ( ndw )
dd Delete line ( ndd )
:n1,n2 d Delete lines n1 through n2
D Delete rest of line
cw Change word ( ncw )
cc Change entire line
C Change rest of line
r Replace character ( nr )
R Replace mode (till ESC )
xp Swap character positions
u Undo last edit
Ctrl + r Redo last undone edit
J Join current line with next line
3. Cursor Movement
Command Description
:n Go to line number n
:1 Go to top of file
:$ Go to end of file
$ Go end of line
0 or ^ Go start of line
k Move cursor up ( nk lines)
j Move cursor down ( nj lines)
h Move cursor left ( nh cols)
2/8
Command Description
l Move cursor right ( nl cols)
Ctrl + b or Pg Up Page up
Ctrl + f or Pg Dn Page down
w Move cursor forward one word
b Move cursor backward one word
gg Go to first line
G Go to last line
4. Working with Multiple Files
Command Description
:n Next file
:N or :prev Previous file
:args List all open files
:vsplit file Vertical split screen with file
:split file Horizontal split screen with file
Ctrl + w w Switch between splits
Ctrl + w q Close current split
Ctrl + w = Equalize split window size
5. Search & Substitution
Command Description
/pattern Search forward
?pattern Search backward
n Next occurrence
3/8
Command Description
N Previous occurrence
:%s/old/new/g Global substitution (all occurrences)
:n1,n2 s/old/new/gc Substitute interactively
:set hlsearch Highlight search matches
:nohl Turn off highlighting
6. Copy, Cut & Paste
Command Description
yy Yank (copy) line ( nyy for multiple lines)
dd Cut line ( ndd for multiple lines)
p Paste below cursor
P Paste above cursor
"ayy Yank line into register 'a'
"ap Paste from register 'a'
7. Regular Expressions & Patterns
Pattern Description
^ Beginning of line
$ End of line
. Any single character
* Zero or more of previous
[A-Z] Uppercase A-Z
[a-z] Lowercase a-z
[0-9] Digits 0-9
4/8
Pattern Description
[abc] Matches 'a', 'b', or 'c'
[^abc] Any char except 'a', 'b', 'c'
\ Escape special char
8. Advanced File Operations
Command Description
:r file Read from file
:n1,n2 w file Write lines n1-n2 to file
:n1,n2 w >> file Append lines n1-n2 to file
:n1,n2 t n3 Transfer lines after line n3
9. Vim Configuration ( .vimrc / .exrc )
Stored in user's $HOME directory.
Sample .vimrc :
vim
set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/[Link]
behave mswin
set nu " Line numbers
set nobackup " No backup files
set ts=5 " Tabstop spaces
set ai " Auto-indent
set ic " Ignore case search
syntax on " Syntax highlighting
set showmatch " Matching brackets highlighting
5/8
set hlsearch " Highlight search results
set ruler " Display cursor position
set smartindent " Intelligent indenting
set expandtab " Convert tabs to spaces
set background=dark " Dark theme
Open .vimrc :
:scriptnames – Lists loaded config files.
:echo($MYVIMRC) – Prints .vimrc path.
:e $MYVIMRC – Edit .vimrc .
10. Extra Useful Commands
Command Description
:set all Show all settings
:set option? Show specific option value
Ctrl + g Show file details
Ctrl + l Refresh screen
:map key cmd Map custom shortcuts
:unmap key Remove key mapping
:ab abbrev expansion Define abbreviation
:%!sort Sort file content
:set nowrap Disable line wrapping
:set wrap Enable line wrapping
Complex Examples:
Remove first 5 characters from all lines:
6/8
vim
:%s/^.....//
Remove last 5 characters from all lines:
vim
:%s/.....$//
Multiple string searches:
vim
/^fail.*warn.*down/ # 'fail' at start, contains 'warn' and 'down'
/fail\|warn # 'fail' OR 'warn'
Open multiple files for comparison:
vim
:vsplit [Link]
:split [Link]
✅ Pro Tips:
Macros: Record macros ( qa ... commands ... q ) and replay ( @a ).
Buffers: Manage buffers using :ls , :bn , :bp .
Sessions: Save current editing sessions ( :mksession [Link] ).
Folding: Collapse/expand code blocks ( zf , zo , zc ).
This comprehensive guide combines clarity, detailed examples, organized structure, and
extensive additions of useful commands and Vim configurations. It’s designed to support
7/8
practical use and enhance productivity for all levels of users, from beginners to experienced
developers.
0/2
8/8