To brush up beginner knowledge: run vimtutor
a.k.a. I hate vim but I'm forced to use it:
Everything here starts in "normal mode"!
- some commands bring you in "insert mode"
- Press
i
to insert text before cursor, insert some text, finish withESC
- Press
- colon brings you to "command mode"
- enter
:w<enter>
to save file :q<enter>
to quit without saving:q!<enter>
to quit, abandoning changes
- enter
- If unsure which mode your're in,
ESC
brings you back to normal mode
hjkl
character-by-character^f
^b
page-wise up/downG
gg
bottom/top line
Poisoned fruit: you can use arrow keys instead of hjkl
. Avoid them!
-
we have already seen
i
-
To insert at the end of line:
A
-
x
andX
to delete char after/before cursor -
dd
to delete whole line -
J
to join line to next one
That's it, the bare minimum to get things done.
u
andC^R
are undo/redo, but only one step unless configured:help undo
D
/C
deletes/changes from cursor to end of line
This feature is closest to the "clipboard":
Yanking saves text in a "register". Deleting saves the deleted text in a register.
y
+ motion,d
+ motion.yy
anddd
yank/delete a linep
/P
paste the yanked/deleted text- before/after the cursor
- when yanked/deleted line-wise, on previous/next line
v
,V
(linewise),C^V
(block-wise)- then motions to move the boundary
o
(andO
in block-wise) to switch boundary- finally, a command like
c
,d
,y
(insert/delete/yank). OrESC
.
:help motion.txt
will blow your mind!
- "find":
f<letter>
andF<letter>
move to next/prev<letter>
on line - "till": same with
t
andT
, but stop right before letter w
b
move to beginning of next /end of previous wordW
B
same fo WORDs (white-space separated text)H
M
L
jump to top/middle/bottom of current screen of text%
jumps to matching bracket (of any kind)<number>G
go to this line0
and$
go to beginning (non-whitespace) and end of line(
)
sentence forward/back[
]
paragraph forward/back/
regular-expression search (vim has its own flavour!)
yy
cc
dd
are line-wise
visual_editing_operation: v (motion)* operator
editing-operation: ([register] [repetition] operator [force] [repetition] (motion | text-object)) | visual-editing-operation
text-object: object | (a or i)
- Register: for yanking and pasting, which register to use instead of default.
:help registers
- Repetition: how many times
- Force:
v
V
C^V
force character-, line-, blockwise mode a
is "around",i
"inside" mode- for text objects defined by delimiters: with vs. without them
- for all others: with or without trailing whitespace
Editing operations can be repeated with the powerful and simple .
.
Typical:
- do a search
- do editing operation
- repeat search
- decide whether to apply op here too
- repeat at (2) or (3)
:help text-objects
These are the objects (without leading a
/i
)
w
W
s
p
words WORDs sentences paragraphs- text quoted in single, double quotes or backticks
- brackets (all four kinds)
- SUPER USEFUL in programming b/c nesting aware
t
HTML/XML tags
In visual mode, text objects (with a
/i
) extend the selection.
:set [no]<feature>
sets/unsets flags:
- feature=
number
: line numbering - feature=
hls
|hlsearch
search result highighting - feature=
paste
past mode: disables some syntax rules for pasting as-is from clipboard
C^R"
paste previous yank (unless line-wise)- example:
ce(C^R")ESC
wraps word in brackets
- example:
C^N
auto-completion from several sourcesC^N
/C^P
... then<enter>
if menu pops up
C^XC^L
completes line: massive time-saver!C^XC^O
"omni-completion" (needs language integration)C^XC^F
complete file path
- to get a comprehensive understanding of vim:
:help user-manual
- to get a good overview of features and discover more motions and bindings, check out this awesome cheat sheet