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
ito 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,
ESCbrings you back to normal mode
hjklcharacter-by-character^f^bpage-wise up/downGggbottom/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 -
xandXto delete char after/before cursor -
ddto delete whole line -
Jto join line to next one
That's it, the bare minimum to get things done.
uandC^Rare undo/redo, but only one step unless configured:help undo
D/Cdeletes/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.yyandddyank/delete a linep/Ppaste 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(andOin 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
tandT, but stop right before letter wbmove to beginning of next /end of previous wordWBsame fo WORDs (white-space separated text)HMLjump to top/middle/bottom of current screen of text%jumps to matching bracket (of any kind)<number>Ggo to this line0and$go to beginning (non-whitespace) and end of line()sentence forward/back[]paragraph forward/back/regular-expression search (vim has its own flavour!)
yyccddare 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:
vVC^Vforce character-, line-, blockwise mode ais "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)
wWspwords WORDs sentences paragraphs- text quoted in single, double quotes or backticks
- brackets (all four kinds)
- SUPER USEFUL in programming b/c nesting aware
tHTML/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|hlsearchsearch result highighting - feature=
pastepast mode: disables some syntax rules for pasting as-is from clipboard
C^R"paste previous yank (unless line-wise)- example:
ce(C^R")ESCwraps word in brackets
- example:
C^Nauto-completion from several sourcesC^N/C^P... then<enter>if menu pops up
C^XC^Lcompletes line: massive time-saver!C^XC^O"omni-completion" (needs language integration)C^XC^Fcomplete 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