I’ve compiled a list of essential Vim commands that I use every day. I have then given a few instructions on how to make Vim as great as it should be, because it’s painful without configuration.
- To avoid having to update this page for every new version, there are links to the directories. From there select the files you want to download. In the file names ## stands for the version number. For example, vim##src.zip with version 8.2 is vim82src.zip and vim-##-src.tar.gz for version 8.2 is vim-8.2-src.tar.gz. Links are provided for quick.
- .visual.txt. For Vim version 7.3. Last change: 2010 Feb 17 VIM REFERENCE MANUAL by Bram Moolenaar Visual mode. Visual. Visual-mode. visual-mode. Visual mode is a flexible and easy way to select a piece of text for an operator.
- In short press the Esc key and then press Shift + G to move cursor to end of file in vi or vim text editor under Linux and Unix-like systems. However, you can use the following keyboard shortcuts too.
- 2021-04-17 spotdiff.vim: A range and area selectable diffthis to compare partially (4.0) Added a group of commands to select the Visual area to be compared in non-diff normal mode. Rick Howe 2021-04-16 diffchar.vim: Highlight the exact differences, based on characters and words (8.9) Fixed not to overrule syntax highlighting.Rick Howe.
Essentials
To check, run vim -version and see if +clipboard exists. If it says -clipboard, you will not be able to copy from outside of Vim. For Mac users, homebrew install Vim with the clipboard option. Install homebrew and then run brew install vim.
Cursor movement (Normal/Visual Mode)
h
j
k
l
- Arrow keysw
/b
- Next/previous wordW
/B
- Next/previous word (space seperated)e
/ge
- Next/previous end of word0
/$
- Start/End of line^
- First non-blank character of line (same as0w
)
Editing text
i
/a
- Start insert mode at/after cursorI
/A
- Start insert mode at the beginning/end of the lineo
/O
- Add blank line below/above current lineEsc
orCtrl+[
- Exit insert moded
- Deletedd
- Delete linec
- Delete, then start insert modecc
- Delete line, then start insert mode
Operators
- Operators also work in Visual Mode
d
- Deletes from the cursor to the movement locationc
- Deletes from the cursor to the movement location, then starts insert modey
- Copy from the cursor to the movement location>
- Indent one level<
- Unindent one level- You can also combine operators with motions. Ex:
d$
deletes from the cursor to the end of the line.
Marking text (visual mode)
v
- Start visual modeV
- Start linewise visual modeCtrl+v
- Start visual block modeEsc
orCtrl+[
- Exit visual mode
Vim Move Line Down Mac
Clipboard
yy
- Yank (copy) a linep
- Paste after cursorP
- Paste before cursordd
- Delete (cut) a linex
- Delete (cut) current characterX
- Delete (cut) previous characterd
/c
- By default, these copy the deleted text
Exiting
:w
- Write (save) the file, but don’t quit:wq
- Write (save) and quit:q
- Quit (fails if anything has changed):q!
- Quit and throw away changes
Search/Replace
Vim Page Down Mac Pro
/pattern
- Search for pattern?pattern
- Search backward for patternn
- Repeat search in same directionN
- Repeat search in opposite direction:%s/old/new/g
- Replace all old with new throughout file (gn is better though):%s/old/new/gc
- Replace all old with new throughout file with confirmations
General
u
- UndoCtrl+r
- Redo
Advanced
Cursor movement
Ctrl+d
- Move down half a pageCtrl+u
- Move up half a page}
- Go forward by paragraph (the next blank line){
- Go backward by paragraph (the next blank line)gg
- Go to the top of the pageG
- Go the bottom of the page: [num] [enter]
- Go to that line in the documentctrl+e / ctrl+y
- Scroll down/up one line
Character search
f [char]
- Move forward to the given charF [char]
- Move backward to the given chart [char]
- Move forward to before the given charT [char]
- Move backward to before the given char;
/,
- Repeat search forwards/backwards
Editing text
J
- Join line below to the current oner [char]
- Replace a single character with the specified char (does not use Insert mode)
Visual mode
O
- Move to other corner of blocko
- Move to other end of marked area
File Tabs
:e filename
- Edit a file:tabe
- Make a new tabgt
- Go to the next tabgT
- Go to the previous tab:vsp
- Vertically split windowsctrl+ws
- Split windows horizontallyctrl+wv
- Split windows verticallyctrl+ww
- Switch between windowsctrl+wq
- Quit a window
Marks
- Marks allow you to jump to designated points in your code.
m{a-z}
- Set mark {a-z} at cursor position- A capital mark {A-Z} sets a global mark and will work between files
'{a-z}
- Move the cursor to the start of the line where the mark was set'
- Go back to the previous jump location
Text Objects
- Say you have
def (arg1, arg2, arg3)
, where your cursor is somewhere in the middle of the parenthesis. di(
deletes everything between the parenthesis. That says “change everything inside the nearest parenthesis”. Without text objects, you would need to doT(dt)
.
General
.
- Repeat last commandCtrl+r + 0
in insert mode inserts the last yanked text (or in command mode)gv
- reselect (select last selected block of text, from visual mode)%
- jumps between matching()
or{}
Vim is quite unpleasant out of the box. It’s an arcane experience:
- Autocomplete is missing
- System clipboard is not used
- Act of typing
:w
to save is cumbersome - Mouse doesn’t work
- Management of multiple files is tricky
- Ability to indent multiple lines is missing
It does have a significant strength though: your fingers can stay on the main keyboard keys to do most editing actions. This is faster and more ergonomic. I find that the toughest part about VIM is guiding people towards getting the benefits of VIM without the drawbacks. Here are two ideas on how to go about this.
Switch caps lock and escape
- I highly recommend you switch the mapping of your caps lock and escape keys. You’ll love it, promise! Switching the two keys is platform dependent.
Visual Studio Code
- VSCode is the simplest way to give you a fantastic editor that also gives you the benefits of VIM. Just install the VIM extension.
- I made a few slight changes which improved the experience for me.
Configure native VIM
For all the given limitations, you’ll need to find a solution. You can either solve the issues one by one, or you can use a reference .vimrc settings file that fix most of the issues out-of-the-box.
- My .vimrc file could be a good starting point. Honestly, it’s a bit old and not the best. I now use VSCode mainly so I haven’t kept a great vimrc.
Using the system clipboard
'+y
copy a selection to the system clipboard'+p
paste from the system clipboard- If this doesn’t work, it’s probably because Vim was not built with the system clipboard option. To check, run
vim --version
and see if+clipboard
exists. If it says-clipboard
, you will not be able to copy from outside of Vim.- For Mac users, homebrew install Vim with the clipboard option. Install homebrew and then run
brew install vim
.- then move the old Vim binary:
$ mv /usr/bin/vim /usr/bin/vimold
- restart your terminal and you should see
vim --version
now with+clipboard
- then move the old Vim binary:
- For Mac users, homebrew install Vim with the clipboard option. Install homebrew and then run
Sublime Text
- Another option is to use Vintageous in Sublime Text (version 3). This gives you Vim mode inside Sublime. I suggest this (or a similar setup with the Atom editor) if you aren’t a Vim master. Check out Advanced Vim if you are.
- Vintageous is great, but I suggest you change a few settings to make it better.
- Clone this repository to
~/.config/sublime-text-3/Packages/Vintageous
, or similar. Then check out the “custom” branch.- Alternatively, you can get a more updated Vintageous version by cloning the official repository and then copying over this patch.
- Change the user settings (
User/Preferences.sublime-settings
) to include:'caret_style': 'solid'
- This will make the cursor not blink, like in Vim.
- Sublime Text might freeze when you do this. It’s a bug; just restart Sublime Text after changing the file.
ctrl+r
in Vim means “redo”. But there is a handy Ctrl + R shortcut in Sublime Text that gives an “outline” of a file. I remapped it to alt+r by putting this in the User keymap{ 'keys': ['alt+r'], 'command': 'show_overlay', 'args': {'overlay': 'goto', 'text': '@'} },
- Mac users: you will not have the ability to hold down a navigation key (like holding j to go down). To fix this, run the commands specified here: https://gist.github.com/kconragan/2510186
- Clone this repository to
- Now you should be able to restart sublime and have a great Vim environment! Sweet Dude.
Other
I don’t personally use these yet, but I’ve heard other people do!
:wqa
- Write and quit all open tabs (thanks Brian Zick)
Additional resources
- Practical Vim is a fantastic resource on many of the useful hidden features of vim.
How to Exit
:q[uit] | Quit Vim. This fails when changes have been made. |
:q[uit]! | Quit without writing. |
:cq[uit] | Quit always, without writing. |
:wq | Write the current file and exit. |
:wq! | Write the current file and exit always. |
:wq {file} | Write to {file}. Exit if not editing the last |
:wq! {file} | Write to {file} and exit always. |
:[range]wq[!] | [file] Same as above, but only write the lines in [range]. |
ZZ | Write current file, if modified, and exit. |
ZQ | Quit current file and exit (same as ':q!'). |
Editing a File
:e[dit] | Edit the current file. This is useful to re-edit the current file, when it has been changed outside of Vim. |
:e[dit]! | Edit the current file always. Discard any changes to the current buffer. This is useful if you want to start all over again. |
:e[dit] {file} | Edit {file}. |
:e[dit]! {file} | Edit {file} always. Discard any changes to the current buffer. |
gf | Edit the file whose name is under or after the cursor. Mnemonic: 'goto file'. |
Inserting Text
a | Append text after the cursor [count] times. |
A | Append text at the end of the line [count] times. |
i | Insert text before the cursor [count] times. |
I | Insert text before the first non-blank in the line [count] times. |
gI | Insert text in column 1 [count] times. |
o | Begin a new line below the cursor and insert text, repeat [count] times. |
O | Begin a new line above the cursor and insert text, repeat [count] times. |
Inserting a file
Vim Page Down Machine
:r[ead] [name] | Insert the file [name] below the cursor. |
:r[ead] !{cmd} | Execute {cmd} and insert its standard output below the cursor. |
Deleting Text
<Del> or x | Delete [count] characters under and after the cursor |
X | Delete [count] characters before the cursor |
d{motion} | Delete text that {motion} moves over |
dd | Delete [count] lines |
D | Delete the characters under the cursor until the end of the line |
{Visual}x or {Visual}d | Delete the highlighted text (for {Visual} see Selecting Text). |
{Visual}CTRL-H or {Visual} | When in Select mode: Delete the highlighted text |
{Visual}X or {Visual}D | Delete the highlighted lines |
:[range]d[elete] | Delete [range] lines (default: current line) |
:[range]d[elete] {count} | Delete {count} lines, starting with [range] |
Changing (or Replacing) Text
r{char} | replace the character under the cursor with {char}. |
R | Enter Insert mode, replacing characters rather than inserting |
~ | Switch case of the character under the cursor and move the cursor to the right. If a [count] is given, do that many characters. |
~{motion} | switch case of {motion} text. |
{Visual}~ | Switch case of highlighted text |
Substituting
:[range]s[ubstitute]/{pattern}/{string}/[c][e][g][p][r][i][I] [count] | For each line in [range] replace a match of {pattern} with {string}. |
:[range]s[ubstitute] [c][e][g][r][i][I] [count] :[range]&[c][e][g][r][i][I] [count] | Repeat last :substitute with same search pattern and substitute string, but without the same flags. You may add extra flags |
Copying and Moving Text
'{a-zA-Z0-9.%#:-'} | Use register {a-zA-Z0-9.%#:-'} for next delete, yank or put (use uppercase character to append with delete and yank) ({.%#:} only work with put). |
:reg[isters] | Display the contents of all numbered and named registers. |
:reg[isters] {arg} | Display the contents of the numbered and named registers that are mentioned in {arg}. |
:di[splay] [arg] | Same as :registers. |
['x]y{motion} | Yank {motion} text [into register x]. |
['x]yy | Yank [count] lines [into register x] |
['x]Y | yank [count] lines [into register x] (synonym for yy). |
{Visual}['x]y | Yank the highlighted text [into register x] (for {Visual} see Selecting Text). |
{Visual}['x]Y | Yank the highlighted lines [into register x] |
:[range]y[ank] [x] | Yank [range] lines [into register x]. |
:[range]y[ank] [x] {count} | Yank {count} lines, starting with last line number in [range] (default: current line), [into register x]. |
['x]p | Put the text [from register x] after the cursor [count] times. |
['x]P | Put the text [from register x] before the cursor [count] times. |
['x]gp | Just like 'p', but leave the cursor just after the new text. |
['x]gP | Just like 'P', but leave the cursor just after the new text. |
:[line]pu[t] [x] | Put the text [from register x] after [line] (default current line). |
:[line]pu[t]! [x] | Put the text [from register x] before [line] (default current line). |
Undo/Redo/Repeat
u | Undo [count] changes. |
:u[ndo] | Undo one change. |
CTRL-R | Redo [count] changes which were undone. |
:red[o] | Redo one change which was undone. |
U | Undo all latest changes on one line. {Vi: while not moved off of it} |
. | Repeat last change, with count replaced with [count]. |
Moving Around
h or | [count] characters to the left (exclusive). |
l or | [count] characters to the right (exclusive). |
k or CTRL-P | [count] lines upward |
j or CTRL-J or CTRL-N | [count] lines downward (linewise). |
0 | To the first character of the line (exclusive). |
<Home> | To the first character of the line (exclusive). |
^ | To the first non-blank character of the line |
$ or <End> | To the end of the line and [count - 1] lines downward |
g0 or g<Home> | When lines wrap ('wrap on): To the first character of the screen line (exclusive). Differs from '0' when a line is wider than the screen. When lines don't wrap ('wrap' off): To the leftmost character of the current line that is on the screen. Differs from '0' when the first character of the line is not on the screen. |
g^ | When lines wrap ('wrap' on): To the first non-blank character of the screen line (exclusive). Differs from '^' when a line is wider than the screen. When lines don't wrap ('wrap' off): To the leftmost non-blank character of the current line that is on the screen. Differs from '^' when the first non-blank character of the line is not on the screen. |
g$ or g<End&gr; | When lines wrap ('wrap' on): To the last character of the screen line and [count - 1] screen lines downward (inclusive). Differs from '$' when a line is wider than the screen. When lines don't wrap ('wrap' off): To the rightmost character of the current line that is visible on the screen. Differs from '$' when the last character of the line is not on the screen or when a count is used. |
f{char} | To [count]'th occurrence of {char} to the right. The cursor is placed on {char} (inclusive). |
F{char} | To the [count]'th occurrence of {char} to the left. The cursor is placed on {char} (inclusive). |
t{char} | Till before [count]'th occurrence of {char} to the right. The cursor is placed on the character left of {char} (inclusive). |
T{char} | Till after [count]'th occurrence of {char} to the left. The cursor is placed on the character right of {char} (inclusive). |
; | Repeat latest f, t, F or T [count] times. |
, | Repeat latest f, t, F or T in opposite direction [count] times. |
- <minus> | [count] lines upward, on the first non-blank character (linewise). |
+ or CTRL-M or <CR> | [count] lines downward, on the first non-blank character (linewise). |
_ <underscore> | [count] - 1 lines downward, on the first non-blank character (linewise). |
<C-End> or G | Goto line [count], default last line, on the first non-blank character. |
<C-Home> or gg | Goto line [count], default first line, on the first non-blank character. |
<S-Right> or w | [count] words forward |
<C-Right> or W | [count] WORDS forward |
e | Forward to the end of word [count] |
E | Forward to the end of WORD [count] |
<S-Left> or b | [count] words backward |
<C-Left> or B | [count] WORDS backward |
ge | Backward to the end of word [count] |
gE | Backward to the end of WORD [count] |
A word consists of a sequence of letters, digits and underscores, or asequence of other non-blank characters, separated with white space (spaces,tabs,
A WORD consists of a sequence of non-blank characters, separated with whitespace. An empty line is also considered to be a word and a WORD.
( | [count] sentences backward |
) | [count] sentences forward |
{ | [count] paragraphs backward |
} | [count] paragraphs forward |
]] | [count] sections forward or to the next '{' in the first column. When used after an operator, then the '}' in the first column. |
][ | [count] sections forward or to the next '}' in the first column |
[[ | [count] sections backward or to the previous '{' in the first column |
[] | [count] sections backward or to the previous '}' in the first column |
Screen movement commands
z. | Center the screen on the cursor |
zt | Scroll the screen so the cursor is at the top |
zb | Scroll the screen so the cursor is at the bottom |
Marks
m{a-zA-Z} | Set mark {a-zA-Z} at cursor position (does not move the cursor, this is not a motion command). |
m' or m` | Set the previous context mark. This can be jumped to with the '' or '``' command (does not move the cursor, this is not a motion command). |
:[range]ma[rk] {a-zA-Z} | Set mark {a-zA-Z} at last line number in [range], column 0. Default is cursor line. |
:[range]k{a-zA-Z} | Same as :mark, but the space before the mark name can be omitted. |
'{a-z} | To the first non-blank character on the line with mark {a-z} (linewise). |
'{A-Z0-9} | To the first non-blank character on the line with mark {A-Z0-9} in the correct file |
`{a-z} | To the mark {a-z} |
`{A-Z0-9} | To the mark {A-Z0-9} in the correct file |
:marks | List all the current marks (not a motion command). |
:marks {arg} | List the marks that are mentioned in {arg} (not a motion command). For example: |
Searching
/{pattern}[/] | Search forward for the [count]'th occurrence of {pattern} |
/{pattern}/{offset} | Search forward for the [count]'th occurrence of {pattern} and go {offset} lines up or down. |
/<CR> | Search forward for the [count]'th latest used pattern |
//{offset}<CR> | Search forward for the [count]'th latest used pattern with new. If {offset} is empty no offset is used. |
?{pattern}[?]<CR> | Search backward for the [count]'th previous occurrence of {pattern} |
?{pattern}?{offset}<CR> | Search backward for the [count]'th previous occurrence of {pattern} and go {offset} lines up or down |
?<CR> | Search backward for the [count]'th latest used pattern |
??{offset}<CR> | Search backward for the [count]'th latest used pattern with new {offset}. If {offset} is empty no offset is used. |
n | Repeat the latest '/' or '?' [count] times. |
N | Repeat the latest '/' or '?' [count] times in opposite direction. |
Selecting Text (Visual Mode)
To select text, enter visual mode with one of the commands below, and usemotion commands to highlight the text you are interestedin. Then, use some command on the text.
v | start Visual mode per character. |
V | start Visual mode linewise. |
<Esc> | exit Visual mode without making any changes |
How to Suspend
CTRL-Z | Suspend Vim, like ':stop'. Works in Normal and in Visual mode. In Insert and Command-line mode, the CTRL-Z is inserted as a normal character. |
:sus[pend][!] or :st[op][!] | Suspend Vim. If the '!' is not given and 'autowrite' is set, every buffer with changes and a file name is written out. If the '!' is given or 'autowrite' is not set, changed buffers are not written, don't forget to bring Vim back to the foreground later! |