Friday, September 23, 2011

Vim - Zero to Hero

"God gave us Vim to train the faithful.  One must not go against the word of God.."  - Anonymous Fremen Developer


Wow, so my blog's still here?  Neat!  Like in that movie you've memorized, I'm not dead.  I'm getting better.  I'll talk about that later.  Right now, I want to talk about Vim.

I really want to thank whoever it was (Google fails me) who said that you should master one text editor.  It made sense to me, so I considered which to use.
"Hmm," said I, sezzee, "it has to be free, because I'm cheap.  It has to be cross platform, because I use at least three OS's daily.  It has to be command line, because OpenSSH is the gift God gave all IT people for their faith through the dark days of DOS.  It's either going to be emacs or vim."

So it was either emacs or vim.  Every time I diddle a file on a remote machine, I fire vim.  Vim it is.

I am so glad I decided to learn more.

See, the ability to manipulate a text field in as quickly and flexibly as possible is the difference between tedium and artwork.  Your editor is an extension of your hand, which in turn is an extension of your mind; the more layers of hindrance and abstraction you put there, the more your work suffers.

I'm going to tell you about some things I love most about Vim.  Well, I'm going to tell myself, and if someone else happens to read this and get something out of it, that's good, too.


1)  Digraphs
You ever need to type a ©, ®, £, ¢, ⅳ, or a ぉ?  Well when you do, you need it now.  Each OS has bad to terrible ways to go about this (I'm looking at you, Microsoft), but in vim, there's one easy way, and it's called a digraph.

For example, for a © symbol, while in an edit mode (press i, dang it), press Ctrl-k, then C, then o.  You get a ©.  (Co, short for Copyright.  Easy, right?  Not A9, 00169, &#0169, etc..)  Want to know the value for some other symbol?  In command mode (hit escape, dang it) enter :digraph for a list.

2)  Splits
I do more than one thing at a time.  So should my editor.  :split for a horizontal split, :vsplit for a vertical split, Control-w/ControlW to move back and forth, and :close to remove a split.  If you're familiar with gnu screen (and you should be), this is cake.

3) Shell and Ranges
This is where I really started hearing angelic choirs.  Let's say you've got text that looks like this:


  1 You will need :
  2 6) Onions
  3 4) Peppers
  4 3) Garlic
  5 1) Soup
  6 2) Kettles
  7 .. in order to prepare some foodish thing..

.. and you want to sort lines 2 through 6, but not 1 or 7.  Yes, there's a lot of ways to do it, but here's the fastest I've ever seen:
Press :
Type 2,6 ! sort -n
Press enter.
Poof.
2,6 was the range of lines in which you wanted a shell command (!), specifically sort, to operate in.  Vim passed the range out to sort, and wrote back what it got.

This means that you can extend your text editor with any script you care to write that accepts standard in.  Which means it can do anything, like Zombo.com, only for real.

That also means you can suck values right out of shell, like this : r ! random_script.py


4)  Macros


Yum.  Before you do macros, learn markers:
Press m, then any alpha, to mark a position.
Press ~, then that same letter, to move your cursor back to that spot.

Now the macro:
Press q, then any alpha.
Do something repetitive.
Type q
Now type @, then the alpha, to do it again.  Better, do 99 @ letter to do it 99 times.  Macros are, blessedly, nesting.  Wanna comment a section of code in a language that doesn't accept multi line comments (there are some)?  Easy...

5) Regex

If techs are magicians, then regular expressions are the bastardized latin their spells are cast in.  Unless you are down tight with grammars (and I acknowledge your superiority if you do, sempai; teach me), they are the most powerful way to manipulate strings in the universe.  And Vim uses them easily.

:%s/regex/replace/g
And yes, that will accept a range:

:2,6 s/\)/:/


.. OK, that's all for now, but heaven help us, it's enough.  Although I see I haven't even mentioned visual mode, or file management (:Ex), or a thousand other things that my trip through studying Vim has yielded.  Know something I didn't mention?  Learn something?  Comment!


No comments:

Post a Comment