Hacker News new | ask | show | jobs
by super_mario 4030 days ago
Don't think of Vi or Vim as of regular editor. Think of it as functional programming language for text transformation, because that is what it essentially is. General form of vim command is nOm: repeat n-times operator O on text the motion command m moves over. So, for example, 5dw says "delete 5 words" because w command moves to next word, d operator stands "delete". The nice thing about this is that once you learn new motion command or operator, you become instantly more powerful. For example, after you learn that ) moves to the next sentence, you can now delete whole sentences with d). After you learn about change c operator, you can now change whole sentence with c). This essentially deletes the sentence and puts you in insert mode so you can type in the replacement text.

There are also text objects that allow you to apply operator on the object. For example if you are anywhere inside if (.) condition, you can type ci(, which stands for change inner ( i.e. change everything inside brackets, to quickly change the condition. Similarly, there is inner word, paragraph, XML tag, brace {, etc text objects, so it is fast to delete/modify etc entire blocks of code.

Vim also has Perl compatible regex engine, which allows you to do sweeping changes and transformation of text. The operators work with search motion as well, so you can do things like d/regex i.e. delete everything from current cursor position to first match of regex etc.

The :g ex command alone alows you to do fairly complex transformations, for example

    :g/pattern/d 
delete all lines that match pattern, or

    :g/pattern/m$
move all lines matching a pattern to end of file, or something more complex:

    :.,$g/^\d/exe "normal! \<C-A>"
Increment each number at the start of a line, from the current line to end-of-file, by one.

Of course there are a plethora of motion commands that allows you to quickly get to the line, character etc where you want to edit, all without taking your hand from the keyboard. In fact, most times you will be editing text well before the person using a mouse finds their mouse cursor.

This is all just scratching the surface (Vim has over 2000 commands), but hopefully it gives you the flavor of the experience editing in Vim. Getting proficient in Vim is liberating, it allows you to bend text effortlessly to your will without too much thinking really. It's programming your text, and in my mind a very valuable skill, just like learning the UNIX/POSIX command line is incredibly valuable (it's another functional programming environment where individual commands are your functions/filters and shell is executing them and you can string these filters together in unique ways to do all kinds of powerful things that simply don't have an equivalent in the GUI). Command line and Vim multiply you as developer and there is nothing in the GUI world that can come even close to this experience.