Slashdot Mirror


Hacking VIM

Craig Maloney writes "Throughout the years, there have been many clones and re-implementations of the venerable vi editor. One variant of vi that emerged and stayed with us is VIM. Since its introduction, VIM has proven itself a worthy successor to the traditional vi editor. VIM has rightfully taken the place of standard vi implementations as the spiritual successor to vi, completely replacing the vi editor on many, if not all of the current Linux distributions. Many improvements have been made to VIM such as tabs, spell checking, folding, and many, many more. However many of these new enhancements may still remain hidden to anyone who isn't keeping up on the cutting edge of VIM development. Hacking VIM is a good resource for becoming more familiar with the new features of VIM and how to make them work best for you." Read below for the rest of Craig's review. Hacking VIM author Kim Schultz pages 210 publisher Packt Publishing rating 7/10 reviewer Craig Maloney ISBN 978-1-847190-93-2 summary A good way to wring more productivity out of an already excellent editor Hacking VIM is a short book, weighing in at a scant 210 pages. The book contains six chapters, and two appendices. The first chapter covers the history of VIM, and the lineage of vi clones that preceded it. Chapter 2 covers personalizing VIM. This chapter covers how to really take VIM and customize it for your own needs, from changing the fonts and colors for GVIM to personalizing the status bar, and using tabs. Chapter 3 deals with navigating better in VIM, whether it's in a singular file, or a group of files (which is especially important for several programming environments). Chapter 4 discusses the many productivity enhancements of VIM, such as templates, auto-completion, code folding, sessions, and the built in diff mode. Advanced formatting is covered in chapter 5, which has a few interesting tips on making code look better. Rounding out the book (and weighing in as the largest chapter of the book) is scripting VIM. VIM has excellent scripting capabilities, and this chapter covers them in great detail, from finding scripts to writing your own. Lastly, the Appendix covers some of the neat scripts available for VIM, such as a minesweeper game, and the obligatory Towers of Hanoi puzzle and mail client (because no software is considered done until it reads mail and news. :) )

Hacking VIM prefaces each tip with which version of VIM will work with each function. There were only a few instances where I noticed that a particular function was mis-marked as requiring a later version of VIM that actually worked with earlier versions. The book also contains good images which help demonstrate some of the more visual components of VIM, like tabs, folding, and the spell checker.

It is full of useful tips for getting the most out of VIM. The book is aimed at those who have already gained some familiarity with the VIM editor, and is by no means a tutorial for the novice user. There is clearly a bias in this book to the intermediate and advanced VIM users. Unfortunately, this is at odds with the first chapter, which starts with a history of the VIM editor. This wastes some of the space of the book, and would have been best used with more unique and different tips. Also, having some experience with VIM, I found certain tips weren't worth the trouble, and others quite confusing. The section on signs was a bit confusing, and I'm still unclear on why they're worth the trouble. There were several instances where I wondered what the productive benefit of a tip would be. On the other hand, I did find several tips invaluable. It's easy to overlook new functions in the CHANGELOGs, so I missed that newer versions of VIM had integrated spell-checking. Overall, Hacking VIM had enough good tips in it that I hadn't discovered on my own to make it worth the read.

Like most editors, VIM can induce editor fiddling sessions that result in little work being done, and Hacking VIM contains lots of fodder to make even the most ardent tweaker happy. Unless you carefully follow the mailing lists for VIM, and try every new feature as it is released, you might miss some really helpful productivity enhancers. My only wish for this book would be more focus on really productive tips, and less history about the other versions of vi that didn't survive. The book may have lots of "of course" items for the truly seasoned VIM user, but for those of us who don't keep up-to-date with the latest features, it is an excellent way to get more familiar with some of the truly great features that have been introduced in later VIM versions.

You can purchase Hacking VIM from amazon.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

