Vim Cheatsheet
Started to learn vim from many years ago and used it on and off from time to time. I still feel it’s hard to master but fun to use. It’s frustrating that I wanted to improve my text editing speed but always stumbled for basic commands. So this time I am not aiming for complex combos, just enjoying the convenience it brings.
Reicepes
This is a section for common vim operations. It is meant to put this section in the end but I have to look back so often so I just move it to the begining for my own convenience.
Write with sudo trick
Allow users to write on a file that needs root permission even when users forgot to open vim with sudo:
|
|
Detailed explaination can be found on stackoverflow.
Fast operations
Ctrl + r(Normal mode)
: redo changes4Ctrl + r(Normal mode)
: redo changes for 4 timesCtrl + r + registername(insert mode)
: paste the current buffer contents"
: last used register%
: name of the current file(Ctrl + r + %
will paste current filename)
"+p
: paste from system clipboard ("+
is the system clipboard register):reg
: access all currently defined registers"ayy
: copy the current line into register a
Moving around
Marks
Similar to the idea of bookmark, a mark in vim allow users to record current curson location with lower case letters and easily go back to recorded location with chosen letter.
ma
: set marka
at current cursor location, mark name can be chosen from[a-z]
'a
: jump to the first non-blank character in line of marka
:marks
: list all the marks
Cursor movements
h
: move one character leftj
: move one row downk
: move one row upl
: move one character right3k
: move two rows up
Word movements
w
: move to beginning of next wordb
: move to previous beginning of worde
: move to end of wordW
: move to beginning of next word after a whitespaceB
: move to beginning of previous word before a whitespaceE
: move to end of word before a whitespace
Fast line movements:
0
: move to the beginning of the line^
: move to first character of the line$
: move to the end of the lineG
: move to the end of the filegg
: move to the beginning of the file99G
: move to the line 9999gg
: move to the line 99
Copy y
yy
: copy current line10yy
: copy 10 linesyw
: copy a wordy$
: copy to the end of current sentenceyfa
: copy between cursor to firsta
charactory2fa
: copy bwtween cursor to seconda
charactor
Cut x
x
: cut a character2x
: cut two charactersxp
: switch two characters
Delete d
dd
: delete current line2dd
: delete two linesdw
: delete a worddfa
: delete from cursor to firsta
charactor
Paste
p
: paste copied and cutted content(from vim register actually)2p
: paste twice
Quote a word
Assume we are using single quotes here: ciw'Ctrl+r"'
ciw
: delete the current word and enter insert mode'
: add the first quoteCtrl+r"
: insert the content of the"
resigter(the last yanked/deleted)'
: add the closing quote
Unquote a word
Assume we are using single quotes: di'hPl2x
di'
: delete the word inside single quoteshp
: move the cursor left for one character and put the deleted content before the quotel
: move the cursor right one character(right on the opeing quote)2x
: delete two quotes
Change case of a word
- visual select a word then
~
- visual select a word then
U
for uppercase oru
for lowercase - cusor on a word then
gUiw
for uppercase orgUaw
for lowercase g~~
to invert case for entire line
Why vim
It’s poweful. Vim enables uses to move around and edit text in a command way, which is more efficient than using mouse. This also introduces complexity, of course.
It’s ubiquitous. The basics remain the same on all text editing tools like vscode, terminal, pycharm, etc. Furthermore, it’s available on all operating systems.
Basics
Concept of modes
If you use normal text editors such as nodepad, there is only one mode: you open it and start type, and that is so nature because everyone use text editor like that. Couterintuitively, vim has 4 modes.
Normal mode: by default, vim starts in normal mode, also known as command mode. In this mode you cannot type characters like in insert mode, instead, what you type becaomes commands for moving and manipulating text.
NoteNormal mode can be accessed from other modes by pressingEsc
.Inert mode: in this mode, you write text as if in normal text editors.
NoteFrom normal mode, use an insert command to enter insert mode.Visual mode: Your movement changes your text highliting. Just like you use mouse to click and drag for a text block selection, but more powerful because you can select text both character-wise or line-wise.
NotePress
v
to enter visual mode then move cursot to select text.Press
V
to enter visual line mode.Press
<ctrl-V>
to enter visual block mode.Ex mode: it’s similar to the command line mode as it allows you to enter Ex commands.
NotePress:
to enter command mode.
Thinking in vim
Like we form a sentence using different kinds of words. Vim is designed to construct operations like a language, more specificly, like a sentence with nouns, verbs, and modifiers.
Verbs
Verbs are the actions that can be performed on nouns.
d
: deletec
: changey
: yank(copy)v
: visually selectV
: visually select in line mode
Modifiers
Modifiers are used before nouns to describe how your verbs work on nouns.
i
: insidea
: aroundt
: search and moves cursor before search termf
: search and moves cursor on serch term
Nouns
Nouns are object you want to work on. Can be a word, a sentence or your selection of text.
w
: words
: sentence
Building operations like a sentence
Think about how we used to do text editing, for example, changing a word to another: we use mouse to double click the word or drag cursor from beginning to end of the word, delete it and then start typing a new word.
In vim, we acheive this in a different way: In normal mode, move cursor to the word and type ciw
, then start typeing new word. The key is having what operation in mind and translate that operation to a “language” construct by verb, modifier, and noun. It sounds complicated and indeed it is, but once you onvercome the learning curve than text editing becomes much faster than normal way.
The learning curve is there and nobody can remember all this in just a glance. I’ll note receipes for common tasks for my own references.