02.Normal Mode
Tip 7. Pause with Your Brush Off the Page
For those unused to Vim, Normal mode can seem like an odd default. But experienced Vim users have difficulty imagining it any other way. This tip uses analogy to illustrate the Vim way.
Normal mode is the natural resting state.
Tip 8. Chunk Your Undos
chunk: to divide (something) into chunks; (of computing) to divide data into separate sections;
In other text editors, invoking the undo command after typing a few words might revert back the last typed word or character. However, in Vim we can control the granularity of the undo command.
- The
u
key triggers the undo command, which reverts the most recent change <C-r>
key triggers redoi{insert some text}<Esc>
constitutes a change- Use
<Esc>
to leave Insert mode, pressA
to carry on where I left off - Use
<Esc>o
to replace<CR>
Moving Around in Insert Mode Resets the Change
When I said that the undo command would revert all characters entered (or deleted) during a trip into Insert mode and back, I was glossing over a small detail. If we use the <Up>
, <Down>
, <Left>
, or <Right>
cursor keys while in Insert mode, a new undo chunk is created. It’s just as though we had switched back to Normal mode to move around with the h
, j
, k
, or l
commands, except that we don’t have to leave Insert mode. This also has implications on the operation of the dot command.
Tip 9. Compose Repeatable Changes
Vim is optimized of repetition. In order to exploit this, we have to be mindful of how we compose our changes.
Delete Backward / Delete Forward / Delete an Entire Word
The end is nigh
dbx
光标位于最后一个字符,先db
从光标处往回删至单词头,再x
删除最后一个字母bdw
光标位于最后一个字符,先b
移动光标回到单词头,再dw
删除整个单词daw
deletes a word
We can be more surgical by using the aw
text object instead of a motion (see :h aw
).
daw
deletes a word,word<Space>
including the trailing spacediw
deletes a word,word
Tip 10. Use Counts to Do Simple Arithmetic
Most Normal mode commands can be executed with a count. We can exploit this feature to do some arithmetic.
The <C-a>
and <C-x>
commands perform addition and subtraction on numbers.
.blog, .news { background-image: url(/sprite.png); }
.blog { background-position: 0px 0px; }
.cars { background-position: -180px 0px; }
We're going to duplicate the last line and make two small modifications to it: replace the word "blog" with "news", and change "0px" to "-180px".
yyp
copy the current line and paste it belowcW
replace from cursor to the end of the word180<C-x>
Tip 11. Don't Count If You Can Repeat
We can minimize the keystrokes required to perform certain tasks by providing a count, but that doesn't mean that we should. Consider the pros and cons of counting versus repeating.
Delete more than one word
Use a Count When It Matters
I have a couple of questions.
With cursor on "a", type c3wsome more<Esc>
and revise.
Tip 12. Combine and Conquer
Much of Vim's power stems from the way that operators and motions can be combined. In this tip, we'll look at how this works and consider the implications.
Operator + Motion = Action
d{motion}
dl
deletes a single characterdaw
deletes a worddap
deletes a paragraph
c{motion}
y{motion}
You can find the complete list by looking up :h operator
Are x
command and dl
command the same? They both appear to delete a single character in Normal mode.
Trigger | Effect |
---|---|
c | Change |
d | Delete |
y | Yank into register |
g~ | Swap case |
gu | Make lowercase |
gU | Make uppercase |
> | Shift right |
< | Shift left |
= | Autoindent |
! | Filter {motion} lines through an external program |
Just some random text for editing purposes.
This is our editing playground. And here some 360 degrees.
daw
deletes a word dd
deletes the current line dap
deletes the current paragraph
What's the difference between a line and a paragraph? Aren't they the same thing in Markdown files? No, they are different. In Mardkown, one line is ended by a <br>
sign, while a paragraph consists of one line or several lines. Therefore, paragraph is a larger unit.