tadhg.com
tadhg.com
 

Time to Try Vim

09:14 Sun 09 Aug 2009
[, , , ]

There are three kinds of major text editors: Emacs, vi (now really Vim), and “other”. I’m sure there are some other quirky small ones out there, but those are basically the major categories. “Other” encompasses what I think most people would expect from a text editor: a window in which you can type freely and see your text appear, and which behaves somewhat like other applications, including word processors, in its basic functionality.

I’ve used the “other” category for years. I think that the first one I used heavily (I’m not counting MS-DOS Editor) was TextPad. I stuck with that for quite some time, and then started using HomeSite. I think I played with UltraEdit for a while too. Eventually, however, I wanted a cross-platform and free software editor, and shifted over to jEdit. I’ve used it heavily for several years, and consider it the best of the “other” class.

I have, however, always thought about making the effort to learn vi, because I’ve always loved the keyboard-only approach and like to save time by making my text editor use more efficient.

I’ve been quite happy with jEdit recently, primarily because I’ve been writing macros for it in Jython. However, last week I made the mistake of applying an OS upgrade to my OS X laptop (currently my main machine), and this screwed up the font display in jEdit, which I found and still find rather aggravating. Further, I’ve had trouble getting the Jython plugin to work in the next version of jEdit, and on top of that am a little irked by the fact that it only supports a rather old version of Jython. The font issue isn’t really the fault of the jEdit team, but is still highly annoying (it’s amazing how much better the same font looks on my other, old, laptop, which I didn’t apply the same Java update to). The Jython thing, however, is cause for concern because it’s clear that what has become a crucial feature for me is an obscure niche.

These things (as well as comments from certain Vim evangelists) made me wonder whether it’s time to try switching to Vim. It supports scripting in Python (directly, not via Jython), and it seems as if this is a well-supported feature likely to remain up-to-date. It’s cross-platform, and free software. It’s highly programmable.

The reason it’s a big deal, however, is that it’s not like other editors. To use it, I’ll have to essentially unlearn years of knowledge and habits about how to manipulate text. The vi family of editors doesn’t assume that you’re automatically entering text whenever you hit a key, but rather has editing modes. Insert mode lets you enter text as per usual, but in “normal mode“ keys have other meanings that provide powerful functionality to alter the text. This sounds pretty simple, but in order to use it properly, you have to know what a lot of those meanings are.

To write a line like this one in jEdit, and then e.g. delete everything after the first comma and replace the comma with a colon, I would hit the following keys after having typed the text:

<Home>, <Cmd-Left Arrow (held down)>, <Shift-End>, <:>

The second action in that sequence would take a while, as I’d be waiting for the cursor to move over.

In Vim, I’d first have to hit <i> to enter edit mode, then type the text, and then I’d have a lot of choices, but it might be something like:

<Esc>, <^>, </>, <,>, <Enter>, <c>, <$>, <:>

Meaning: enter edit mode, go to the start of the line, enter search mode, search for a comma, delete the rest of the line and enter insert mode, colon.

Or:

<Esc>, <?>, <,>, <Enter>, <n>, <c>, <$>, <:>

Which searches backwards instead of going to the start of the line.

That looks pretty nuts, and I’m sure there are a better ways that I’ll discover at some point, the key is that at the moment it requires a lot more thought on my part to figure out how to do basic edit constructions. The Vim approaches I list above are quite fast, and don’t require waiting, but the jEdit way, which is also how I’d do it in just about any other editor, comes very naturally.

The thing about the vi way, though, is that it has the promise of eventually making text editing much faster for me. There’s the initial frustration and difficulty, but further down the line, more and more complicated text mainpulation is reduced to very fast sequences of keystrokes—most of which are designed to keep your fingers near the home row.

I’m not giving up on jEdit, but I’m going to finally make a serious effort to get to grips with Vim over the next month or so. I’ll be trying out MacVim, and will attempt to use it for everyday writing and coding, which will include both learning how to edit text in the damn thing and porting my various Jython macros over to it.

