- most of programming time is spent editing, not writing.
- editing is a process of manipulating text in ways beyond just writing, such as deletion, replacement, reordering, duplication, formatting, etc.
- since writing (input) is a relatively small part of the whole of programming, making the other tasks easier by default can be a productivity win.
- os shortcuts for editing can't get 'shadowed' by custom actions, since they take the form of commands in 'normal' (command) mode
You will almost always be in this mode, so much so that it is useful to think of this mode as the 'normal' mode. In fact, Vim calls it Normal Mode. In general, the other modes can be thought of transitional or temporary–you are always in the process of returning to Command Mode.
Return to Command Mode by pressing esc
(or ctrl+c
).
Command Mode is centered on the concept of 'movements'
- left:
h
- down:
j
- up:
k
- right:
l
WTF
hjkl
???There are apparently historical reasons for this–early terminals often lacked arrow keys. However, a side affect of this is that the most common movement comes without having to even leave the 'home row'
- next word:
w
- end of word:
e
- beginning of word:
b
- beginning of line:
^
- end of line:
$
- next paragraph:
}
- previous paragraph:
{
- matching bracket:
%
- move to next occurrence of the current word:
*
- move to previous occurrence of the current word:
#
- move (un)til character:
t<char>
- move back (un)til character:
T<char>
- move to found character:
f<char>
- move back to found character:
F<char>
- move to match:
/<regex>
- move back to match:
?<regex>
- move to next occurrence of a match:
n
- move to previous occurrence of a match:
N
This mode works the way text editing works throughout the OS. Same shortcuts (cmd+c
, cmd+p
, etc), same movements (arrow keys, page up/down, ctrl+a
, ctrl+e
, etc).
There are many movement commands that can result in switching to Insert Mode:
- insert:
i
- insert after:
a
- insert at the start of the line:
I
- insert at the end of the line:
A
- insert on new line:
o
- insert on new line above:
O
- substitute:
s
- change:
c
A mode for creating selections. It's a modified version of Command mode, where commands either result in modifications to a selection range, or perform an editing task on the selection.
To enter Visual Mode (starting from Command Mode):
v
to select by characterV
to select by line
While in visual mode, most movement commands can be used to expand or contract the selection. Commands that make edits do so on the current selection, and automatically switch to either Command Mode or Insert Mode, depending on the command. For example, pressing i
Command Mode presents a sort of "language" that allows describing text editing tasks with combinations of commands. Many commands can be conveniently thought of as the "verbs", "nouns" and "prepositions" that make up the "language".
- change (
c
) - replace (
r
) - substitute (
s
) - delete (
d
) - yank (
y
) - put (
p
)
WTF 'change', 'replace', AND 'substitute'? aren't these all the same thing?
Nope!
- Change deletes everything in the next movement and puts you in Insert Mode (though with a selection, it behaves like substitute).
- Substitute immediately deletes the current character (or selection) and puts you in Insert Mode.
- Replace never enters Insert Mode at all, but just replaces the current character (or every character in a selection) with the provided character.
- word (
w
) - end (
e
) - beginning (
b
)
"Nouns" can be any of the available movements!
- inside (
i
) - around (
a
) - (un)til (
t
)
WTF i thought
i
anda
were insert commands!Many Vim commands expect to be appended with a movement. When waiting for that movement, the command is 'pending', during which time "prepositional" commands become available. So, when a command like
d
(delete) is entered, nothing will happen because the command is 'pending' a movement. Pressingi
next won't enter Insert Mode, as you might expect when starting out. In fact, it will leave the command 'pending', becausei
in this context is the 'inside' command, which requires a subject (such asw
) to complete the movement!
You can combine these parts of the "language" to perform editing tasks:
- delete to the end of a word:
dw
(orde
) - delete the current word:
diw
- yank to the end of word:
ye
- change inside parentheses:
ci)
(orci(
) - delete around brackets:
da]
(orda[
)
There are many other ways to combine commands. For example:
- delete 3 words:
d3w
(or3dw
) - change a visual selection:
v<movement>c
- yank the next 5 characters:
y5l
(or5yl
) - auto-indent inside a block:
=i}
(or=i{
) - delete back through the word 'foo':
d?foo
- duplicate this line plus the 3 lines above:
y3kp
- replace the entire document with a bunch of Qs:
ggvGrQ
- repeat the last command with
.
- undo with
u
- redo with
ctrl+r
- jump to top of the document with
gg
- jump to the bottom with
G
- jump to a line number with
<number>G
- Some commands can be performed on the current line by repeating them:
- delete the current line:
dd
- change the current line:
cc
- yank the current line:
yy
- delete the current line:
- remap
CAPSLOCK
toESC
- open the key binding resolver in Atom (
cmd+.
) to learn what various keys do.
- Multiple cursors!
- OS integration!
- MUCH prettier!