Wednesday, January 02, 2008

Vim: Gimme More Magic

I spent an hour reading about TextMate to get a feel for what I'm missing since I know it's so popular. I'm sure it's a lovely editor, but I have such an emotional attachment to Vim, Linux, free software, etc. Anyway, I'm sure you're tired of hearing me talk about that ;)

Anyway, I tried out the snippetsEmu plugin. It's a plugin that provides TextMate style snippets. Once you have it installed, you can edit a Python file, type "def<tab>" and it'll start doing interesting things.

I can see the appeal of snippetsEmu for more verbose languages. It knows how to type "public static void main" ;) However, it's hard to get excited about it in Python. Python has such low syntactic overhead that it's faster for me to just type things out, especially considering I'm a touch typist. It'd probably make more sense in Ruby, where you have to type "end" all the time. Nonetheless, I'm happy to have someone tell me how to use snippetsEmu more effectively.

I also tried out xmledit. That one's a keeper. Once you have it installed, you can type "<p><tab>" and it'll automatically add "</p>". Hit ">" again, and it'll even put the two tags on separate lines and position your cursor between them. Very nice! The one trick to remember though, is that when you install xml.vim into your ~/.vim/ftplugin directory, remember to symlink it to html.vim so it works for HTML too.

9 comments:

Arnar Birgisson said...

Hey there,

I'm an avid Vim fan as well, it is like my third arm. However, I fell for TextMate as well and bought a license. I now use both pretty much fifty fifty, depending on what kind of editing I'm doing. I especially benefit from how TextMate handles projects.

As for snippets, yes, it is hard to get excited over them for plain Python. However, I have found them to be very useful in the Python context, for example when doing repetitive stuff like SQLAlchemy tables and mappers, other ORM models, and in general any code that is has to have some specific form to be used by libraries through introspection.

For an example, I have two snippets triggered by "table" and "col" for writing SQLAlchemy table definitions. "table" expands to

xxx = table("xxx",
[cursor here]
)

where xxx are synced, and "col" expands to

Column("xxx", Integer, nullable=false)

with the cursor going through the parameters in order to change or remove them.

So, for me at least, snippets are very useful in Python, although not for plain vanilla Python but rather for library-dependent code format.

xmledit looks good, thanks for that pointer.

Anonymous said...

I just switched back to Linux from about a year on OS X (with TextMate etc).

I really never used the snippet things, ever. I read about them on multiple occasions, tried some out and just never felt much interest.

I did like the simple "jump to a file in my project with name like X" but I can do that easily (and not just for files but classes, functions, etc) ctags + tselect. I'd say you aren't really missing anything.

bimone said...

For the snippets stuff, what about iabbrev on vim ?
You don't have the "Tab make me switch to the next xxx" thought, but there is a way to do that in Hacking vim.

Viva vim !

BTW I just discovered c% (in the Office vi plugin blog) and using f to navigate throught long lines, editing CSV with ; as the C, with
cf; and f; to navigate.

Shannon -jj Behrens said...

> So, for me at least, snippets are very useful in Python, although not for plain vanilla Python but rather for library-dependent code format.

Awesome comment. Thanks!

Shannon -jj Behrens said...

> BTW I just discovered c% (in the Office vi plugin blog) and using f to navigate throught long lines, editing CSV with ; as the C, with
cf; and f; to navigate.

Can you write more? I know about f and I know what CSV is, but I don't understand the rest of what you're saying.

Thanks!

bimone said...

The message was rather cryptic ... sorry. I was refering to this link tip.

Example #2: smart ranges

Right now I'm listing all my book in a file, to become the input for a toy webapp. The format is CSV. Let's say I have this line:

(author);(title);(publisher)
Isaac Asimov ; Fondation ; Folio_SF

I have another book of Asimov, so I do

* Yp to duplicate the line
* j to go one line below
* f; to go to the first ;
* cf; to replace the book name and go to insert mode, and type the name of my second Asimov book. Instead of using c%, my {motion} is f; -> cf;

bimone said...

The viemu guy explains how he came to vim, and that's quite fun. I think that with all the mobile platform (eee pc), vim is gonna be handy to type, that's for sure. (just having to hit the delete on my laptop hurt me while typing this, instead of Ctrl-w or Ctrl-h ...)

BTW, do you know vimperator ? It has some quirks (I is the magic touch for escaping all chars (gmail or FB, sites with lots of JS I believe)), but it's working well for me.

Shannon -jj Behrens said...

> The message was rather cryptic ... sorry. I was refering to this link tip.

Great comment. Thanks for the link!

Shannon -jj Behrens said...

> BTW, do you know vimperator ?

I haven't taken that dive yet ;)