In today’s post, we will review some basic operations in Vim.
Basics
Moving Around: h, j, k, l
- To start:
gg
- To end:
G
- To start of line:
0
- To end of line:
$
- To previous location:
Ctrl + o
Number + Char:
5j
: Down 5 lines6dd
: Delete 6 lines7G
: Jump to line 7
Other:
Quit:
Shift + zz
Repeat Same Command:
.
Change cases (upper / lower):
-
Quick switch with the line below:
ddp
Tab:
>>
, and back:<<
Common Options
Delete
x
: Del the chardw
&de
: Del wordsdgg
: Del till start of fileD
: Del till end of line
Undo
u
: UndoCtrl + r
: Undo undo
Cut, Copy & Paste
Cut:
dd
: Cut
Copy:
y
: Copyyy
: Whole line- Other usage are the same with Delete.
Paste:
p
: After the cursorP
: Before the cursor
Quick Search
/<STRING>
&?<STRING>
n
: Next resultN
: Previous one
Find & Replace
Format: : {range} s/ {target} / {replace} / {sign}
:%s/hello/world/g
%
: Range is entire file (Without%
: Only current line)hello
: Found stringworld
: Replace to this wordg
: Only replace first match of each line- Other choices of
{sign}
: Ignore the cases (i
), Confirm at each replacement (gc
)
:5,12s/hello/world
: Between line 5 - 12
:.,+2s/hello/world
:
.
: Current line+2
: 2 lines after
:'<,'>s/hello/world
: In Visual mode, :'<,'>
is auto-complete in the selected area
Additional Options
Edit Multiple Files
Two Files: vim file1 file2
:n
: Switch to file 2 (changes unsaved):N
: Switch to file 1 (changes unsaved)
Multiple Files:
Open a 3rd file:
:e file3
:e! file4
: Abandon file3 (unsaved), open a new fileBack to previous:
:e#
List the files:
:ls
Go to file 1:
b file1
Change filename:
:f <NEW NAME>
Open Multiple Windows
Open a new window:
:new
orCtrl + w
New horizontal window:
:sp file1
New vertical window:
:vsp file2
Move among windows:
Ctrl + w h / j / k / l
Move windows around:
Ctrl + w H / J / K / L
Reduce / Increase window’s height:
Ctrl + w - / +
Command-Line Mode
Check vim’s settings (
:scriptname
). Create.vimrc
under home directory.
:set nu
: Line numbers:set tabstop=4
: Set to 4 (Default is 8):set autoindent
: Indent automatically:ce
: Center the line:set readonly
: Read-only:set encoding=utf-8
: UFT-8 coding:set backup
: Backup file:ver
: Show version
Other
Visual mode:
v
, highlighting textsSelect mode:
gvim
- Start:
:set selectmode+=mouse
- Start:
Replace mode:
R
Encrypt file:
vim -x <FILE>
Help:
F1