📜 ⬆️ ⬇️

effective use of vim: "from the very begining"

“For me, vi is Zen.
Using vi is a zen practice.
Each team is a koan.
Full of meaning for the user,
Senseless for the uninitiated.
You will know the truth every time you use it. ”
--reddy@lion.austin.
how well do you know vim language?
look at the keyboard,
Can you say what each letter performs?
how much do you know?
how much do you use?



Good all the time of day, I would like to write a series of articles on working with the vim editor, this is my first article (quite the first) and I would like to start with the basics: insert mode, command mode, loading and saving files, and so on. It is designed to help beginners develop their skills with which they can use vim more effectively.

> cx <means Ctrl-X, holding down the Ctrl key and pressing the x key. You can get information about most of the commands used here by typing: help command in the vim editor, where command is the command you want help with.
')
So, for vim has three modes:
let's solve the command - copying \ pasting \ deleting \ formatting text and etc.
insert mode - text input mode
selection mode - it is clear from the name =)
Esc - cancellation of the current action \ exit to the command mode

When you open the file, you automatically get into the command mode, and start with it.

Efficient moving


do you use h / j / k / l to move?
or still live in the world "GUIarrows?"
if yes - relearn
save for yourself endless kilometers of movement to the arrows and the "home" keys

Basically, you have to spend as little time as possible in insert mode, because this mode acts like a dumb editor. That is why beginners spend so much time in insert mode - it makes using vim easier. But the real power of vim is hidden in the use of command mode! You will understand: the better you know vim, the less time you will spend in insert mode.

Use h, j, k and l



The first step to effective editing will be disaccustoming from using the arrow keys. One of the advantages of vim's modular design is that you don’t need to move your hands back and forth between the cursor keys and regular keys; when in the command mode, the letters h, j, k, l correspond to the directions left, down, up, and right. Of course, it takes some time to practice, but you will see a difference in speed as soon as you try.

Use combinations to move the cursor in the current line.



Most editors have only simple commands to move the cursor (left, right, up, down, to the beginning of the line and to its end, and so on). Vim has very advanced cursor control commands; These commands are called combinations (orig: "motions"). When the cursor moves from one point of text to another, the text between these points (including themselves) is considered “missed” (orig: “moved over”) (this term will be important later)

fx move the cursor forward (forward) to the next occurrence of the character x in the current line (of course, x is any character). This is an incredibly useful command. You can type; in order to repeat the last f command you entered.
tx is the same as described above, but the cursor is set directly in front of the character, and not on the character itself.
Fx Move the cursor backward to the previous entry of the x in the current line.
w move the cursor forward one word
b move the cursor back one word
0 move the cursor to the beginning of the current line
^ move the cursor to the first character in the current line
$ move the cursor to the end of the current line


We print effectively



Use auto-complete words.



Vim has a very good word auto-complement system. This means that you can type part of a long word, press a key and vim completes the word for you. For example, if you have a variable called iAmALongAndAwkwardVarName somewhere in your code, you probably don’t really want to print the whole name with each use.

To use auto-complete words, just type the first few letters (for example, iAmAL) and press> cn <or> cp <.

Efficient transition to insert mode


How many methods can you go to insert mode?
let's see i I o O S S a A c C?
and r R for the full range
and these are just single letters ....

Most new vim users enter insert mode by pressing the i key. This works, but is often quite inefficient, so vim has several commands for switching to insert mode. Here are some of the most popular:
i insert text to the left of the current one
I insert text at the beginning of the line
a paste text to the right of the current character
A insert text at the end of the current line
o create a new line under the current one and insert text into the new line
O create a new line above the current one and insert text into a new line.
c {motions} delete text marked as “missing” and insert replacement text. For example, c $ deletes the text from the cursor position to the end of the line and goes into insert mode (it is easier to say to c = change what it means to change). The deleted text is transferred to the clipboard and can later be pasted.
d {motions} is the same as c {motions}, but without switching to the insert mode (d - delete)


Efficiently navigate the file


how are you moving
at the beginning / end of the file? gg / g
on the 100, 1546, nth row? 100G, 1546G, nG
on the first non-empty character in the string? ^
in the next line? > CR <
in the previous one? - (minus)


