📜 ⬆️ ⬇️

Vim antipatterns

When you're streaming, 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 keep bad habits from the time when you just mastered the editor. Vim speeds up the work so much that it is especially difficult to eradicate these habits, because you can not even notice them. But it's worth it. I will list some of the most common anti-patterns.

Move one line


If you need to jump more than a pair of lines, then moving one line at a time with the j or k keys is ineffective. In Vim, there are many ways to move vertically . I think that the two most useful ones are a jump on paragraphs and on screens. It depends on how far and how exactly you need to move.


If you know exactly where you need to move, you can use the navigation through the search: search forward with the / key, and backward search ? .

It is also always useful to return to the previous place, which is easy enough to do with two quotes or gi : return to the last place where you entered the text. If you want, you can even move between all the places where the text was entered using g; and g,
')

Move one character


Similarly, moving one character at a time with the h and l keys is often a waste of time when we have t and f :


Moving around with the w , W , b , B , e and e keys is also better. And here navigation with search is useful, and do not forget that you can copy, delete and change the text forward or back to the search result:
y/search<Enter>
y?search<Enter>
d/search<Enter>
d?search<Enter>
c/search<Enter>
c?search<Enter>

Search for a word under the cursor


Don't bother with typing a word or copying / pasting, just press * or # . It is amazing how much faster the work becomes when you master these hotkeys.

Delete and paste


There is no need to delete the text in order to replace it by going to insert mode:

d2wi

Faster and more accurate use of the change key c :

c2w

So the whole operation becomes repeatable with a dot ( . ).

Use 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 hjkl to navigate, the arrows already seem clumsy because the fingers are in the center of the keyboard throughout the Vim session, which is convenient for blind printing. Similarly, the Home and End keys: although they work like most editors, there is no particular reason to use them when the ^ and $ equivalents are closer.

Thus, it is advisable to wean yourself from the arrow keys, disabling them at least temporarily:

noremap <Up> <nop>
noremap <Down> <nop>
noremap <Left> <nop>
noremap <Right> <nop>


Blind typing with central keys not only gives the advantage of speed, but also allows you to feel better, because the wrists are resting in front of the keyboard and do not move too far, and for some people it even helps to protect themselves from the tunnel syndrome .

Move in insert mode


An additional advantage of blind dialing in the center line is that you get rid of the habit of moving in insert mode: this is the normal mode. As soon as you need to move somewhere, you go to the normal mode and move much more efficiently. It also helps to make the insertion operations more atomic and, therefore, more convenient for repetition.

Esc key


The Escape key on modern keyboards is much farther from the central row than on Bill Joy's keyboard when he was developing 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 comparing the rather useless Caps Lock key on the Esc operating system or even unusual combinations, such as jj . Although this is a slightly radical proposition, it works well for many people:

inoremap jj <Esc>

Move to the beginning or end of the line, then insert


Just use I and A In addition, they make the action repeatable for other lines where the same operation may be necessary.

Enter insert mode, then create new line


Use o and O to create a new line below and above, respectively, and simultaneously enter insert mode on it.

Enter insert mode to delete text


There is a rather obvious contradiction. Instead, delete the text by moving into it and using d with the appropriate motion or text object. Again, this action is repeatable and means that you are not holding Backspace. In general, if you hold a key in Vim, there is probably a faster way.

Repeat commands or searches


Just enter @: for a command or n / N for a search; Vim does not forget the last search. If it was not the latest 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& .

Recall Macros


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 plug-ins or significant key reassignments. See also the Vim wiki wiki , there are several other really useful examples.

Source: https://habr.com/ru/post/442110/


All Articles