This information is partially taken from here
- Nearly all commands can be preceded by a number for a repeat count. eg.
5dd
deletes 5 lines Esc
gets you out of any mode and back to command mode- Commands preceded by
:
are executed on the command line at the bottom of the screen :help
helps with any command- Note: The key for productivity in Vim is to stay most of the time in NORMAL mode, and use from there all the available commands to modify text and move around.
-
Cursor movement: ←h ↓j ↑k l→
-
By words:
NOTE: 'words' are letters and the underscore character, while 'WORDS' are characters separated by blanks. For instance, in the following line:
(name_1,vision_3; # this is a comment.
Here the first 'word' is name_1
, while the first 'WORD' is (name_1,vision_3;
-
w
next word (by punctuation) -
W
next WORD (by spaces) -
b
back word (by punctuation) -
B
back WORD (by spaces) -
e
end word (by punctuation) -
E
end WORD (by spaces) -
*
goes to the NEXT occurence of the word under the cursor -
#
goes to the PREVIOUS occurence of the word under the cursor -
By line:
0
goes to first column^
goes to first NON-BLANK character of the line$
goes to end of lineg_
goes to the last NON-BLANK character of the lineg;
goes to place of last insertionfX
goes to the NEXT occurence of character X on the line;
will find the NEXT occurrence,
will find the PREVIOUS occurrence
3fX
finds the third occurence of character X on this linetX
goes to just BEFORE character XF
andT
are likef
andt
, but BACKWARDSdtX
removes everything from here to just before X
-
By paragraph:
{
previous blank line}
next blank line
-
By file:
gg
goes to start of fileG
goes to end of file123G
goes to specific line number ('123' in this case)
-
By marker:
mx
sets bookmark x'x
goes to bookmark x'.
goes to position of last edit''
goes back to last point before jump:marks
shows all bookmarks:delm x
deletes bookmark x:delm!
deletes all bookmarks
-
Scrolling:
^F
forward full screen^B
backward full screen^D
down half screen^U
up half screen^E
scroll one line up^Y
scroll one line downzt
puts current line at the Top of the screenzz
puts current line in the center of the screenzb
puts current line at the Bottom of the screenH
puts the cursor to the Highest line of the screenM
puts the cursor to the Middle line of the screenL
puts the cursor to the Lower line of the screen
u
undo^R
redo.
repeats last editing commandN<command>
repeats N times (p.e.100idesu [ESC]
inserts 'desu ' 100 timesN.
repeats last editing command N times
All insertion commands are terminated with Esc
to return to command mode.
i
inserts text AT CURSORI
inserts text at START OF LINEa
appends text AFTER CURSORA
appends text AFTER END OF LINEo
opens new line BELOWO
opens new line ABOVE
All change commands except r
and R
are terminated with Esc
to return
to command mode.
r
replaces single characterR
replaces multiple characterss
changes single charactercw
changes from the cursor to end of the wordC
changes to end of linecc
changes whole linec<motion>
changes text in the direction of the motionci(
changes inside parentheses (see text object selection for more examples)
x
deletes chardw
deletes wordD
deletes to end of linedd
deletes whole lined<motion>
deletes in the direction of the motion
yy
copies line into paste bufferdd
cuts line into paste bufferp
pastes buffer BELOW cursor lineP
pastes buffer ABOVE cursor linexp
swaps two characters (x
to delete one character, thenp
to put it back after the cursor position)]p
pastes with auto-reindent
v
visual block streamV
visual block line^v
visual block column- most motion commands extend the block to the new cursor position
o
move the cursor to the other end of the block
d
orx
cuts block into paste buffery
copies block into paste buffer>
indents block<
unindents blockgv
reselects last visual block:sort
sorts selected rowsU
converts selected text to uppercaseu
converts selected text to lowercase~
inverts case of selected text
:ls
shows list of buffers:bX
changes to buffer number X:bn
shows next file (buffer):bp
shows previous file (buffer):bd
close current buffer and delete it from buffer list
:%s/foo/bar/g
substitutes all occurrences of "foo" to "bar" IN WHOLE FILE:%s/foo/bar/gc
substitutes all occurrences of "foo" to "bar" IN WHOLE FILE, with a prompt whether to substitute or not.:#,#s/old/new/g
where #,# are the line numbers of the range of lines where the substitution is to be done.%
is a range that indicates every line in the file/g
is a flag that changes all occurrences on a line instead of just the first one
/
searches FORWARD?
searches BACKWARD*
searches FORWARD for word under cursor#
searches BACKWARD for word under cursorn
next match in SAME directionN
next match in OPPOSITE directionfX
forward to NEXT character XFX
backward to PREVIOUS character X;
moves again to same character in SAME direction,
moves again to same character in OPPOSITE direction/word\c
ignores case for just one search command:set ic
sets the 'ignore case' option ('ignorecase'):set hls
sets highlighting of matches ('hlsearch'):set is
shows partial matching phrases ('incsearch'):nohlsearch
removes the highlighting of matches- Prepend "no" to switch an option off. For instance:
:set noic
:w
writes file to disk:w <filename>
writes file to disk as filename:x
writes file to disk and quit (only save if necessary)ZZ
writes file to disk and quit:enew
opens a new buffer to edit a new file:n!
edits a new file without saving current changes:q
quits editing a file:q!
quits editing without saving changes:qa!
quits even if there are modified hidden buffers:e
edits same file again (if changed outside Vim):e .
directory explorer:e <FILENAME>
opens FILENAME:saveas <path/to/file>
saves to path/to/filev <motion> :w FILENAME
saves the visually selected lines to FILENAME:r FILENAME
retrieves FILENAME and puts it below the cursor position:r !command
reads output of command and puts it below cursor position
^Wn
new window^Wj
down to next window^Wk
up to previous window^W_
maximises current window^W=
makes all windows equal size^W+
increases window size^W-
decreases window size
%
jumps to matching parenthesis/bracket/brace, or language block if language module loadedgd
goes to definition of local symbol under cursorCtrl-O
returns to previous positionCtrl-]
jumps to definition of global symbol (requires tags file)Ctrl-T
returns to previous position (arbitrary stack of positions maintained)Ctrl-N
(in insert mode) automatic word completion
qa
-> records your actions in register 'a'@a
-> this will call the macro saved in register 'a'@@
-> calls the last executed macroCtrl-j
window downCtrl-k
window upCtrl-l
window rightCtrl-h
window leftzm
opens search result in a new window<F5>
switches between themesCtrl-P
Super searching<space>
(orza
): Folding code:sp <filename>
splits the layout horizontally (:split):vs <filename>
splits the layout vertically (:vsplit):tabedit
opens a new empty tab:!<command>
executes an external command:help <topic>
provides help about topicCtrl-W Ctrl-W
jumps between windows- In command mode (
:e
),Ctrl-D
shows a list of commands that start with "e" - Most commands can be used with the following general format:
<START-position><COMMAND><END-position>
For example, 0y$
means:
0
goes to the beggining of this liney
yanks from here...$
...up to the end of this line
So, ye
means 'yank from here to the end of the word'
Another example is y2/foo
: 'yank up to the second occurrence of "foo"'
Also:
-
gU$
changes to uppercase from here to the end of the line -
guw
changes to lowercase from here to the beginning of next word -
d3w
deletes from here to the beginnning of the third word -
:hardcopy
sends file to printer (but format is not very good) -
:retab
converts tabs to spaces -
:shell
launches a shell console from Vim -
Ctrl-d
comes back from the console to Vim -
:colorscheme emacs
changes the color scheme to emacs -
<F8>/Shift-<F8>
changes to next/previous color scheme (setcolors
plugin) -
SetColors [all|my|blues slate ron|emacs]
displays current scheme names or sets scheme names to specific list (setcolors
plugin)
Vim has some features that make it easy to highlight lines that have been changed from a base version in source control. Greg Hewgill has created a small Vim script that makes this easy.