DEV Community

Discussion on: Vim: from foe to friend in 9 minutes

Collapse
 
omerxx profile image
Omer Hamerman • Edited

Hey! And thanks for raising the discussion because there are great questions.
I'll try to answer to the best of my understanding.

There is no claim that IDE's as a general concept are all inferior and cannot do what Vim does. Vim has a very profound concept of motion that is designed to make you faster.

For example, you mentioned the motions you use when moving in a line. In Vim, while it's obvious your mouse isn't playing a role at all, just reaching for your kb arrows is considered a big no-no. The motion is designed to keep your wrists in the exact same position, while only moving the tips of your fingers. Arrows are considered a complete finger context switch. And so motions are being used with hjkl (hard to get used to, but once you do you won't go back to taking your fingers away :) )

Another point in the motions you described in the first question is only involving a specific line. Try to consider moving in a large text file (or a code file for that matter) where you have to jump between definitions or methods. Vim always tried to involve the shortest way such as typing <number of lines>+<down motion (j) or the use of marks. The same goes for the Vim quick search and navigating between results with motions that won't draw your fingers away. You can even take it a step further and use the motion plugin.

Another powerful motion is f (find) and F (find backward) which lets you find the next char in front or behind the cursor. And so moving to the next space (or next 3 spaces for that matter) are a chain of number and find motion. This goes for anything and is one of my most used motions in Vim, which when writing text in another editor (like at this moment for instance) I find it very hard to use without my superfast options..

If you're really into learning it you can try reading the docs or reading the matching section in Drew Niel's book (mentioned in the post).

When it comes to repeated typing, Vim is super useful, mastering macros allows a super-efficient way of refactoring code and custom changes where a simple multi-cursor concept won't do (which by the way Vim implements really nicely too).

To conclude I'd add that Vim's idea of doing stuff is a number of repetition followed by a motion followed by a target (char / word / line / code block / page etc...). If you know how to handle even a few it makes you a wizard of motion and efficiency.

To me, it was the difference of translating my thoughts into code (or text) in the same speed my mind works. Which also contributes to the flow of work and creation.

Hope this helps or answers your questions :)

Collapse
 
viccw profile image
Vic Seedoubleyew

Thanks a lot for taking the time to address my questions.

This reply helps a lot. I start to understand some of the benefits of using vim. Not having to move my wrists can definitely speed up things tremendously.

Could you please share an example of "mastering macros allows a super-efficient way of refactoring code and custom changes where a simple multi-cursor concept won't do". I still don't have a good picture of what that can be.

Thanks a lot again!

Thread Thread
 
omerxx profile image
Omer Hamerman

Hey Vic,

Sure: imagine a situation where you write some javascript code and would like to change them all to async and add an optional parameter to them as well.
With any other tool you'd have to go one by one, maybe you'll be able to search for function and maybe be able to add the prefix of async but how about a last optional argument?

With Vim the logic for a macro would be this:

  1. Search for all functions: /function
  2. Start recording to a specific register:qq means record to register q, qw - the same for w
  3. Add a preceding async: I async <space> (cap I means "insert at the beginning of the line)
  4. Go to normal mode: <esc>
  5. Find the closing arguments line by f) (*f*ind forward the char ))
  6. Insert the argument: i somearg? (*i*nsert the arg with optional ?)
  7. Go to normal mode: <esc>
  8. Go to next appearance of function: n
  9. Stop recording: q

Now, if you @q Vim will perform all of the above on the second appearance of function and will end on the next one. if you @@ it will do once again the last used macro.
If you 50@q you'll run this 50 times more. Assuming you have less than 50 of them, and that function is generic enough in JS to indicate a function and will not appear anywhere else in the file (in other case you have to fix your initial search target), you've quickly editted a file regardless of the size with the exact change you wanted.

While this may seem very specific, I can't stress enough how useful this is. When you deal with huge json or yaml files this is like a magic wand.

You can also combine this with NERDTree to visually walk through any other file (maybe even with specific naming) to edit multiple ones.

You can even edit your macro by reading the register q and saving it again (will not explain here unless you really want to dive into it) and use it later, or just fix stuff.

Thread Thread
 
viccw profile image
Vic Seedoubleyew

Thanks a lot once again for taking the time to share insights with me. I really appreciate it.

As you mention, I find this example pretty specific. I don't write JavaScript, so I wouldn't know about adding "async" to many functions at the same time. But I can't imagine when I would add the same optional argument to many contiguous functions at the same time. Do you have a real life example of some vim-specific refactoring that you did recently?

About processing large json or xml files, this has never happened to me, and I guess this is pretty rare for standard software engineers.

