VIM - Command Examples
Opening and Closing Files
To open a file in Vim, use the following command:
vim filename
To open multiple files:
vim file1 file2 file3
To quit Vim:
:q
To save changes and exit:
:wq
To force quit without saving changes:
:q!
To save a file without exiting:
:w
Moving the Cursor
Move the cursor word by word:
w (Move to the start of the next word) b (Move to the start of the previous word)
Move the cursor by line:
j (Move down one line) k (Move up one line)
Move to the beginning or end of a line:
0 (Move to the beginning of the line) $ (Move to the end of the line)
Move to the beginning of the file:
gg
Move to the end of the file:
G
Search for a word:
/search_term
Jump to the next match:
n
Jump to the previous match:
N
Editing Text
Delete a character under the cursor:
x
Delete an entire line:
dd
Delete a word:
dw
Delete until the end of the line:
D
Delete from the cursor to a specific character:
dF<char> (Delete up to and including <char>)
Undo the last change:
u
Redo the last undone change:
Ctrl + r
Copy a line:
yy
Paste the copied text:
p
Replace the character under the cursor:
r<char>
Replace a word:
cw
Visual Mode
Enter visual mode (to select text):
v
Select a block of text:
V (Visual mode for line selection)
Select text in a block, for columns:
Ctrl + v (Visual block mode)
Searching and Replacing
Find and replace in the entire file:
:%s/old/new/g
Find and replace with confirmation:
:%s/old/new/gc
Find and replace in a specific range (lines 10 to 20):
:10,20s/old/new/g
Find and replace the current word under the cursor:
* (Search for the word under the cursor)
Working with Buffers
List all open buffers:
:ls
Switch to the next buffer:
:bn
Switch to the previous buffer:
:bp
Delete a buffer:
:bd
Working with Windows and Tabs
Split the window horizontally:
:split
Split the window vertically:
:vsplit
Move between windows:
Ctrl + w, h (Move left) Ctrl + w, j (Move down) Ctrl + w, k (Move up) Ctrl + w, l (Move right)
Open a new tab:
:tabnew
Switch to the next tab:
:tabn
Switch to the previous tab:
:tabp
Macros
Start recording a macro:
qa (Record into register 'a')
Stop recording the macro:
q
Play the macro:
@a
Repeat the macro multiple times:
5@a (Repeat macro 'a' five times)
Customizing Vim
Set line numbers:
:set number
Set relative line numbers:
:set relativenumber
Enable line wrapping:
:set wrap
Set indentation level:
:set shiftwidth=4
Turn on auto-indentation:
:set smartindent
Set search highlighting:
:set hlsearch
Disable search highlighting:
:set nohlsearch
Enable syntax highlighting:
:syntax on
Working with Multiple Files
Open files side by side:
:vs filename (Vertical split with file) :sp filename (Horizontal split with file)
Cycle through files in the current window:
Ctrl + w, w
Advanced Text Navigation
Go to the matching parenthesis or bracket:
%
Move to the first non-blank character of the current line:
^
Move to the first character of the current line:
0
Move to the end of the current line:
$
Jump to a specific line:
:linenumber
Search backward:
?search_term
Ex Commands
Execute a shell command:
:!command
Write all files:
:wall
Open a file in a different tab:
:tabnew filename
Exit all files:
:qa!
Autocommands
Set an autocmd to highlight trailing spaces:
autocmd BufWritePre * %s/\s\+$//e
Plugins and Extensions
To install plugins with vim-plug:
Plug 'plugin/repo'
To install all plugins:
:PlugInstall
Miscellaneous
Show the current mode (insert, command, etc.):
Ctrl + g
Show file information (file name, line, column, etc.):
Ctrl + g
