This information is partially taken from here
- Nearly all commands can be preceded by a number for a repeat count. eg.
5dddeletes 5 lines Escgets 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 :helphelps 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;
-
wnext word (by punctuation) -
Wnext WORD (by spaces) -
bback word (by punctuation) -
Bback WORD (by spaces) -
eend word (by punctuation) -
Eend 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:
0goes 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 insertionfXgoes to the NEXT occurence of character X on the line;will find the NEXT occurrence,will find the PREVIOUS occurrence
3fXfinds the third occurence of character X on this linetXgoes to just BEFORE character XFandTare likefandt, but BACKWARDSdtXremoves everything from here to just before X
-
By paragraph:
{previous blank line}next blank line
-
By file:
gggoes to start of fileGgoes to end of file123Ggoes to specific line number ('123' in this case)
-
By marker:
mxsets bookmark x'xgoes to bookmark x'.goes to position of last edit''goes back to last point before jump:marksshows all bookmarks:delm xdeletes bookmark x:delm!deletes all bookmarks
-
Scrolling:
^Fforward full screen^Bbackward full screen^Ddown half screen^Uup half screen^Escroll one line up^Yscroll one line downztputs current line at the Top of the screenzzputs current line in the center of the screenzbputs current line at the Bottom of the screenHputs the cursor to the Highest line of the screenMputs the cursor to the Middle line of the screenLputs the cursor to the Lower line of the screen
uundo^Rredo.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.
iinserts text AT CURSORIinserts text at START OF LINEaappends text AFTER CURSORAappends text AFTER END OF LINEoopens new line BELOWOopens new line ABOVE
All change commands except r and R are terminated with Esc to return
to command mode.
rreplaces single characterRreplaces multiple charactersschanges single charactercwchanges from the cursor to end of the wordCchanges to end of lineccchanges whole linec<motion>changes text in the direction of the motionci(changes inside parentheses (see text object selection for more examples)
xdeletes chardwdeletes wordDdeletes to end of linedddeletes whole lined<motion>deletes in the direction of the motion
yycopies line into paste bufferddcuts line into paste bufferppastes buffer BELOW cursor linePpastes buffer ABOVE cursor linexpswaps two characters (xto delete one character, thenpto put it back after the cursor position)]ppastes with auto-reindent
vvisual block streamVvisual block line^vvisual block column- most motion commands extend the block to the new cursor position
omove the cursor to the other end of the block
dorxcuts block into paste bufferycopies block into paste buffer>indents block<unindents blockgvreselects last visual block:sortsorts selected rowsUconverts selected text to uppercaseuconverts selected text to lowercase~inverts case of selected text
:lsshows list of buffers:bXchanges to buffer number X:bnshows next file (buffer):bpshows previous file (buffer):bdclose current buffer and delete it from buffer list
:%s/foo/bar/gsubstitutes all occurrences of "foo" to "bar" IN WHOLE FILE:%s/foo/bar/gcsubstitutes all occurrences of "foo" to "bar" IN WHOLE FILE, with a prompt whether to substitute or not.:#,#s/old/new/gwhere #,# 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/gis 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 cursornnext match in SAME directionNnext match in OPPOSITE directionfXforward to NEXT character XFXbackward to PREVIOUS character X;moves again to same character in SAME direction,moves again to same character in OPPOSITE direction/word\cignores case for just one search command:set icsets the 'ignore case' option ('ignorecase'):set hlssets highlighting of matches ('hlsearch'):set isshows partial matching phrases ('incsearch'):nohlsearchremoves the highlighting of matches- Prepend "no" to switch an option off. For instance:
:set noic
:wwrites file to disk:w <filename>writes file to disk as filename:xwrites file to disk and quit (only save if necessary)ZZwrites file to disk and quit:enewopens a new buffer to edit a new file:n!edits a new file without saving current changes:qquits editing a file:q!quits editing without saving changes:qa!quits even if there are modified hidden buffers:eedits 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 FILENAMEsaves the visually selected lines to FILENAME:r FILENAMEretrieves FILENAME and puts it below the cursor position:r !commandreads output of command and puts it below cursor position
^Wnnew window^Wjdown to next window^Wkup 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 loadedgdgoes to definition of local symbol under cursorCtrl-Oreturns to previous positionCtrl-]jumps to definition of global symbol (requires tags file)Ctrl-Treturns 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-jwindow downCtrl-kwindow upCtrl-lwindow rightCtrl-hwindow leftzmopens search result in a new window<F5>switches between themesCtrl-PSuper searching<space>(orza): Folding code:sp <filename>splits the layout horizontally (:split):vs <filename>splits the layout vertically (:vsplit):tabeditopens a new empty tab:!<command>executes an external command:help <topic>provides help about topicCtrl-W Ctrl-Wjumps between windows- In command mode (
:e),Ctrl-Dshows 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:
0goes to the beggining of this lineyyanks 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 -
guwchanges to lowercase from here to the beginning of next word -
d3wdeletes from here to the beginnning of the third word -
:hardcopysends file to printer (but format is not very good) -
:retabconverts tabs to spaces -
:shelllaunches a shell console from Vim -
Ctrl-dcomes back from the console to Vim -
:colorscheme emacschanges the color scheme to emacs -
<F8>/Shift-<F8>changes to next/previous color scheme (setcolorsplugin) -
SetColors [all|my|blues slate ron|emacs]displays current scheme names or sets scheme names to specific list (setcolorsplugin)
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.