Vim Cheatsheet

Today’s post is not really a post. It is an attempt to create a workable Vim cheat-sheet. I know that there hundreds vi/vim chat-sheets online, some better than other but naturally none of them is complete and includes all the commands that I care about.

By far the most popular sheet is probably this one which actually hangs over my desk. It is a very good one and I use it all the time to remind myself certain key-strokes. Still, it is not very comprehensive since you just can’t physically fit all the useful key-strokes in that format. I decided to organize mine into sections rather typographical groupings. The result is below.

Please note that I skipped the obvious stuff like ‘hjkl’ and some of the advanced stuff like marking, folding, and register operations. That’s because I don’t really use these commands that often, and furthermore a lot of Vi emulators (like the one in Komodo Edit) do not support them.

Your mission, should you choose to accept it is to add your favorite commands and/or indispensable shortcuts which I have omitted. And yes, I omitted a lot here. There are just to many things to list – this is why I’m relying on you guys!

Movements

w – next word
W – next white space delimited word
b – previous word
B – previous space delimited word
e – end of word
E – end of white space delimited word
0 or ^ – to the physical beginning of the line
$ – to the physical end of the line (ie. the newline char)
g0 – to the beginning of the virtual screen line
gm – to the middle of virtual screen line
g$ – to the end of the virtual screen line
gk – up screen line
gj – down screen line
fchar – till the next occurrence of character ‘char’ (inclusive)
Fchar – till the previous occurrence of character ‘char’ (inclusive)
tchar – till the previous occurrence of character ‘char’ (exclusive)
Tchar – till the previous occurrence of character ‘char’ (exclusive)
G – end of file
gg – start of file
gd – go to the definition (first occurrence of the word under cursor)
{} – begging/end of paragraph
() – begging/end of sentence
[{ ]} – beginning/end of a code block
[[ ]] – beginning/end of a method
[* ]* – beginning/end of a comment block
% – find next open block char ({[ and jump to it’s matching close char
‘. – go to the line with the latest change
* – search to the next occurrence of the word under cursor
# – search to previous occurrence of the word under cursor
/pattern – search to next occurrence of “pattern”
?pattern – search to previous occurrence of “pattern”
n – repeat last search (*,#,/,?) forward
N – repeat last search (*,#,/,?) backward

In the examples below I indicate a movement specified above as mov

Entering Insertion Mode

i – insert at the cursor
I – insert at the beginning of the line
gI – insert in column 1 of the line
a – append after the cursor
A – append at the end of the line
o – open a blank line below cursor
O – open a blank line above cursor
r – replace character under cursor and exit insert mode
R – insert at the cursor in overstrike mode
cmov – delete (change) movement and enter insert mode
cc – replace entire line
C – delete (change) line and enter insert mode
s – delete character under cursor and enter insert mode
S – delete line under cursor and enter insert mode

Deleting

x – delete char under cursor
X – delete char before cursor
dmov – delete range of movement m
dd – delete current line (text moves up)
D – delete to the end of line
J – join the line below to the current
gJ – append the line below to the end of current line

Block Modifiers

When dealing with blocks of text delimited by parentheses (), brackets [], braces {} or <> you can apply the command to either the contents of the block or the whole thing by following it with:

iblk – inner block (everything inside braces, parenthesis etc..)
ablk – all (whole parenthesized block)

Where ‘blk’ is ), ], } or >

Copy/Paste

yy – copy current line
ymov – copy movement m
p – paste after cursor
P – paste before cursor
vipy – visual interior paragraph yank – copy the current paragraph

Visual Mode
v – enter visual mode highlighting 1 char at a time
V – enter visual mode highlighting 1 line at a time
Ctrl+V – enter visual block mode
aw – highlight word
as – highlight sentence
ap – highlight paragraph
ab – highlight a parenthesized block
aB – highlight a {} blocks
ib – highlight contents of a parenthesized block
iB – highlight contents of a {} block

Special

< > – indent left/right
. – repeat last command
u – undo last command
U – undo all changes on the current line
Ctrl+R – redo last undo
Ctrl+O – jump outwards (up-back) to the previous cursor position
Ctrl+I – jump inwards (down-forward) to the previous cursor position

Completion

Ctrl+X – enter completion mode
Ctrl+N – auto complete word in insert and completion mode
Ctrl+P – auto complete word in insert and completion mode
Ctrl+L – auto complete whole line in completion mode
Ctrl+K – dictionary completion in completion mode
Ctrl+F – filename completion in completion mode

Useful Tricks

g~mov – toggle uppercase/lowercase on the range of ‘mov’
gumov – lowercase the range of ‘mov’
guu – lowercase the current line
gUmov – uppercase the range of ‘mov’
gUU – upercase the whole line
g?mov – rot13 on the range of ‘mov’

Regexp Replace

:ranges/foo/bar/arg – replace foo with bar in ‘range’ with

Values of ‘range’:

% – whole file
number – that particular line
none – apply to current line only

Values of ‘arg’:

none – apply to first occurrence
g – global (all occurrences)
i – ignore case
I – don’t ignore case
c – confirm each substitution
p – print the last line containing substitution
e – ignore errors

Macros

qchar – start recording macro storing it in register ‘char’
q – end recording
@char – replay the macro stored in ‘char’

That’s all I have for now. I will update this page with any useful suggestions. If we have enough perhaps I will take this, re-format it nicely and generate some sort of nice looking single sheet PDF that you could hang in your cube or above your fireplace or whatnot.

This entry was posted in Uncategorized. Bookmark the permalink.



19 Responses to Vim Cheatsheet

  1. Steve CANADA Mozilla Firefox Windows Terminalist says:

    You know, the few times I have to do something more than modify or insert a line in vi, I usually ftp to my workstation and use Ultraedit, then ftp back. VI is way too ridiculous.

    Reply  |  Quote
  2. Luke Maciak UNITED STATES Mozilla Firefox Ubuntu Linux Terminalist says:

    Oh, yeah – I do that too. I SFTP and then use gVim. Don’t particularly like using Vim on the console over SSH connection:

    a. the remote machine doesn’t have my .vimrc so I lose all my settings
    b. the arrow keys and backspace usually don’t work the way I’m used to
    c. the console fonts are to big/small for comfortable editing
    d. lag can be supper annoying when you are trying to edit something

    It’s just that I use vim as my primary text editor both on windows and on Linux. In fact I’m typing this very comment in vim using the itsalltext plugin. :)

    Reply  |  Quote
  3. mcai8sh4 UNITED KINGDOM Opera Linux Terminalist says:

    Great idea! I’ve used vim on and off for a couple of years, but only recently (mainly due to your preachings) been forcing myself to use it by default. In the short space of time that I’ve been using it as my primary text editor, I’ve descovered LOTS of features that now I wonder how I coped without. For those who wish get into it, I’d certainly recommend “vimtutor”, I still run through that occasionally to refresh the old grey matter.
    A neat, comprehensive cheat sheet, compiled by ordinary users would prove invaluable to those of us still scratching the surface of this powerful editor. Thanks!
    On another note… great blog, always amuses/informs/educates. Keep it up.

    Reply  |  Quote
  4. IceBrain PORTUGAL Mozilla Firefox Debian GNU/Linux Terminalist says:

    I using Vim too (from Emacs) and I really like it, I can’t understand the people who try not to use the keyboard for anything besides the minimum indispensable. It’s just faster and fluent, but no, just keep wasting four hours a week doing 30 mouse movements to erase a simple line.

    But I still haven’t found a sheet for VIm so good as this for VImperator: http://dl.getdropbox.com/u/263140/cheats.html
    It’s really well organized and easy to read.

    Reply  |  Quote
  5. ths UNITED KINGDOM Mozilla Firefox Windows Terminalist says:

    > % – next occurrence of the character under cursor

    this is strange, I learnt % as for showing matching () [] {} which is very nice in complicated boolean expressions or nested blocks.

    I was used to Emacs at the university (loved it for email, nntp, ftp and C programming) but in industry noone bothers to install and support a bloated monster like Emacs, they all stay at stock OS provided pieces like good ole vi. I’m now quite used to vi, it’s convenient and efficient, but has a steep learning curve. At unix shell I still prefer emacs mode ;)

    Reply  |  Quote
  6. Afton CANADA Mozilla Firefox Ubuntu Linux says:

    Thanks for the review of a couple of these (I’d forgotten that you can rot13 sections of a file!). Two minor nits:

    1. ‘%’ This matches (by default) only the paren-like things, plus some C patterns (comments and preprocessor conditionals). Of course it can be expanded with matchit, but I don’t think it ever does what you describe (“% – next occurrence of the character under cursor”).

    2. As a poster on reddit pointed out, ‘[*’ is matched with ‘]*’, not ‘*]’.

    Other than that, nice work, thanks for the post.

    Reply  |  Quote
  7. Luke Maciak UNITED STATES Mozilla Firefox Ubuntu Linux Terminalist says:

    I fixed it as per the suggestions. See, this is why I put it here first. So that people can pick it apart and make me fix all the obvious errors. :)

    Reply  |  Quote
  8. k00pa FINLAND Mozilla Firefox Mac OS says:

    Thanks! Good cheatsheet very useful.

    I like vim, but only problem with it is that it is too difficult to copy paste text. I just havent used it enough…

    But Vim is the best editor!

    Reply  |  Quote
  9. Luke Maciak UNITED STATES Mozilla Firefox Ubuntu Linux Terminalist says:

    @k00pa: It’s really not that difficult though. If you are pasting between vim buffers doing y+p thing usually works.

    When you are pasting from somewhere else (eg firefox) you can usually do it with the middle mouse button. :)

    Reply  |  Quote
  10. Afton CANADA Mozilla Firefox Ubuntu Linux says:

    @Luke, k00pa

    Instead of using the middle mouse button, which depends a lot on your gui setup (outside of (g)vim), just know that the default buffers differ from system to system. On Windows, the default buffer (when you copy/paste) is the ‘*’ buffer, and on linux (and probably OS X) it’s the ‘+’ register.

    So to copy something from another program, you copy it into the default register as usual (ctrl-c), then paste it in vim with "*p or "+p, depending. Ditto in reverse, you use "*y or "+y to yank into these buffers so that you can paste into another program with the standard ‘paste’ commands (usually ctrl-v).

    You can copy to any of the a-z registers as well, but they will be inaccessible outside of vim. See :h reg for more.

    </pedantic>

    Reply  |  Quote
  11. astine UNITED STATES Google Chrome Windows says:

    @@ – previous macro
    :s/pattern/replace/ – search and replace
    :g/pattern/ command – modify by line

    Reply  |  Quote
  12. Kenny CANADA Mozilla Firefox Mac OS says:

    Not related to the article, but to the comments on remote editing.

    Instead of all the sftp-ing, you might be interested in setting up FUSE and using sshfs to access those files. Allows you to mount remote directories and edit the files like they were local.

    Reply  |  Quote
  13. Luke Maciak UNITED STATES Mozilla Firefox Windows Terminalist says:

    @astine: Nice! Thanks!

    @Kenny: Actually I use KDE so I can just type in sftp:// in the address bar and I can edit files as if they were on my local file system.

    Reply  |  Quote
  14. alexertech VENEZUELA Mozilla Firefox SuSE Linux says:

    Hey, nice cheatsheet… Directly to my printer. I like to have this kind of things in paper :D

    Thanks again! ;)

    Reply  |  Quote
  15. vacri AUSTRALIA Mozilla Firefox Debian GNU/Linux says:

    That cheatsheet is better than this mug which is currently sitting on my desk. Now if only the printer worked… damn contract printers.

    And I don’t even drink coffee…

    Reply  |  Quote
  16. freelancer SWEDEN Mozilla Firefox Windows Terminalist says:

    I’m confused. I got an e-mail about a new comment on this article, but I haven’t commented (and therefor not subscribed to updates). I’m not complaining, I’m just…confused…

    Reply  |  Quote
  17. Luke Maciak UNITED STATES Mozilla Firefox Ubuntu Linux Terminalist says:

    @freelancer: Actually, I think it happened before to someone else too. I don’t think I ever figured out why. I think there is a glitch in the notification plugin and sometimes people who commented in the past get notifications for recent threads they did not subscribe to. It happens very rarely though.

    I’ll go an check if there is a newer version of the plugin available out there. Maybe they fixed it by now. :P

    Reply  |  Quote
  18. mcai8sh4 UNITED KINGDOM Opera Linux Terminalist says:

    I’ve just realised another handy little thing that should be on this cheat sheet. I seem to use this quite a bit – so maybe others will find it useful.
    When searching and you want to ignore case, throw in ‘\c’ and the search will ignore the case ie.
    /foo
    will only find 'foo' (NOT Foo or foO etc)
    but throw a '\c' in there...
    /\cfoo
    will find foo, Foo, FOo, FOO, fOo.. you get the picture

    Reply  |  Quote
  19. BC UNITED STATES Mozilla Firefox Windows says:

    I’m not a coder, but I love Vim for general writing.
    If I’m not too late to join the party:

    My all-time favorite command, which I don’t think is mentioned in Vimtutor, though it should be, is:
    gf (go to file?)
    Just hit that when the cursor is on the name of an existing file, and it’ll open it. Voilà, kind of a quick and dirty wiki. When I’m using Linux, I make a bunch of files with no extension, as “wiki words”, and keep them in the same directory. Well, it works in Windows too, though I dislike creating files without an extension in Windows, since the OS is too dumb to know what they are without an extension.
    In Cream, shift+Enter does the same thing, and will also open URLs in your browser. (I think gf only opens URLs for editing, using wget)

    I also have a problem when I have to use Vim without (one of my various clumsy attempts at) a .vimrc .

    Thanks for the cheat sheet — I liked the ones for visual mode especially.
    For some reason, I prefer to use right parentheses with visual to select a sentence, rather than as.

    Reply  |  Quote

Leave a Reply

Your email address will not be published. Required fields are marked *