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/12
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/12
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/12
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/12
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/mswin.vim
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/12
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/12
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 file2.txt
:split file3.txt
✅ Pro Tips:
Macros: Record macros ( qa ... commands ... q ) and replay ( @a ).
Buffers: Manage buffers using :ls , :bn , :bp .
Sessions: Save current editing sessions ( :mksession session.vim ).
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/12
practical use and enhance productivity for all levels of users, from beginners to experienced
developers.
0/2
Here are 50 more powerful Vim commands & features to add to your guide:
🚩 Advanced Navigation
Command Description
% Jump between matching parentheses/brackets {} , [] , ()
'' Jump to the previous cursor position
Jump back to last edit position
:marks List all marks
ma Create mark 'a'
`a Jump to mark 'a'
Ctrl + o Jump back in cursor position history
Ctrl + i Jump forward in cursor position history
zt Scroll cursor line to top of screen
zz Scroll cursor line to center of screen
zb Scroll cursor line to bottom of screen
🎯 Advanced Editing
Command Description
~ Toggle case of current character
gUw Change word to uppercase
guw Change word to lowercase
gUU Uppercase current line
8/12
Command Description
guu Lowercase current line
:set paste Enable paste mode (avoids auto-indent)
:set nopaste Disable paste mode
. Repeat last editing action
gq Format lines (auto-wrap according to settings)
= Auto-indent line(s)
🧩 Windows & Tabs Management
Command Description
:tabnew filename Open file in new tab
gt Go to next tab
gT Go to previous tab
:tabs List open tabs
Ctrl + w s Split window horizontally
Ctrl + w v Split window vertically
Ctrl + w o Close all splits except current
Ctrl + w T Move current window to new tab
📌 Buffers Management
Command Description
:ls List buffers
:bn Go to next buffer
:bp Go to previous buffer
:bd Delete (close) current buffer
9/12
Command Description
:b# Switch to alternate buffer
📁 File Exploration & Management
Command Description
:Explore or :E Open built-in file explorer
:Sexplore Open explorer horizontally
:Vexplore Open explorer vertically
:find filename Find and open file in path
gf Open file under cursor
🔧 Advanced Search & Replace
Command Description
:%s/foo/bar/gci Interactive search & replace with case-insensitive
* Search for word under cursor forward
# Search for word under cursor backward
g* Search partial word forward
g# Search partial word backward
🛠 Macros & Automation
Command Description
qa Start recording macro to register 'a'
q Stop macro recording
10/12
Command Description
@a Execute macro stored in 'a'
@@ Repeat last executed macro
💻 Syntax Highlighting & Themes
Command Description
:syntax on/off Enable or disable syntax highlighting
:colorscheme <name> Change Vim theme ( default , elflord , desert , etc.)
:set background=light Switch to light background theme
⚡ Performance & Debugging
Command Description
:set lazyredraw Improve performance by reducing redraw
:set nowrapscan Stop searches from looping around the file
:set history=1000 Increase undo/search history limit
📡 Miscellaneous Utilities
Command Description
:!<command> Run shell commands from Vim (e.g., :!ls )
:r !<command> Insert output of shell command into file
K Look up word under cursor with man page
:set spell spelllang=en_us Enable spell-check
]s / [s Move to next/previous misspelled word
11/12
Command Description
z= Suggest corrections for misspelled word
These additional 50 commands and features further elevate your Vim skills, enabling greater
efficiency, automation, and powerful editing capabilities.
12/12