This is a read-only archive!

Vim vs. Emacs (scripting)

In my ongoing quest to force myself to learn Emacs, I used it today to hack some Ruby scripts.

Vim comes with a Ruby syntax highlighting script, but Emacs doesn't. That's one Emacs annoyance: You have to go fetch Emacs' ruby-mode scripts from somewhere yourself. That's sort of a common thing I'm finding with Emacs. For many things that Vim has built-in or bundled-in, Emacs requires you to go on a treasure hunt.

As another example, I wanted to display line numbers down the left side of my buffer. like seeing line numbers, because it tells me my position in a file at a glance and it shows me wrapped lines easily. And most compilers give errors in terms of line numbers. Vim gives you a black-box built-in solution to this: :set nu.

Emacs on the other hand has no built-in way to do this, that I could find. It has options for displaying the current line number in the modeline, which isn't quite what I want. I had to go looking for 3rd-party scripts to give me my column of line numbers. I found multiple scripts do it, in fact. The first I tried, setnu, seemed to work OK, but after a while it somehow choked and I had read-only line number text splattered throughout one of my buffers. This is clearly not acceptable. Another I found, linum, hasn't died on me yet, but we'll see.

This brings up an interesting point, I think. In the video I talked about before, one question Peter Seibel asks is which you'd rather have, a programming language that has 1,000 built-in functions but doesn't let you define your own, or one that gives you 10 built-in functions but lets you define your own. The answer is probably the one that lets you define your own.

Vim is like the editor with a thousand built-in functions, and Emacs is like the editor that lets you define your own easily. Of course the analogy isn't perfect: Emacs has tons of built-in things, and Vim has Vim-script and remappings etc. to let you customize it. But the point is that Emacs gives you much nicer, lower-level primitives to work with to customize Emacs at a level of granularity that Vim doesn't provide in some cases.

For example, I don't know if there's any way in Vim to make your own read-only column of text down the side of an editable buffer. If Vim didn't have :set nu, could you write it yourself in Vim script? I don't think so, or if so, it surely wouldn't be easy. I'd imagine you'd have to resort to something in C. Vim's built-in :set nu is implemented in C. If I wanted to change how the line numbers look or act in Vim, can I customize it? Only to a certain point: I can customize it only using the options the Vim devs gave me.

Emacs on the other hand gives you accessible, lower-level primitives to work with. So people can and did write their own scripts to implement a column of read-only line numbers; they're written in Elisp. The multiple scripts to do this are different and work in slightly different ways. They're also customizable if I don't like how they work. I don't have to hack into the guts of Emacs to do this; I can do it all in my ~/.emacs.

So I think one area that Emacs totally blows Vim out of the water is in terms of editor customization via scripting. Elisp is leaps and bounds nicer to write than the barftastic mess of crap that is Vim script. Any sufficiently complex Vim script will end up being a tangled heap of evaled strings and mode-switching and gibberish. In Emacs you have a tasty Lisp dialect, powerful and extensible and consistent and regular. And you can do crazy stuff in Emacs like embed shells or Ruby or Lisp interpreters right into the editor, which Vim doesn't let you do.

On the other hand, Vim's :set nu just works, whereas one of the Emacs scripts I tried did not. For a given feature, like displaying line numbers, which would you rather have, a feature written and tested by the Vim devs themselves, or something unofficial that some random hackers (who may be skilled or entirely unskilled) threw together as a 3rd-party extension?

The two cases degenerate into each other. A language or program with so many built-in features that it does EVERYTHING you want is probably just as good as (or in some cases better than) a language or program that lets you define your own ways to do things.

It depends on how much you actually want or need to customize your editor, but I think for my needs, Emacs actually ends up a bit better than Vim in this case. God help me.

January 07, 2008 @ 12:48 PM PST
Cateogory: Programming
Tags: Ruby, Emacs, Vim


Christopher Giroir
Quoth Christopher Giroir on January 08, 2008 @ 2:47 AM PST

You finally hit on why I like emacs :)

People like things that do their job well... Emacs often gets bashed for this reason. I disagree. The base of emacs is incredibly simple and powerful and that's the reason it gets expanded upon. I love the fact that I can rebuild the editor in many different ways by defining my own complete custom functions, but anyway!

About the ruby mode, I think one reason it's not included is that the official ruby distribution includes an emacs mode that they keep updated. In gentoo isn't it just emerge ruby-mode anyway?

Quoth Brian on January 08, 2008 @ 4:55 AM PST

Yeah, it's easy in Gentoo, but I use other OSes too that make you fetch it manually.

Christopher Giroir
Quoth Christopher Giroir on January 08, 2008 @ 7:40 AM PST

Very true, I've used so many where even though they include ruby and emacs, they don't have the ruby emacs files... seems a total waste. They are right in the ruby distribution ready to be installed!

Ivar Refsdal
Quoth Ivar Refsdal on January 13, 2008 @ 8:59 PM PST

How is the Emacs struggle? ;)

"For many things that Vim has built-in or bundled-in, Emacs requires you to go on a treasure hunt."

Well, I do like Emacs' (Slime's really) fuzzy autocompletion better than Vim's omnicompletion. Same goes for the bookmarking utility (not found in Vim). (Read the manual -> Bookmarks) I also like the the tramp (/user@host:/some/file/here) far better than Vim's (keeps asking for a password over and over again..) scp:// Emacs also has flymake,, you might want to have a go with that instead of line numbers for compiler/syntax stuff? Slime has slime-next-note for going to the next compiler error note. M-n and M-p (previous) is the default keybinding I think.

Feel free to correct me if you know any better than the above.

And I do CERTAINLY agree Emacs could have more sensible defaults. Like the buffer switching you were talking about.

For web stuff and lisp, I'm trying to learn weblocks: Have you tried it? It looks very good, although it has been a bit confusing.


Quoth Brian on January 14, 2008 @ 6:00 AM PST

I'm actually almost enjoying Emacs now. (almost)

I have looked at Weblocks, but it seems a bit young and yes, highly confusing. And probably overkill for my needs

I will look into flymake, thanks. I agree Emacs' autocompletion is very nice and Vim's isn't so much. I've never found any use for Vim's.

Quoth dum8d0g on April 18, 2009 @ 8:30 PM PDT

You can use

set completeopt=longest,menuone

and then do '^p' everywhere you want. You will get a compete list of possibilities in nice menu and it's searching thru all included files too (great for contants from c/c++ headers). And if this is not enough, you should read about '^x'. Especially the '^x^l' is the killer.

David Brady
Quoth David Brady on August 19, 2010 @ 8:29 AM PDT

You may already know this by now but emacs 23 ships with linum-mode; I find it quite good.