Don't get me wrong, I am not trying to argue here, or prove that vim is not useful. I am really genuinely interested in understand specifically what benefits it can offer me, and other typical software engineers.

Thanks again very much for your help!

Thread Thread
 
omerxx profile image
Omer Hamerman

Hey Vic!

No worries at all! I totally get you.
I think what's important here is that you learn what you need. If you don't feel you have repetitive refactoring work then there's no need for you to dive into it.
Macros in Vim are a relatively very easy thing to learn so I think spending the hour and adding them to your arsenal of Vim magic is a good thing.

I think I find myself using a macro once a day. Sometimes it could probably done with a regex replacements statement (%s/<pattern-to-replace>/<replacement>/g) and sometimes with multiple cursors which I find to be more dangerous then helpful.

Thread Thread
 
viccw profile image
Vic Seedoubleyew

Thanks again for your reply.

About not feeling that I have a need, I think one does not need to feel the need. Very often, one doesn't image that one could go faster. This is why although I don't imagine how I could be faster, I would love to get examples that I don't picture yet.

About refactorings, I never use multiple cursors. However I very often use IDEs' rename variable, rename field, rename method, rename class, move class, etc, tools

Thread Thread
 
omerxx profile image
Omer Hamerman

Hey Vic,

One example I used today: I'm building our product's backend and am testing it both with unit tests and mocks of calls and both with sending real-life requests from postman.

The request is a 100 line JSON, and we agreed yesterday about removing a specific nested field from specific locations in the request. I've created a macro that searches for the constant field about each set of actual fields needed deletion (/<field>/) then, I created automation for deleting it (this was specifically "go 3 lines down, remove the trailing comma, then delete the line below" since this should be a valid JSON). So: 3jEx (3 lines down, End of line, delete char), and jdd (line down delete line). Now, all I had to do is find the next (n) appearance of my search index, and run the macro as the number of nested objects I have (<#_of_objects>@@)

Thread Thread
 
viccw profile image
Vic Seedoubleyew

Thanks a lot again for your reply! I really appreciate your taking the time to share your experience.

I am not exactly sure, but wouldn't see be doable with a simple search and replace with regular expressions in an IDE? Something that would take a variable number of white space before your field, and an optional comma if the next character is not a closing bracket?

Thread Thread
 
omerxx profile image
Omer Hamerman • Edited

Depends, sometimes it is sometimes it isn't, regex on its own when trying to achieve complex solutions is hard.
Many times a macro is just a faster simpler solution which allows you to think like a human being, and sometimes the only way is a macro. Search and replace is a single specific use-case, not a substitute to all macro powers.

Thread Thread
 
viccw profile image
Vic Seedoubleyew

Thanks for the swift reply!

After thinking about all of this, my conclusion is the following:

  • I don't see how macros can change much my productivity
  • not having to move my wrists to the arrows, and using mainly the home row instead, is conversely a killer feature, which I can see making me a lot faster
  • the problem I have with switching is the following. Muscles only have a single memory. If I switch my muscle memory to vim, it means I will be very slow outside of vim. Which means either I have to go to vim every time I want to type anything, such as replying to each email, or I have to suffer frustration each time I type something outside of vim. If there was a way to change system-wide input settings to use an "embedded vim", I would definitely make the switch

I guess the situation is a bit the same as with switching to a Dvorak keyboard layout. I tried it before, and then I realized that I couldn't set it up on my phone, so I was super slow and frustrated on my phone. I moved back to a standard keyboard layout just because of that.

Thread Thread
 
omerxx profile image
Omer Hamerman

Hey Vic,

Basically I agree with the last 2 statements. With the first one not fully but I can only send you to the docs where you may find something I didn't say :)

About the "problem" with moving out of Vim, I agree but you'd be surprised to know that since Vim is deeply integrated a long time into any *nix system it's convention keys can be found almost anywhere. Even modern mail clients like SuperHuman integrated Vim shortcuts by default...

Personally I use the same for everything, even my MacOS desktops are configured to switch with Vim hjkl.

Another example is the terminal which can be set with set -o vi to provide Vim motions within the lines.

The same goes for IDEs obviously and many more...

Thread Thread
 
viccw profile image
Vic Seedoubleyew

Thanks again very much for taking the time to clear things out for me. I really feel lucky to receive such guidance.

One crucial point for me is the ability to use Google Docs and Gmail. I tried looking online but so far haven't found any really good solution. VimAnywhere seems to be good for short non-formatted input fields, but not very convenient with Google Docs.

Would you by any chance have something to suggest?

Thanks a lot again!