Terminal Friday: Thinking Vim [Some tips]

Sorry for the Gif spelling mistakes I wrote very fast

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

$ vimtutor

Now yes, without more we start 😀

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.

Where:
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 🙂

Vim

 

Sequence used:

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

Replace

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
Examples:

The most common to change a string of characters throughout the document:

Terminal2

Command:

:% 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:

Terminal3

Command:

: 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

Terminal4

Command:

: 3,5s / Vim / VIM

With this line we eliminate all the lines that contain a #

Terminal5

Command:

:., $ 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

Terminal6

Command:

: 3, $ sort

From line 3 to the end of the document order it

Other Commands

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:

Terminal7
Command in order of appearance:

#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.

Selection

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

Terminal8

Select what is inside double or single quotes:
vi' Select single quotes
vi" Select double quotes

Terminal9

viB Select everything that is inside the keys

Terminal10

Selection by line

ggVG Select the entire document

Terminal11

Here is basically gg go to start; enter visual selector V; and go to the end G.

Selection by block

control+v

Terminal12

[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

: help

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. 😀


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

18 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Nodetino said

    I think I'll start using Vim 🙂

  2.   Rodrigo bravo said

    Excellent article thanks for sharing. I didn't know about selecting by block. Regards!

  3.   Blue skull said

    Emacs ..., I think someone had to say it, so why wait more: D, no, it is not the beginning of a Flame, between Emacs and Vim there can be no discussion: D, it is quite clear which is superior, and no, no e Vim;).

    1.    Bla bla bla said

      Yes, it is the beginning of a flame. Be careful with these types of comments because they are not objective. In addition, it does not contribute anything to what the article is about. Regards.

    2.    Zironid said

      Emacs! Emacs! Emacs! 😀

    3.    Martin said

      Emacs for the masses!
      «[…] I think someone had to say it […]» Hahaha, +1!

      Dress like it is, someone comes along who plays the exotic and says «cursors with arrows? WASD? No no, I will show you the way, it is hjkl »and there are a lot that are lit behind>: D

      @Blablabla: well there, your nick lives up to your chatter!

    4.    Giskard said

      A survey to see which readers prefer would not hurt.

  4.   ahdezzz said

    Great! Vim is by far the best text editor. Hopefully publish more often on this site about said editor. Regards.

  5.   Jorgicio said

    I always had problems understanding macros in Vim, there in a little free time I'll give it a spin 😛

    Note: For KDE users, use Vim-QT 😀

    Thank you

  6.   eliotime3000 said

    At last my Galaxy Mini will have one more reason to live: I don't have EMACS, but I do have VIM.

    I will take your tips into account.

  7.   kuk said

    good info thanks 🙂

  8.   Lito said

    share your vimrc is very understandable visually pleasing :) !!! I hope you share it with me :)!

    1.    Wada said

      Of course I will 😀 let me prepare a post

  9.   ramg91m said

    Wow it looks so good :)! haha I'll start to practice more, and your vimrc looks very good Oo! I hope you share it please;)!

    1.    Wada said

      In fact it is not so much vimrc, but I will explain that I have done hahaha

      1.    ramg91m said

        haha much better :)! so we all learn haha ​​thank you :)! 😉

  10.   Tesla said

    Very good advice Wada. The truth is that my programming needs are very basic and maybe that's why I have never seen the potential of Vim. However, what you say seems interesting to me. I will try to put it into practice when I have to do something.

    Thank you very much!

  11.   Jonathan Leonel Gasparrini said

    Excellent post! Although I already knew these tips very well explained for those who did not know them!
    A little help for new users, «vimtutor» is in English, for those who want the tutorial in Spanish, just type «vimtutor es».

    I love vim, my life would be totally different without him!
    I have been using it for 2 years and every day I am more surprised =)

    regards

    P / D: Anyone who wants to see an example of a .vimrc configuration, I invite you to see mine! =) https://github.com/jlgasparrini/dotvimrc