Basics
Vim has 3 modes (Normal Mode, Insert Mode, Line Mode). When you enter into Vim, you’re automatically placed into Normal mode, also known as Command mode.
From Normal mode, press ‘i’ to go into insert mode to enter text content. Press ESC to go back into Normal mode.
Press ‘:’ to go into Line mode from Normal mode. Press ESC to go back into Normal mode. Use ‘q’ from line mode to quit Vim. If you have made changes to the currently loaded file, it will ask you to save changes. You can use ‘q!’ to quit without saving the changes. You can use ‘wq’ to save the changes and quit at the same time.
Normal Mode
This mode is intended mostly for navigating and using commands to modify the file.
Navigation
These are the same as using the arrow keys.
j
- move down a linek
- move up a linel
- move to the righth
- move to the left
Jump between words
w
- move to next wordW
- move to next word (ignore punctuation)b
- move to previous wordB
- move to previoius word (ignore punctuation){num}w
- Moves forward {num} words{num}j
- Moves {num} lines down
Jumping to lines
gg
- Jump to beginning of fileG
- Jump to end of file- Line number followed by ‘gg’ or ‘G’ (e.g. ‘22g’ or ‘22G’)
Jumping on a line
0
- Move to beginning of a line^
- Move to beginning of a line$
- Move to end of a linef{char}
- Place cursor in first occurance of {char} to the rightF{char}
- Place cursor in first occurance of {char} to the leftt{char}
- Place cursor one character before the first occurance of {char} to the rightT{char}
- Place cursor one character before the first occurance of {char} to the left;
- Repeats the last motion
These are the same as using Page-Up and Page-Down keys, if present.
- CTRL-F - move forward / page down
- CTRL-B - move backwards / page up
Jumping Sentences / Paragraphs
(
- Move to the next sentence)
- Move to the previous sentence{
- Move to the next paragraph}
- Move to the previous paragraph
Status Line
- CTRL-g - display line with current status (file name, status, cursor position, total lines)
g
+ CTRL-g - display line with columns, lines, words, and bytes
Shift viewport
- z-ENTER - Moves text at current cursor up
Modification
x
- Delete (Cut) character under cursorX
- Delete (Cut) character before cursor (to the left){num}x
- Deletes {num} charactersdw
- Delete (Cut) current worddd
- Delete current linedl
- Deletes the current characterdh
- Deletes character before cursor (to the left)dj
- Deletes current line, and the one below itdk
- Deletes current line, and the one above itd0
- Deletes from cursor to beginning of lined\$
- Deletes from cursor to end of the linedG
- Deletes everything from the current line till the end of the fileD
- Deletes from cursor to end of the liney
- Yank (copy) current characteryy
- Yank (copy) current lineyw
- Yank (copy) current wordy0
- Yank (copy) from cursor to beginning of liney\$
- Yank (copy) from cursor to end of the linep
- Paste last deleted/yanked content after cursorP
- Paste last deleted/yanked content before cursoru
- Undo- CTRL-R - Redo
- . - Issues the previous command
Line Mode
These commands begin with ‘:’ to go into line mode, followed by ENTER.
wq
- write quitq
- quitq!
- quit without saving0-100000000
- moves to line number specifiedset ruler
- display ruler
If you type a partial command, then press CTRL-D, you’ll be shown a list of suggested commands.
Help
You can access the help by running ‘help’ or ‘h’ from the line mode. You can also try to jump to a specific topic by typing a blurb after this command (e.g. ‘:h dd’ jumps to ‘dd’ command).
If you place your cursor over a help topic, you can press CTRL + ] to go into that topic. Press CTRL + o to go back to your previous location.
Searching
Taken from Finding a Word in Vi/Vim.
To find a word in Vi/Vim, simply type the / or ? key, followed by the word you’re searching for.
Once found, you can press the n key to go directly to the next occurrence of the word.
Vi/Vim also allows you to launch a search on the word over which your cursor is positioned. To do this, place the cursor over the term, and then press * or # to look it up.