All notes
Vi

Cheet sheet

Any keys to enter command mode: ": / ?".

Format:

Example:

dw 		delete a word.
6dw 	delete 6 words.
y$		yank to the end of line.
3fe		move the cursor forward to the 3rd occurence of the letter e
:3,7d	delete lines 3-7.

Configuration

Help


:h subject

Motion

#---------- Move cursor

j		Jump to next line.
k		Jump to previous line.
h		Move to previous char.
l		Move to next char.

0/$		move cursor to start/end of line.
^		move cursor to first nonblank column.
w		move cursor to start of next word.
b		move back to start of previous word.
e		move forward to word end.

3SPACE  move forward 3 chars.
nEnter	n 为数字。光标向下移劢 n 行(常用)

fc		Move forward to char 'c'
Fc		Move back to char 'c'
()		move a sentence forward/back.
{}		move a paragraph forward/back.
%		Move to associated parenthesis.

G		Move to last line of the file.
nG		n 为数字。移劢到这个档案癿第 n 行。例如 20G 则会移劢到这个档案第 20 行(可配合 :set nu)
gg		移到这个档案第一行,相当于 1G (常用)

# Command mode.
:0		move cursor to first line.
:n		move cursor to line n.
:$		move cursor to last line.

#---------- Move screen

z.    to put the line with the cursor at the center,
zt    to put the line with the cursor at the top,
zb    to put the line with the cursor at the bottom of the screen.

H/M/L	Move to head/middle/last of screen.

Seletion

Wikia.

  1. v to begin character-based visual selection. V to select whole lines. Ctrl-v or Ctrl-q to select a block (all rows having same width, forming a rectangle).
  2. Move the cursor.
  3. d (delete) to cut, or y (yank) to copy.
  4. Move the cursor to the desired paste location.
  5. p to paste after the cursor, or P to paste before (wcf note: then I should use P according to my habit).
  6. If you want to change the selected text, press c instead of d or y, it will delete the selected text and enter insert mode so you can type the new text.
  7. Instead of inserting the block, it is also possible to replace (paste over) the destination. To do this, move to the target location then press 1vp (1v selects an area equal to the original, and p pastes over it).
  8. o will move the cursor to the opposite end of the selection. O allowing you to position the cursor in any of the four corners.
  9. gv in Normal mode to restore your previous selection.
  10. It's really worth it to check out the register functionality in Vim: :help registers.

Edit mode

i/I		insert before cursor/line.
a/A		Append after cursor/line.
o/O		open a new line after/before current line.
r		Replace one char.
R		Replaces through end of current line, then inserts.

Delete, yank, change

x/X		delete a char to the right/left of cursor.
Nx		delete N chars.
dw		delete a word.
D		delete to the end of the line.
dd		delete entire line.
:d		delete entire line.
yy		yank/copy current line.
:y		yank/copy current line.
p/P		Paste after/before the position.
C		Change to the end of the line. Change means a deletion plus switching to insert mode. cw: change word.
cc		Change the whole line.
J       Join the current line and the next. wcfNote: delete carriage return.

Replace

It is commonly used in combination with ranges or the :g command (below).

:s/pattern/string/flags	Replace pattern with string according to flags.
	Flags:
	g	Replace all occurences of pattern
	c	Confirm replaces.
&	Repeat last :s command

Ranges

:n,m			Lines n-m
:.				Current line
:$				Last line
:'c				Marker c
:%				All lines in file
:g/pattern/		All lines that contain pattern

Markers

mc		Set marker c on this line
`c		Go to beginning of marker c line.
'c		Go to first non-blank character of marker c line.

Other

.		repeat last text-changing command.
u		undo last change.
U		undo all changes to line.
J		Join lines.
~		Toggle upp and lower case.

File IO

:edit [your file]
  Open an existing file
:e#
  Move back to the previous file

FAQ

Exit from readonly mode

stackExchange.com: exit from read only mode.


:set noro

gvim, vim vs vi

ArchWiki.

Vim's added features include syntax highlighting, a comprehensive help system, native scripting (vimscript), a visual mode for text selection, and comparison of files (vimdiff).

gvim is built with GTK/X support.

Line number

To show the line number column, use :set number. By default absolute line numbers are shown, relative numbers can be enabled with :set relativenumber.

Jumping to a specific line is possible with :line number or line number gg. Jumps are remembered in a jump list, see :h jump-motions for details.

vimdiff


vimdiff file1 file2
Action 	Shortcut
next change 	]c
previous change 	[c
diff obtain 	do
diff put 	dp
fold open 	zo
fold close 	zc
rescan files 	:diffupdate
switch windows 	Ctrl+w+w