![](http://habrastorage.org/getpro/habr/avatars/4ec/bd0/85d/4ecbd085d692835a931d03174ff19539.png)
Antipatterns Vim
- Transfer
- Tutorial
When you're in a state of flow, Vim seriously speeds up editing, whether it's writing code, poetry or prose. But since the learning curve is too steep for a text editor, it is very easy to maintain bad habits from the time when you just mastered the editor. Vim speeds up work so much that it’s especially difficult to eradicate these habits, because you might not even notice them. But it's worth it. I will list some of the most common antipatterns.
If you need to jump more than a couple of lines, then moving along one line with the keys
If you know exactly where you need to move, then you can use the navigation through the search: search forward with the key
It is also always useful to return to the previous place, which is quite easy to do with two quotation marks or
Likewise, moving one character at a time with keys
Word moves the keys
Do not bother typing or copying / pasting, just press
There is no need to delete the text in order to replace it by going into insert mode:
Faster and more accurate use the changes key
So the whole operation becomes repeatable with a dot (
Vim allows you to use the arrow keys to move in normal mode and in insert mode, but if you are used to using it for navigation
Thus, it is advisable to wean yourself from the arrow keys by disabling them at least temporarily:
Blind typing with the central keys not only gives an advantage of speed, but also makes you feel better, because your wrists rest in front of the keyboard and do not move too far, and it helps some people to protect themselves from tunnel syndrome .
An additional advantage of blind typing in the center line is that you get rid of the habit of moving in insert mode: normal mode is designed for this. As soon as you need to move somewhere, you switch to normal mode and move much more efficiently. It also helps to make paste operations more atomic and therefore more convenient for repetition.
The Escape key on modern keyboards is much further from the center row than on Bill Joy’s keyboard when he designed vi. Usually pressing Escape is not required:
Just use
Use
This is a fairly obvious contradiction. Instead, delete the text by moving into it and using
Just enter
Enter
Just enter
In fact, these are just a few common ways to increase the speed and overall efficiency of working with the editor without installing plugins or significant reassignment of keys. See also the wiki for tips on Vim , there are several other really useful examples.
One line move
If you need to jump more than a couple of lines, then moving along one line with the keys
j
or is k
inefficient. There are many ways to move vertically in Vim . I find the two most useful are paragraph and screen jumps. It depends on how far and how exactly you need to move.{
- Go to the beginning of the previous paragraph or block of code.}
- Go to the end of the next paragraph or block of code.Ctrl+F
- Go forward one screen.Ctrl+B
- Go back one screen.
If you know exactly where you need to move, then you can use the navigation through the search: search forward with the key
/
and backward with the key ?
. It is also always useful to return to the previous place, which is quite easy to do with two quotation marks or
gi
: return to the last place where you entered the text. If you want, you can even navigate between all the places where the text was entered using g;
and g,
.Single character movement
Likewise, moving one character at a time with keys
h
and is l
often a waste of time when we have t
and f
:t
- Move forward until the next character appears.f
- Move forward for the next occurrence of the character.T
- Move back to the previous occurrence of a character.F
- Move backward for the previous occurrence of a character.
Word moves the keys
w
, W
, b
, B
, e
and E
also better. And search navigation is useful here, and don't forget that you can copy, delete and change the text forward or backward to the search result:y/search
y?search
d/search
d?search
c/search
c?search
Search for a word under the cursor
Do not bother typing or copying / pasting, just press
*
or #
. It is amazing how much faster the work becomes when you master these hot keys.Delete and Paste
There is no need to delete the text in order to replace it by going into insert mode:
d2wi
Faster and more accurate use the changes key
c
: c2w
So the whole operation becomes repeatable with a dot (
.
).Using the arrow keys
Vim allows you to use the arrow keys to move in normal mode and in insert mode, but if you are used to using it for navigation
hjkl
, the arrows already seem clumsy, because throughout the Vim session the fingers are in the center of the keyboard , which is convenient for the blind print. The Home and End keys are similar: although they work as in most editors, there is no particular reason to use them when the equivalents of ^
and are closer $
. Thus, it is advisable to wean yourself from the arrow keys by disabling them at least temporarily:
noremap
noremap
noremap
noremap
Blind typing with the central keys not only gives an advantage of speed, but also makes you feel better, because your wrists rest in front of the keyboard and do not move too far, and it helps some people to protect themselves from tunnel syndrome .
Move in insert mode
An additional advantage of blind typing in the center line is that you get rid of the habit of moving in insert mode: normal mode is designed for this. As soon as you need to move somewhere, you switch to normal mode and move much more efficiently. It also helps to make paste operations more atomic and therefore more convenient for repetition.
Press Esc
The Escape key on modern keyboards is much further from the center row than on Bill Joy’s keyboard when he designed vi. Usually pressing Escape is not required:
Ctrl+[
much closer and more convenient, you will quickly change your habits. As an option, you can consider matching in the operating system on Esc a rather useless Caps Lock key or even unusual combinations, such as jj
. Although this is a bit of a radical proposal, it works well for many people:inoremap jj
Move to the beginning or end of a line, then insert
Just use
I
and A
. In addition, they make the action repeatable for other lines where the same operation might be needed.Enter insert mode, then create a new line
Use
o
and O
to create a new line lower and higher, respectively, and simultaneously enter insert mode on it.Enter insert mode to delete text
This is a fairly obvious contradiction. Instead, delete the text by moving into it and using
d
with the appropriate movement or text object. Again, this action is repeatable and means you are not holding backspace. In general, if you hold down a key in Vim, there is probably a faster way.Repeat commands or searches
Just enter
@:
for the command or n / N for the search; Vim does not forget the last search. If this was not the last command or search, but it is definitely in the history, type q:
or q/
, find it in the list and press Enter.Replacements
Enter
&
to repeat on the current line of the last replacement. You can repeat it on all lines by typing g&
.Macro Recall
Just enter
@@
. In fact, these are just a few common ways to increase the speed and overall efficiency of working with the editor without installing plugins or significant reassignment of keys. See also the wiki for tips on Vim , there are several other really useful examples.