How often do we limit our understanding of familiar patterns? Everyone probably knows that vim is a text editor, with built-in scripting language. And compare vim with other editors, sometimes even with IDE.
An analogy came to my mind, which is perhaps somewhat closer to understanding the possibilities of vim. Or it can be considered as another facet of the vim functional. I apologize for the repetition of the exhausted topic, but the disputes about this “editor” do not leave me indifferent. Perhaps among you, too, there are such people.
Earlier, somewhere in the vastness of Habr wrote about the killer feature vim'a - modes. To which a reasonable comment was made: regimes complicate the implementation of simple operations (among many other, no less reasonable comments). I always use vim in my work and I cannot deny that modes really complicate the work in the simplest cases. But let's consider the possibilities of vim as an esoteric text editing language.
In command mode, all keystrokes are interpreted as commands or parameters of these commands. For the convenience of typing, the commands are made in most cases one-character. Imagine what C # would look like if all language constructs and variables were single characters. And that's why vim has a very steep learning curve. It is necessary to remember and be able to navigate a large number of single-character commands. On stackoverflow, you can find the answers for the vim tag, which are a set of such commands. For example:
')
vmap \c :s!^!//!<CR> vmap \u :s!^//!!<CR>
These things remind me of examples of programs on Brainfuck (although the vim commands in my opinion are a bit simpler)
++++++++++[>+++++++>++++++++++>+++<<<-]>++.>+.+++++++ ..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.
Or, for example, in my config file you can find a wonderful line:
ve"aydeBvE"by
What conclusions can be drawn from this?
Wikipedia enlightens:
A common property inherent in any esoteric language - the text of the program on it is understandable only to the "initiated" or incomprehensible at all.
Those. I do not think vim
should be studied by anyone. Just as no one will argue that the ability to write on Brainfuck will make life easier for someone. However, if you have enough free time and you (like me) love unusual things, then I will give a brief introduction to this esoteric science - possession of vim. I will make a small reservation, we are not talking about vimscript - the built-in vim scripting language. We are only talking about the presentation of vim commands as a separate programming language.
It makes no sense to write about different modes of operation in vim, you really only need one mode - command mode. You can even say that the text input mode is needed only in order not to overload the command mode with text input capabilities. Therefore, I will group the material according to another principle - according to the capabilities of the teams.
Move Commands
In my opinion, with a good detailed study of this possibility, one should begin acquaintance with vim. A short list of what I use regularly:
h, j, k, l - move the cursor one character left, down, up, right, respectively.
/ word - move the cursor to the position of the word word in the text (what the uninitiated call search).
? word - reverse search (yes, I am also uninitiated).
Hereinafter, in a good way, we should have left the wording with moving instead of searching (because this is the key point of the team), but I'm too lazy for such long phrases.
n - move to the next position of the word from the search in the text.
N - the same in the opposite direction.
* - search for a word under the cursor.
# - the same in the opposite direction.
f [az] - move to the position in the string of the character passed as a parameter.
F [az] - the same in the opposite direction.
w, b - move the word forward, backward, respectively.
m [az] - put a bookmark in the text with the name of the character passed as a parameter.
`[az] - go to the bookmark by name.
() - move to the previous, next sentence.
{} - move to the previous, next paragraph.
% - move to the corresponding bracket () {} [] (from the opening to the closing and back).
Just remember it is difficult and boring, you need to use in everyday tasks. Let your hands memorize these commands in the same way that they learned to touch typing - through regular workouts.
Further editing commands. They could be separated into a separate category, but they are useless without moving commands. And besides, there are very few of them. In your practice, you can generally limit yourself to three:
d - delete without entering text input mode.
c - delete with the transition to text input mode.
y - copy the text without making changes.
v - select text.
The last two do not quite fit the editing team, but behave similarly to the first two.
Some bundles of editing and moving commands operate through an auxiliary selection mode - v. Those. first select the required piece of code, then delete.
A few examples:
de - delete the word
v / a <Enter> d - delete from the current position to the first position of the character “a”
dta - delete before the first position in the character string “a”
Variables
Variables in vim are called registers. There are several special registers:
0 - the last copied text.
+ - text from the clipboard.
And named az, the contents of which is set explicitly by the command "[az] added as a parameter to the editing command. That is, ve" ay - select the word and copy to the register a. Add text to the contents of the register by specifying its name with a capital letter ve "Ay.
The contents of all registers can be viewed with the command: registers
Macros
The most useful, in my opinion, the function of the esoteric language. Analog procedures in ordinary languages. Functionality, rarely mentioned, but very often used by me.
The macro text is stored in a named register and can be called by name.
q [az] - start recording
q - stop recording
@ [az] - execute the macro (call the procedure)
For example, if the following text ve "Ay is put in a named register b, and then typed @b, the word under the cursor will be added to register A. Accordingly, the qbve" Ayq command sequence is similar to writing a macro to register b with parallel execution of commands (i.e. you can see the result in the recording process).
Restart
All editing commands and macros can be run with the parameter - the number of iterations. For example, 5de will delete 5 words, 5 @ b will run the macro b 5 times. There is also a command "." - Repeat the last command executed. Those. 5de. - will delete 10 words.
Example
For the first acquaintance, I think it is enough. In conclusion, I will show a small example. Suppose we have a list of columns and corresponding tables:
Col1, Table1
Col2, Table2
Col3, Table3
...
And we want for each pair to write select with the result merged into one data set (i.e. union all). Of course, this problem can be solved in any other more appropriate way. And even better to solve it in adequate ways. But it is too easy.
The text of the program will be as follows:
qq0iselect <Esc>,s from<Esc> union all<Esc>0q2@q
A few words about what was left
1. Plugins vim.
2. VimScript.
3. Ed.
4. Another bunch of interesting teams.
VimScript is a scripting language embedded in vim (mentioned above). Ed is a text editor that is built into vim (perhaps not very well put it, but the essence is close to this). It has a completely separate syntax, but to some extent everyone who has used the sed utility is familiar with it. In ed there is a move / delete lines, display on the screen and some other functions.
That's all. Happy vimming!