Vim has a lot of commands that can send you to any place (so don't be offended if that =), in your file - it is very rare to scroll manually through the jungle of text.
Here are a few of the most useful moves:

> cf & rt move forward (forward) one screen
> cb & rt move backward one screen
G Move the cursor to the end of the file.
numG move the cursor to the line number num (for example, 10G move the cursor to the line number 10)
gg move the cursor to the beginning of the file
H move the cursor to the top of the screen
M Move the cursor to the center of the screen.
L move the cursor down the screen


You can also place tags in the buffer:
m - create a label indicating the current position of the cursor.
`- set the cursor on the label. Please note - reverse quotes `
'- set the cursor to one line marked with `. or '. - set the cursor to the position where the last changes were made (the difference between `and ', see above)
`` - the transition between the last two positions


- the label will be local
- global
: marks - lists all marks

For those who tightly decided tonight to get off the notepad ++ (sorry, for those who have not yet come across tags), I would like to give a small pseudo example, running a little ahead

just write that
u - undo
> cR < - rendo

open c: \ windows \ system.ini
5G - go to the 5th line
ma - set the “a” brand
15G - go to the 15th drain
c'a - change the text on your own

after the changes we go to the command mode (Esc)
10G - cm above
mb - cm above
d'b - d = delete

go to insert mode again
> cv <- selection mode (about it a bit later, I just want to show the full power of vim)
4j - j - shift the cursor down nj - means to repeat the shift n times
4 >> - shift the text to the right, n >> is similar to nj
20G - go to the 20th line
= 'a - auto-formatting wonders? =)

go to insert mode
: q! - exit without saving changes
if you think that vim is already mastered - you just need to enter: wq (I do not advise, but you can try)



Search for words in text



* go to the next mention of the current word in the file (For example, if the cursor points to the word "Vasya", then it will move to the next mention of this word in your file)
# same as *, but moves to the previous mention
/ text, starting from the cursor, searches for the next reference to the text line and goes to it. To perform the search, press enter. To repeat the last search, type n (next)
? text is the same as / text, but the search goes in the opposite direction


Effectively moving blocks of text



Use visual selection and the appropriate selection mode.



There are three main modes of visual selection (text highlighting modes). These modes are activated as follows:

v character selection mode. This mode is used by most people, so try to practice before trying the rest.
V line selection mode. In this mode, entire lines are always highlighted. This is much more convenient if you want to select multiple lines.
> cv < block selection mode. Insanely powerful tool available in a small number of editors. You can select a rectangular block with any text inside and it will be highlighted.

All normal cursor commands also work. For example, the vwww command will put vim into character-by-character visual editing and highlight the following three words. The Vjj command will put vim into character-by-character visual editing mode and highlight the current line and the two lines below it.

Cut and paste from visual selection



Probably, after you select some part of the text, you will have a desire to do something with it, otherwise why would you need to select it? Here are a few useful commands that can be used over the highlighted section of the text:

d cut (delete) the selected text and put it on the clipboard
y copy (yank) tagged text to the clipboard
c cut the marked text and place it on the clipboard. This command acts like the d command, but leaves the editor in insert mode.

Cutting and pasting from non-highlight mode



If you know what you need to cut or copy, then you can perform these operations without switching to the visual selection mode. It saves you time.

d {motion} - cut the text marked as “skipped” and place it on the clipboard. For example, the dw command cuts a word, and the dfS command cuts text starting at the cursor position and ending with the next S character that appears in the current line, including the S character itself.
y {motion} copy the “skipped” text
c {motion} will cut the “skipped” text and leave the editor in insert mode
dd cuts the current line
yy copies the current line
cc cuts the current line and leaves the editor in insert mode.
D cuts the text from the cursor position to the end of the current line.
Y copy all the text, like yy . (This is of course non-standard, you can use y $ for the action you could expect from the Y command)
C cut the text from the cursor to the end of the line and leave the editor in insert mode
x cut the current character (acts like backspace)
s cut the current character and leave the editor in insert mode

To insert, place the cursor where you want to insert the text and press the p key

Avoid repeating your actions.



Amazing team .



In vim, click . (full stop) will repeat the last command entered. For example, if the last command was dw (delete word), after clicking. in command mode, vim will delete another word.

Use counters



Counters are another powerful and time-saving tool. Any commands can be preceded by a number. This number indicates how many times the command must be repeated. Here are some examples:
3j will move the cursor down 3 lines
1 0dd remove 10 lines
y3f " will copy the text from the cursor to the third quote after the cursor on the current line.


Thank you all for your attention
To be honest, I wanted to write more (I didn’t touch on the topic of macros, registers, additional modules, etc.), but I’ll think that
if I have enough karma, I promise to continue the nap =)

I'd add on my own - it is difficult in the teaching to be easy in battle, at first for me vim seemed like something terribly complicated and piled up, constantly wanted something, did not let me just enter the text =)
now - I just can not type the text somewhere in a notebook and etc. Even I send mail through vim.

ps imperceptibly for everyone I want to thank a person who first introduced me to linux and vim, you could say thanks to him that this article was born, thank you Roman Romanchuck, for a long time did not communicate with you
pss request - do not kick hard, write badly, but I try !!

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


All Articles