May 24, 2021 Vim
So far we've covered some of the most commonly used Vimscript commands, but they're not related to the way text is handled in normal mode every day. Is there a way to combine our scripts with everyday text editing commands?
The answer is clearly yes. W
e've seen
normal
command before, and it's time to cover it in more detail.
Execute the following command:
:normal G
Vim will move your cursor to the last line of the current file, as if you were pressing G in
G
Now do the following command:
:normal ggdd
Vim moves to the first line of the file
gg
and deletes it
dd
normal
command simply accepts a string of key values and treats them as if they were entered in normal mode.
It's as simple as that!
Do the following command to map
G
key to something else:
:nnoremap G dd
Pressing G in normal mode
G
an entire line.
Try this command:
:normal G
Vim deletes the current row.
normal
command will take into account all current maps.
This means that we
normal
provide normal with a version similar to
nnoremap
to
nmap
otherwise we can't use it -- considering we can't guess how users are mapped.
Fortunately, Vim does have such an order
normal!
To execute this command:
:normal! G
This time Vim will move the cursor to the bottom of the file, even
G
has been mapped.
When you write a Vim script, you should always use
normal!
, never use
normal
Do not trust the
~/.vimrc
If you use
normal!
O
ver time, you'll probably notice a problem.
Try the following command:
:normal! /foo<cr>
At first glance it looks like it should start searching for
foo
you'll see that it won't work. T
he
normal!
Special character
<cr>
are not resolved.
So Vim thinks you want to search for the string sequence "foo" without realizing that you're even pressing enter to search! (The original text is you even pressed return to perform the search! Press the meaning below should be not pressed return, to be asked the author) We will discuss how to deal with this problem in the next chapter.
Read
:help normal
In the final section, you'll get tips on the topics in the next chapter.
If you're not ready for the challenge, skip this section. If you're bold enough, good luck!
:help normal
undo. T
ry to design a map that deletes two rows but can undo each deletion individually.
It is
nnoremap <leader>d dddd
This time you don't really
normal!
nnoremap
enough), but it reveals one thing: Sometimes reading a Vim-command document can inspire ideas about something else.
If you haven't used
helpgrep
it's time to use it. R
ead
:help helpgrep
.
Watch out for sections on how to navigate through the matching content.
Don't tangle patterns for a while, we'll talk about them soon.
Now just know that you can
foo.*bar
method to find lines in your document that include that pattern.
Unfortunately,
helpgrep
be frustrating from time to time, because in order to find certain words, you need to know how to search for certain words.
I'll save you some useless work, and this time you'll have to find a way to manually modify Vim's undo sequence so that the two deletions you map can be undone independently.
You have to be flexible in the future. Sometimes when you're confused, Google, you know it.