This post was published in the forum a couple of months ago, I think it is time for me to go to the blog, doubts, comments, everything can be done in the comments, please DO NOT FLAME If you don't like Vim, just withdraw from the post 🙂
I will skip the basic part because it will become very extensive if you want or are interested in learning it, I recommend that you run from the terminal
Now yes, without more we start 😀
Table of Contents
Macros in VIM
It may not be one of the great mysteries of vim but it is good to know vim has macro support it is possible to store macros from the a to z ; to start recording you only need to press q+letter to stop recording we press q and to invoke the macro it is something like number+@+letter.
letter: It is a key of the a to z.
number: It is the number of times we will repeat the action.
This will facilitate many repetitive tasks. For example; Suppose we have these lines:
one example one two example two three example three four example four five example five
We want to give it this format:
'one': 'example' 'one'; 'two': 'example' 'two'; 'three': 'example' 'three'; 'four': 'example' 'four'; 'five': 'example' 'five';
This is where macros make everything easier for us 🙂
qa I '[Esc] ea': [Esc] wi '[Esc] ea' [Esc] wi '[Esc] A'; [Esc] 0j q
Then place the cursor on the line to change and use number+@+letter in this case I used 4+@+a
A very important tool in Vim is to substitute for this we place ourselves in Vim Normal mode by pressing Esc later we press : and we write the command I say command because it is not limited to substitution. is very similar to RegEx
The most common to change a string of characters throughout the document:
:% s / vim / Vim / g
Replace in all vim lines by Vim note: if i didn't have the sign % I would only search the line that you are located
Substitute between a certain range here between lines 3,5:
: 3,5s / Vim / VIM / g
From lines 3 to 5 replace Vim with VIM
Here note that there is no g in the end it only edits the first word that matches
: 3,5s / Vim / VIM
With this line we eliminate all the lines that contain a #
:., $ g / # / d
of the current line . until the end of the document $ look for the numerals # and delete them d
Here a command sort the file section
: 3, $ sort
From line 3 to the end of the document order it
Here the trick is in the command :r read what this does as its name says hahaha read, you could add the text of another document, but in this example we add the indicator ! that this separately is to execute, it is possible to execute a script from the same vim to debug or whatever you can think of here an example:
#Date:: r! Date #Date with format:: r! Date + \% D #List directories:: r! Ls Documents
I am using common commands but vim takes the output of any command and inserts it as text in the document.
And this is only a small part, there is still more ... And the things that I do not know hahahaha.
Last but not least is the Visual state where we can select lines to manipulate them easily. There are three types of visual selection.
v Visual by characters
V Visual by lines
control+v Visual by blocks
Select what is inside a parenthesis:
v% If you are at the beginning / end of the parenthesis
vib If you are inside the parentheses
Select what is inside double or single quotes:
vi' Select single quotes
vi" Select double quotes
viB Select everything that is inside the keys
Selection by line
ggVG Select the entire document
Here is basically gg go to start; enter visual selector V; and go to the end G.
Selection by block
[control] v e5j C [write the text] [esc] [esc]
And in this you enter the visual selector by blocks controlv, I advance to the end of the word, and 5 lines down 5j, I erase the word with C I write the text to replace and press Esc Esc.
The other is the same but instead of c it's something like delete and go into insert mode i use i enters insert I mode where the pointer is located.
All this that I explain may seem mysterious or something like that like vim ninja hahaha but it is nothing that is not in the vim manual Vim has one of the best documentations I know to enter the documentation just enter
if you need something specific
: help: w
This will take you directly to the section that talks about: w save.
Now if ... Happy Vim people. 😀