Why Vim is awesome

Posted on in vim

In this post, I’ll just list some things I love about Vim. This post is not intended to teach you how to use Vim, I won’t deeply explain the commands I use, I just want you to see how easy things can be with Vim.

Repeatable commands

Something I love about Vim is that it allows you to repeat commands. And I don’t mean macros, I’m talking about the power of the . operator, which repeats the last thing you made. It’s one of my favourite commands as it gives you quite a lot of power. A very common use case is changing the name of a variable in a file:

What I did was simply step on the word I want to change, press * to search for the next occurrence. Once I’m on it, I simply change it to something else, the Vim way to do it is cw<Type text here><ESC>. Now comes the fun part, Using n. I search for the next word, and repeat my last action, which was change it’s name, running n. again gets the job done. The complete command list used is *cwSomethingeElse<ESC>n.n.

Another common use for this is spacing and formatting lines, for example, if we wanted to space all + signs in this statement, we can get away with repeating commands again.

The full command here is f+s + <ESC>;.;.;., nothing too different, only that instead of using * we actually use a motion to move to the plus sign using f+, because of this, the next command changes and instead of using n we use ;

Formatting code

Normally I limit my lines to 80 columns. So I have textwidth set to 80 in my .vimrc file. With this I can use gq to format a selection to 80 columns.

You can of course always format code using =, even though sometimes it breaks… Note the usage of % which matches the closing tag of an element, which is extremely handy, especially in visual line mode and HTML.

The full command list used there is Vl%=, that’s it.

Visual block mode

Vim has several selection modes, most of the time I use line mode but this one is super handy and works similarly to Sublime’s multiple cursors.

Visual block mode is accessed though <Ctrl-V> in Vim, but it also works with <Ctrl-Q> which I prefer.

Sorting CSS lines

Sorting is as easy as selecting the lines and using the :sort command.

That’s just the tip of the iceberg

Everything I’ve showed requires no plugins, is just plain old Vim. If you want to try it out, I recommend Practical Vim, it’s a really good book, only with reading the first few chapters you can already get a taste of Vim’s power!

  • Nic West

    If you like cwn.n. then you might be interested in :h gn

    “Search forward for the last used search pattern, like
    with `n`, and start Visual mode to select the match.”

    means you can do cgn then just keep hitting . to repeat, or n to skip. Think this was an addition in 7.4

    • gosukiwi

      Nice one! I’ll check that out :)

  • Nice tips, tho! :D Thanks

  • Awesome tips! Are you using a terminal or another text editor with a Vim plugin? I have switched to sublime with the Vim plugin but it brings just the basics (doesn’t support commands).

    • gosukiwi

      Sorry for the super late reply :P I’ve tried sublime’s vim emulation but it lacks a lot of things and I feel like it needs a lot of customization to be usable, if using Sublime I guess I just have to do things “the subilme way”.

      To be honest, most vim emulations suck, the only exception is “evil-mode” which is a pretty awesome implementation. It depends on Emacs though :p