Normal mode
- Cut
- Copy
- Paste
- Undo
- Redo
- Replace characters
- Repeat
- Open new line
- Indenting
- Arrow Movements
- Moving within current line
- Word and Character based move
- Text Objects based move
- Moving within current file
- Scrolling
- Marking frequently used locations
- Jumping back and forth
- Numbers
- Multiple copy-paste using " registers
- Special registers
- Combining editing commands with movement commands
- Context editing
- Miscellaneous
Remember to be in Normal mode, if not press Esc key. Press Esc again if needed
Cut
There are various ways to delete text, which can then be pasted elsewhere using paste command
- Select text (using mouse or visual commands), press
dto delete the text dddelete current line2dddelete current line and the line below it - total 2 linesdjord↓can also be used (jand↓both function as down arrow key)
10dddelete current line and 9 lines below it - total 10 linesdkdelete current line and the line above itd↑can also be used
Ddelete from current character to end of linexdelete only the current character under cursor5xdelete character under cursor and 4 characters to its right - total 5 charactersccdelete current line and change to Insert mode4ccdelete current line and 3 lines below it and change to Insert mode - total 4 linesCdelete from current character to end of line and change to Insert modesdelete only the character under cursor and change to Insert mode5sdelete character under cursor and 4 characters to its right and change to Insert mode - total 5 charactersSdelete current line and change to Insert mode (same ascc)
Copy
There are various ways to copy text using the yank command y
- Select text to copy (using mouse or visual commands), press
yto copy the text yycopy current lineYalso copies current liney$copy from current character to end of line2yycopy current line and the line below it - total 2 linesyjandy↓can also be used
10yycopy current line and 9 lines below it - total 10 linesykcopy current line and the line above ity↑can also be used
Paste
The paste command p is used after cut or copy operations
ppaste the copied content one time- If copied text was line(s) paste below the current line
- If the copied text was less than a line, paste the content to right of the cursor
Ppaste the copied content one time- If copied text was line(s) paste above the current line
- If the copied text was less than a line, paste the content to left of the cursor
3ppaste the copied content three times
Undo
uundo last edit- press
uagain for further undos
- press
Uundo changes on current line- press
Uagain to redo changes
- press
Redo
Ctrl+rto redo a change undone byu
Replace characters
Often, we just need to change one character (ex: changing i to j)
rjreplace the charcter under cursor with jryreplace the character under cursor with y3rareplace character under cursor and 2 characters to the right with aaa
To replace mutliple characters with different characters, use uppercase R
RlionEscreplace character under cursor and 3 characters to right with lion.Esckey marks the completion ofRcommand
The advantage of r and R commands is that one remains in Normal mode, without the need to switch to Insert mode and back
Repeat
.the dot command repeats the last change- If the last command was deleting two lines and dot key is pressed, two more lines will get deleted
- If last command was to select five characters and delete them, dot key will select five characters and delete
- If the last change was clearing till end of word and inserting some text, dot key will repeat that change
Open new line
oopen new line below the current line and change to Insert modeOopen new line above the current line and change to Insert mode
Indenting
>>indent current line3>>indent current line and two lines below<<unindent current line5<<unindent current line and four lines below
Arrow Movements
The four arrow keys can be used in Vim to move around as in any other text editor. Vim also maps them to four characters in Normal mode (faster typing compared to arrow keys)
hleft ←jdown ↓kup ↑lright →
Moving within current line
0move to beginning of current line - column number 1^move to beginning of first non-blank character of current line, useful for indented lines$move to end of current lineg_move to last non-blank character of current line3|move to 3rd column character
Moving within long lines(spread over multiple screen lines)
g0to move to beginning of current screen lineg$to move to end of current screen linegmto move to middle of current screen lineg^to move to first non-blank character of current screen line
See :h left-right-motions for more info
Word and Character based move
Difference between word and WORD (definitions quoted from :h word)
word A word consists of a sequence of letters, digits and underscores, or a sequence of other non-blank characters, separated with white space (spaces, tabs,
). This can be changed with the 'iskeyword' option. An empty line is also considered to be a word. WORD A WORD consists of a sequence of non-blank characters, separated with white space. An empty line is also considered to be a WORD.
Word based move
wmove to start of next word (192.1.168.43 requires multiplewmovements)Wmove to start of next WORD (192.1.168.43 requires oneWmovement)bmove to beginning of current word if cursor is not at start of word or beginning of previous wordBmove to beginning of current WORD if cursor is not at start of WORD or beginning of previous WORDemove to end of current word if cursor is not already at end of word or end of next wordEmove to end of current WORD if cursor is not already at end of WORD or end of next WORD3wmove 3 words forward, similarly number can be prefixed forW,b,B,e,E
Character based move
f(move forward in the current line to next occurrence of character (fbmove forward in the current line to next occurrence of character b3f"move forward to third occurrence of character " in current linet;move forward in the current line to character just before ;3txmove forward to character just before third occurrence of character x in current lineFamove backward in the current line to character aTamove backward in the current line to character just after a;repeat previousf,F,t,Tmovement in forward direction,repeat previousf,F,t,Tmovement in backward direction
Text Objects based move
(move backward a sentence)move forward a sentence{move backward a paragraph}move forward a paragraph:h object-motionsfor more info
Moving within current file
ggmove to first non-blank character of first line of fileGmove to first non-blank character of last line of file5Gmove to first non-blank character of fifth line of file, similarly10Gfor tenth line and so on:5move to first non-blank character of fifth line of file, similarly:10for tenth line and so on%move to matching pair of brackets like() {} [](Nesting is taken care)- To add a new matching pair like <>, add
set matchpairs+=<:>to your vimrc file - It is also possible to match a pair of keywords like HTML tags, if-else, etc with
%Check:h matchit-installfor more info
- To add a new matching pair like <>, add
Scrolling
Ctrl+dscroll half page downCtrl+uscroll half page upCtrl+fscroll one page forwardCtrl+bscroll one page backwardCtrl+Mouse Scrollscroll one page forward or backward
Marking frequently used locations
mamark location in the file with variable a (use any of the 26 alphabets)`amove from anywhere in the file to exact location marked by a'awill move to first non-blank character of the line marked by a:markswill show existing marks
d`adelete upto character marked by a- marks can be paired with any command that accept movements like
d,c,y
- marks can be paired with any command that accept movements like
Jumping back and forth
This is helpful if you are moving around while editing a large file or moving between different buffers
Ctrl+onavigate to previous location in jump list (can remember it by thinking o as old)Ctrl+inavigate to next location in jump list (remember by noting that i and o are next to each other in most keyboards)g;go to previous change locationg,go to newer change location:h jump-motionsfor more info
Related
gito place the cursor at same position where it was left last time in Insert mode
Numbers
Ctrl+aincrement a number (decimal/octal/hex will be automatically recognized - octal is a number prefixed by0and hex by0x)Ctrl+xdecrement a number
Multiple copy-paste using " registers
One can use lowercase alphabets a-z to save content for future use. And append content to those registers by using corresponding uppercase alphabets A-Z at later stage
"ayycopy current line to"aregister"appaste content from"aregister"Ayjappend current line and line below it to"aregister ("ahas total 3 lines now)"cyiwcopy word under cursor to"cregister
Special registers
Vim has special purpose registers with pre-defined behavior
"all yanked/deleted text is stored in this register.pcommand can also be invoked as""p"0yanked text is stored in this register- Useful for this sequence: yanking content, deleting something and then pasting yanked content using
"0p
- Useful for this sequence: yanking content, deleting something and then pasting yanked content using
"1to"9deleted contents are stored in these registers and get shifted with each new deletion."1has the most recently deleted content"2ppaste content deleted before the last deletion
"+this register stores system clipboard contentsgg"+yGcopy entire file contents to clipboard"+ppaste content from clipboard
"*this register stores visually selected text- contents of this register can be pasted using middle mouse button click or
"*p
- contents of this register can be pasted using middle mouse button click or
Further reading
Combining editing commands with movement commands
dGdelete from current line to end of filedggdelete from current line to beginning of filed`adelete from current character upto location marked by ad%delete upto matching pairs() {} []cedelete till end of word and change to Insert modeylcopy character under cursord)delete upto end of sentence in forward direction
Context editing
We have seen movement using w,%,f etc. They require precise positioning to be effective. Vim provides a way to modify commands that accepts movement like y,c,d to recognize context. These are i and a text object selections - easy way to remember their subtle differences is to think of i as inner and a as around
diwdelete a word regardless of where the cursor is on that word. Equivalent to usingdewhen cursor is on first character of the worddiWdelete a WORD regardless of where the cursor is on that worddawdelete a word regardless of where the cursor is on that word and a space character to left/right of the word depending on its position as part of sentencedisdelete a sentence regardless of where the cursor is on that sentenceyascopy a sentence regardless of where the cursor is on that sentence and a space left/rightcipdelete a paragraph regardless of where the cursor is on that paragraph and change to Insert modedi"delete all characters within pair of double quotes, regardless of where cursor is within quotesda'delete all characters within pair of single quotes as well as the quotesci(delete all characters within () and change to Insert mode. Works even if matching parenthesis are spread over multiple linesya}copy all characters within {} including the {}. Works even if matching braces are spread over multiple lines
For more info :h text-objects
Miscellaneous
gfopen the file pointed by file path under the cursor:h gfand:h 'suffixesadd'for more info
*searches the word under the cursor in forward direction - matches only the whole wordshift+left mouse clickcan also be used in gvim
g*searches the word under the cursor in forward direction - matches as part of another word also#searches the word under the cursor in backward direction - matches only the whole wordg#searches the word under the cursor in backward direction - matches as part of another word alsoCtrl+gdisplay file information like name, number of lines, etc at bottom of screen- for more info and related commands, see
:h CTRL-G
- for more info and related commands, see
Jjoin current and next line with one space character in betweengJjoin current and next line without any character in between3Jjoin current and next two lines with space in between the lines~invert the case of character under cursor, i.e lowercase becomes UPPERCASE and vice versag~followed by motion command to invert case of those charactersgufollowed by motion command to change case of those characters to lowercase- example:
gue,gu$,guiwetc
- example:
gUfollowed by motion command to change case of those characters to UPPERCASE- example:
gUe,gU$,gUiwetc
- example:
=followed by motion command to indent code- example:
=},gg=Getc
- example: