Basics
Exit
:q
quit:q!
quit without saving:qa
close all files and quit:wq
or:x
save and close file
Edit
i
inserta
appendA
append at the end of lineo
next lineO
previous liner
replace one characterR
replace modex
delete characteru
undo<C-R>
redo
Copy/paste
yy
copy linep
pasteP
paste beforedd
cut
Sort
:sort
sort:sort!
sort in reverse order:sort u
sort and remove duplicates:sort i
ignore case
Search & replace
:s
search and replace in current line
:%s
search and replace in whole file
Note:
.
, *
, \
, [
, ^
, and $
are meta characters.
+
, ?
, |
, &
, {
, (
, and )
must be escaped to use their special function.
Replace character with new line, use \r
:
%s/;/\r/g
Tabs
- Open file in a new tab
:tabedit file.txt
or make the command shorter
:tabe file.txt
- Open the
file.txt
in a new tab, or go to the tab withfile.txt
if it’s already open:
:tab drop file.txt
- Open multiple files in tabs at startup
vim -p file1.txt file2.txt file3.txt
Moving between tabs
gt
or:tabn
- next tabgT
or:tabp
- previous tabNgt
go to tab numberN
-1gt
first tab,2gt
second tab etc.
Rearranging tabs
tabm N
- move tab to position N
(starting from 0)
Tricks
Delete empty lines
g/^$/d
Paste mode
set paste
Disable mouse
set mouse=""
Recover .swp file
Open vim, type:
:recover file.name.swp