8 Responses to “Time to Try Vim”

  1. Barak Says:

    Cool!

    Thinking in Vim mode is def. an uphill climb at first, but once it gets burned in, you miss not having it.

    There are a bunch of cool plugins — I use SuperTab, TabBar and surround.vim extensively.

    Also, remap your CapsLock to Ctrl if you haven’t done so already (it’s easy on Macs — “Modifier Keys” under Keyboard and Mouse settings). Aside from making just about everything better, it gives you an alternative to , in close reach. I also like mapping Ctrl-A and Ctrl-E to their (emacs-like) shell meanings of Home and End, superior to $ and ^.

    Look into the and motions and also :help text-objects (cib ftw)

    –Barak

  2. Seth Milliken Says:

    dF,

  3. Seth Milliken Says:

    Oops. Misread. You can still save a keystroke using f (and I can pedantically illustrate the complementary t command as well): ^f,ct:

    FWIW, your original jEdit editing sequence could be made to work in vim without much difficulty, too.

  4. Tadhg Says:

    Barak: Thanks for the tips, I’ll look into those as soon as I overcome the basic hurdles of file management and plugin setup, the former of which is proving difficult to adjust to. I will have to remap Caps Lock to Control for Vim work… I heretically have it remapped to Command on my Mac machines at the moment.

    Seth: heh, I encountered the f command myself while looking through the help and realized that I could save that keystroke. As for making the jEdit sequence work, I feel that would be counterproductive in terms of learning the Vim approach.

  5. Niall O'Higgins Says:

    Unfortunately Mac OS X makes it extremely difficult, if not downright impossible, to map Caps Lock to Escape (which is trivial under both X-Windows and MS Windows). Caps Lock to Control is workable though, I suppose, even if it forces unnecessary chording.

    Anyways, I think the most essential things in modern Vim are (in no particular order):

    - Tabs (:tabe instead of :e), then gt/gT to switch
    - A to append to current line, I to insert at beginning of current line, o to open a new line below current line, O to open a new line above current line etc etc
    - Visual mode (V/v) + cut/copy/replace commands
    - Recording mode (:help recording)
    - Context marks (:help mark)
    - `ciw’
    - `gqip’
    - VCSCommand plugin (like VCS support in Emacs)
    - OmniComplete is sort of nice sometimes.

    Also, Vi and Vim are kind of biased toward KNF C. To write Python in a sane, PEP 8-compliant way, you have to do something like this article: http://henry.precheur.org/2008/4/18/Indenting_Python_with_VIM.html

    I still haven’t found a good file/project tree browser for Vim, even after trying a bunch. I think that Emacs is still better in this area.

  6. Tadhg Says:

    Niall: You could try something like mapping CL to Command and then within Vim getting Command-Enter to work like Escape; that’s what I’d try if I wanted to go that way.

    Tabs will likely be very important to me when I figure out my approach to file management in Vim. Currently that’s still an obstacle.

    A, I, o, and O are all extremely useful, and I’m sure visual mode will be when I get to using it. Recording doesn’t seem as useful, but I could be completely wrong there. “ciw” looks great, and I’m sure I’ll find uses for “gqip” as well. I’ll have to look into VCSCommand and OmniComplete.

    I came across those tips for editing Python, they’re pretty useful. I’ll have to investigate, but I might have to write my own script to control indentation for reST.

  7. Seth Milliken Says:

    I prefer “caw” myself, since I can type it with one hand. NERD tree seems like a fine filesystem browser, but I don’t know what your requirements are. Mapping Caps Lock to Escape seems like a waste; Ctrl-C works just fine for me for switching back to normal mode, and as a bonus it gets you out of cmdline-window where Esc doesn’t.

  8. Seth Milliken Says:

    It occurred to me that that problem you have with vim’s list and linebreak being mutually exclusive is actually just a deficiency in the rst.vim syntax. What are some of the specific indentation issues that are problematic?

Leave a Reply