48 of 308 comments (clear)

  1. vimdiff by loudmax · · Score: 5, Interesting

    A few months ago I heard someone raving about the usefulness vimdiff at a Perl user group meeting. I looked into it, and it's become one of my favorite tools. In addition to the regular syntax highlighting, it highlights differences in two texts and by default folds areas that are identical. It's fantastic for programmers and also for sysadmins like me who want to compare different versions of configuration files.

    I use vim every day, but I know I'm only scratching the surface of it's capabilities. There are probably a lot of others on Slashdot who use vim all the time and would stand to gain from understanding more of what it can do. I'll definitely give this book a look.

    --
    KTHXBYE
    1. Re:vimdiff by ScytheBlade1 · · Score: 4, Insightful

      I was simply going to mod you offtopic (two points left) - but instead I decided to reply. Mostly because I'm curious.

      Your comment is the typical argument: "emacs is better."

      However, you do not go into why it's better. You don't even mention a slight reason as to why it's better. You state that "the rest of us" "use more decent tools" and "snicker [at those who don't]."

      Would you mind qualifying your statements?
      1) What is a "more decent tool"?
      2) Why is this other tool better to use?
      3) Who is "the rest of us"?
      4) Why make this statement with nothing to back it up? If your statement is 100% qualified and "correct" - why not just give a few reasons as to why?

      I really want to know why your "more decent tools" are so plainly superior that you don't even bother to qualify your statements as to why.

    2. Re:vimdiff by jmorris42 · · Score: 4, Informative

      > the rest of us, who use more decent tools, can just snicker.

      I take it you are a code monkey developing mainstream apps, most of the more evolved ones are emacs folks. But admins and embedded folks are often working in diverse environments, many of which don't have emacs but any *NIX type environment will have vi. Busybox implements a vi clone. I seriously doubt you will find emacs on very many routers, access points, settop boxes, cell phones, etc.

      Since I use so many different machines it just makes sense to default to vi/vim and stick to assuming only the default behaviour. Emacs only makes sense if you mostly use a single home directory where you can have emacs customized.

      --
      Democrat delenda est
    3. Re:vimdiff by mOdQuArK! · · Score: 2, Funny

      *wark* *wark* *wark*

      Attempt at starting religious emacs vs vi. flame war has been detected!

      All civilians evacuate to the safety zones!

      Repeat,
      All civilians evacuate to the safety zones!

    4. Re:vimdiff by ScytheBlade1 · · Score: 2, Interesting

      Actually I was being 100% honest in my question(s). My extent of emacs usage is at my school during the "Intro to UNIX" class - which I completed in four hours (the entire class).

      Four hours of emacs is just enough to hate it, and not nearly enough to understand it.

      Again, honest questions.

  2. All of this talk of scripting vi made me think by jandrese · · Score: 5, Funny

    vim is clearly the emacs of vi clones.

    --

    I read the internet for the articles.
    1. Re:All of this talk of scripting vi made me think by Citizen+of+Earth · · Score: 4, Funny

      vim is clearly the emacs of vi clones.

      You referenced emacs in a vi thread. I invoke Goodwin's Law!

    2. Re:All of this talk of scripting vi made me think by RegularFry · · Score: 4, Funny

      I don't want to be a spelling Nazi, but that should be Godwin...

      --
      Reality is the ultimate Rorschach.
  3. vim syntaxt is quite arcane by pongo000 · · Score: 3, Insightful

    I was actually thinkink about this the other day: As a long-time vim user (have you donated yet?), the one beef I've always had about customizing vim is the rather arcane and inaccessible syntax style that's used throughout vim. This coming from a long-time perl hacker might come across as somewhat disingenuous, but it's the truth: I love to hack with vim, but hate to hack on it!

    I plan on checking this out only to see if there is some light shed on the secrets behind writing a vim plugin.

  4. VI SUCKS! by Seumas · · Score: 5, Funny

    Real men use emacs, bitches!

    Okay, not really -- but I thought someone should get that out of the way so we can move on.

    Besides, most people who say Vi or Emacs are the best secretly use nano/ae/pico when nobody is looking and we all know you do, too.

    1. Re:VI SUCKS! by rk · · Score: 2, Funny

      The most terrifying part of that page was the brainfuck interpreter implemented in TECO. *shudder* Someday when the stars are right, Cthulhu dreaming in R'yleh will run that program and unmake the world.

  5. Re:Vim is painful. by Anonymous Coward · · Score: 2, Funny

    What is this emacs thing you speak of ? Apparently it is "vile".

  6. A very powerfull tool of the past almost forgotten by deweycheetham · · Score: 3, Informative

    Most don't take the time to read the documentation http://www.eandem.co.uk/mrw/vim/usr_doc/index.html . One of the great thing about VI(M) is the ability to execute this in Batch Mode (i.e. Ex ). For that matter in these days of Microsoft Glory and GUI's that don't work so well, its nice to know that GNU Tools http://gnuwin32.sourceforge.net/ are around for all those pesky OS's that fall short of Batch Processing abilities of any sort to speak of. VI(M) also operates in this area quite well.

    Nice to know it's not forgotten.

  7. Re:Vim is painful. by Yold · · Score: 3, Insightful

    I think emacs and vi are both painful at first, if you are used to a fully graphical IDE. I tried desperately to get comfortable w/ emacs, to no avail. For the hell of it, I tried out vim, and the flexibility of keyboard commands has really helped w/ my sore wrists.

    I've been using vi extensively for more than 3 months, and I am still a newbie. It seems like the kind of editor you literally have to use for years to become competent with, but if you learn the basics its pretty intuitive for editing.

  8. As a linux neophyte... by Spy+der+Mann · · Score: 2, Interesting

    I use nano. It's enough for my basic needs, and doesn't depend on cryptic key sequences :)

    1. Re:As a linux neophyte... by css-hack · · Score: 2, Informative

      When I first started using *nix, I used nano too. It's got simple key combinations to do simple actions. I used it for years.

      I didn't start to use Vim until I started work at a software shop where the lead developers used it almost exclusively. I couldn't figure out why... but then they showed me how to customize parts of it. To add syntax highlighting and tabs, and to record macros (for doing the same operation on a bunch of different chunks of text). You can even enable mouse support when working in an XTERM (forget this GVIM thing).

      It was a steep learning curve that I probably wouldn't have endured if I hadn't been coding, but now I can edit any text more efficiently in vim than in any CLI or GUI program out there.

    2. Re:As a linux neophyte... by Hatta · · Score: 2, Informative

      That's what 'ssh -X' is for.

      --
      Give me Classic Slashdot or give me death!
    3. Re:As a linux neophyte... by Hatta · · Score: 3, Insightful

      Well there's a difference between being hard to understand and being hard to remember. VIM and nano are both easy to understand. VIM is just hard to remember. Nano gets a pass because it's got a crib right on the screen. Of course you can only fit so much on a screen, so there's only so much you can do with nano. Which approach is friendlier than the other is up to the user to decide. Personally I'll take the more powerful editor and just print out my own crib sheet.

      --
      Give me Classic Slashdot or give me death!
    4. Re:As a linux neophyte... by xaxa · · Score: 2, Funny

      M-x w3-fetch http://books.slashdot.org/
      C-s agree 100% (search for the comment I want to reply to)
      Enter (on the Reply to This link)

      C-c C-c to edit the multiline edit box

      That almost wasn't complicated enough. This post brought to you by Emacs!

      (Ok, the captcha was harder -- I had to download the MP3!)

    5. Re:As a linux neophyte... by Just+Some+Guy · · Score: 3, Funny

      I use nano. It's enough for my basic needs, and doesn't depend on cryptic key sequences :)

      I used to use pico until my mentor saw it open in my terminal one day. He uninstalled it and gave me a Vim cheatsheet. I cursed his name for about a week, then grudgingly committed myself to learning Vim for a few weeks, then eventually came to thank him.

      Then he introduced me to Emacs, and I'm still cursing his name.

      C-x C-s

      --
      Dewey, what part of this looks like authorities should be involved?
  9. Using vi by Anonymous Coward · · Score: 5, Funny

    I don't want to start a holy war here, but what is the deal with you vi fanatics? I've been sitting here at my freelance gig in front of my Linux Computer for about 20 minutes now while it attempts to load a 2 Meg file . 20 minutes. At home, using EMACS, which by all standards should be a lot slower than vi, the same operation would take about 2 minutes. If that.

    In addition, during this file transfer, Synaptic will not work. And everything else has ground to a halt. Even Firefox is straining to keep up as I type this.

    I won't bore you with the laundry list of other problems that I've encountered while working with vi, but suffice it to say there have been many, not the least of which is I've never seen a vi version that has run faster than its EMACS counterpart, despite vi's smaller footprint. My Windows 95 version of Notepad.exe runs faster than vi at times. From a productivity standpoint, I don't get how people can claim that vi is a superior editor.

    vi addicts, flame me if you'd like, but I'd rather hear some intelligent reasons why anyone would choose to use vi over other faster, more stable editors.

    1. Re:Using vi by Hatta · · Score: 4, Informative

      LOL, best troll EVER.

      Not only is this a simple edit of the classic Mac troll(scroll to the bottom), but he gets modded insightful and 8 people take him seriously. Very good job sir.

      --
      Give me Classic Slashdot or give me death!
    2. Re:Using vi by just_another_sean · · Score: 2, Informative

      He's just recycling an old joke. I found a copy of it here: http://kottke.org/order/apple - scroll to the bottom to see the original "I don't want to start a holy war..." troll. :-)

      --
      Creationist Textbook Stickers Declared Unconstitutional by CowboyNeal
  10. That would be 'vile' actually... by Rhys · · Score: 2, Informative

    vile = VI Like Emacs (http://invisible-island.net/vile/).

    Maybe vim has caught up. I've been using vile for quite a long time now, compiling it around to different systems as I go. Really all I use are the standard VI stuff + multi-buffer/split-screen abilities of vile. I should probably update myself to vim.

    --
    Slashdot Patriotism: We Support our Dupes!
  11. Re:Vim is painful. by morgan_greywolf · · Score: 5, Informative

    Nah. You can become competent with vi in a few days. Save yourself a lot of hassle. Print out one of these handy-dandy vi cheat sheets. This one's not too bad, but is poorly formatted, IMHO.

  12. Re:Why I dislike VIM by Anonymous Coward · · Score: 2, Informative

    to have a "No, I just want vi!" mode?

    Remove your .vimrc, or make "set compatible" the first line of it. Or use -C to force compatibility mode.

  13. Re:Why I dislike VIM by morgan_greywolf · · Score: 2, Interesting

    Remove your .vimrc, or make "set compatible" the first line of it. Or use -C to force compatibility mode. One minor point: the last one doesn't seem to work right if you have a .vimrc. For instance, if you set smartidents and syntax highlighting on in your .vimrc and then want to go to compatibility mode, -C won't turn those off.

  14. Re:I prefer EMACS! by Frymaster · · Score: 3, Insightful
    the non-intuitive (if short) command syntax of vi.

    okay... i hear this a lot, and not just on this thread, that the vim syntax is 'non-intuitive', and i'm starting to wonder why people say that.

    i mean, most people here are users of some sort of *nix, an interface where 'resolv.conf' is missing an 'e' for no apparent reason and cp uses a capital R for recursive, but scp demands a lowercase one. even those of us who use the allegedly 'easy' windows operating system are confronted with a shut down command located under a menu labeled 'start'. and we all speak english, perhaps the least intuitive syntax yet developed, where 'slaughter' does not, for some reason, rhyme with 'laughter' and words like 'cleave' and 'table' can have two meanings which are diametrically opposed to each other.

    we are, all of us, confronted every day with systems of syntax that are grossly complex, inane and massively counterintuitive.

    so why are you all picking on vim?

  15. Used both... by Lodragandraoidh · · Score: 2, Informative

    RSM must have a time machine tucked away at the FSF. Emacs was ahead of its time - and in many respects still is. Its 20 MB footprint is slim in comparison to other tools on the market today.
    Vi, via vim, is getting more bloated - and at some point will look enough like Emacs to make the matter moot.

    --

    Lodragan Draoidh
    The more you explain it, the more I don't understand it. - Mark Twain
  16. The best vi clone by hibiki_r · · Score: 4, Interesting

    I for one would rather use emacs, but if key combinations like ctrl+alt+meta+% are beyond your manual dexterity, the best vi clone is vigor

    A few years ago, I modified all of the system test environments at my workplace so that vi was just an alias to vigor. All of the administrators were thrilled with vigor's responses, including everyone's favorite: 'You pressed the right arrow key. Push OK to continue'. No OS can be considered mature (or senile) if Vigor isn't installed by default.

  17. Re:VI = Virtually Impossible by shoor · · Score: 3, Insightful

    Ah, you've never used ed, have you?

    --
    In theory, theory and practice are the same; in practice they're different. (Yogi Berra & A. Einstein)
  18. Re:I prefer EMACS! by Anonymous Coward · · Score: 5, Insightful

    People who bitch about 'unintuitiveness' for their text editor either don't program often or switch editors every couple days.

    Intuitiveness just means that it is not something you can just "figure out." It's not free, it comes with a trade-off. Intuitivity comes if you inject long nomenclature, multiple steps, wizards, lots of graphical icons, and so on. All these things serve as a means of keeping you from having to commit anything to memory, since you are able to visually 'figure it out' from scratch each time.

    On the flip side, an editor like vi trades intuitiveness for precision and speed. Sure, you need to memorize some keys and commands, but the end result is improved speed, productivity, and precision. Like all things worth learning, there is a curve, and it is painful, but there are benefits.

    Why software engineers seem to think intuitivity is something worth striving for in their tools is beyond me, very few other engineering tools strive for intuitivity. Can you just figure out how to use AutoCAD to design a house? What about a TI calculator to perform calculus? Can you just intuitively use a slide rule? Of course not, because if these tools were designed with intuitivity in mind, and not overall effectiveness when trained properly, people would not be able to be nearly as productive with them.

  19. I don't use vim for the fancy features.... by lena_10326 · · Score: 2, Interesting
    I use vim because it gives me a very satisfying feeling of get in, do edit, get out. It's like sniping... if that makes sense. I also religiously use its visual editing, which is one of the best methods of selecting text I've ever seen. Plus, vim just gives better sensory satisfaction.

    That and screen is the killer combo for me.

    .vimrc

    set nohlsearch
    colo pablo
    set expandtab
    set shiftwidth=4
    set tabstop=4
    --
    Camping on quad since 1996.
  20. VIM is useful... by david.emery · · Score: 4, Funny

    If you need to edit the makefile for EMACS... :-)

    dave

  21. coders vs. sysadmins by mungtor · · Score: 4, Informative

    Coders like Emacs, sysadmins like VI. VI is small, fast, and almost always there on any system you can get to boot. Emacs is feature-packed and almost and entire development environment in itself, but it is rarely in /sbin or somewhere else that it can be useful on a crashed system.

    Whether VI is good at handling 2MB files is generally irrelevant when you need to correct a typo in /etc/vfstab that's keeping one of your systems from booting. It may not be prefect, but it's better than ed.

  22. Shell support by c0nst · · Score: 2, Interesting

    As a long time Vim user my main gripe is lack of good, built-in shell support. There's a patch at http://www.wana.at/vimshell/ for creating a term emulator window inside Vim. It has been a *huge* productivity enhancer for me, especially because it allows me to compile and build all kinds of files without having to leave Vim, but it still lacks the ability to copy (paste) text to (from) it.

  23. Religion by PPH · · Score: 2, Funny

    vi is my shepherd. I shall not font.

    --
    Have gnu, will travel.
  24. Re:Vim is painful. by fsmunoz · · Score: 2, Funny

    I wondered, as I read the summary, how long it would take for someone to mention Emacs in the comments. Congratulations. It wasn't sooner because they had to change mode, only after 16 beeps did the sentence appear.
  25. Re:Vim is painful. by jinxidoru · · Score: 2, Interesting

    I am a vim zealot. I'll put that right out there. I am extremely fast at coding in vim. That said, I also want some of the more advanced features that exist in modern IDEs. As a result, what I generally do is have the same file open in two windows. One is my vim window, the other is my Xcode window. For the things that work best in vim (ie general development), I use vim. For the things that work best in an IDE (ie debugging), I use Xcode. I would love to merge these two. Are there any IDEs out there that have good embedded tools that allow you to run vim inside of an IDE editor window? That would be awesome. But, I have my doubts that such a thing might exist.

  26. Re:I prefer EMACS! by spun · · Score: 4, Funny

    What good is an unnecessary war between EMACS and vi without a comment touting the merits of ed?

    Ed is the standard text editor!

    --
    - None can love freedom heartily, but good men; the rest love not freedom, but license. -- John Milton
  27. Re:Vim is painful. by jdgeorge · · Score: 4, Informative

    You're not alone. For those who dig eclipse, there do appear to be efforts related to this, such as eclim, vimplugin, and viPlugin.

    May the source be with you.

  28. Re:I prefer EMACS! by Stamen · · Score: 2, Insightful

    I have no mod points to give you to pull you out of AC zero land, so I'll just respond that I totally agree.

    I can understand having simple tools for casual computer users or for learning purposes, but professional developers or admins who refuse to use something because it takes a little bit of work is disturbing. It's similar to programmers who can't type; seriously if you can't spend the 2 weeks it takes to learn how to touch type, then how much effort are you going to put into your craft?

  29. Re:Vim is painful. by jinxidoru · · Score: 2, Insightful

    It ever amazes me that the most benign, innocuous comments can be met with insult on Slashdot. It is truly amazing.

  30. Re:Vim is painful. by jrumney · · Score: 3, Funny

    Here's my concise cheat sheet, which I have pinned in a prominent place on my cubicle wall. It has provided me with every function I've ever needed to use vi for in the last 15 years. Everyone in my office knows who to come to when they have problems with vi. :q!

  31. Fistfight in a tunnel by tempest69 · · Score: 2, Informative

    I for one would rather use emacs, but if key combinations like ctrl+alt+meta+% are beyond your manual dexterity, the best vi clone is vigor A few years ago, I modified all of the system test environments at my workplace so that vi was just an alias to vigor. All of the administrators were thrilled with vigor's responses, including everyone's favorite: 'You pressed the right arrow key. Push OK to continue'. No OS can be considered mature (or senile) if Vigor isn't installed by default.
    ok, this one got me.. Why sysadmins go for vim.. first it's everywhere, which has been covers an-nauseum here.. But it doesn't need jack except the escape key to get through a system. Alt tab on a windows box, and poof, youve gone to a new window.. and it didnt do jack to your emacs session. VIM just needs escape..

    yes, Emacs can be set up great, and windows to ignore alt tab, and a myriad of other things. But VIM is slick, migrating from VI acolyte to EMACS is just heresy

    Storm

  32. Re:Vim is painful. by Thornkin · · Score: 2, Informative

    http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html is another good cheat sheet and a good tutorial.

  33. Re:Vim is painful. by seanmeister · · Score: 2, Informative

    I am a vim zealot. I'll put that right out there. I am extremely fast at coding in vim. That said, I also want some of the more advanced features that exist in modern IDEs.

    You might like PIDA - it gives you a real, honest-to-gosh vim wrapped in fancy IDE features.

  34. What about Joe...? by uweg · · Score: 2, Funny

    ...because WordStar key bindings became hardwired inside my spinal cord during childhood....

    ...ahh those CP/M days....