Two Things I Learned About Vim from an Emacs User
Yesterday I was working on a problem with an Emacs wizard, and I saw him do a couple things in Emacs that made me squirm. You see, I badger and belittle him at every opportunity for not embracing The One True Editor, but I saw him perform two actions in Emacs that I didn’t know how to do in Vim. And no way was I going to admit that, at least not to him.
Here are the two items:
When he ran a command from a shell with a lot of output, he piped the output directly to Emacs like this:
$ some_command | ecbuffer
I’m on a Mac, so I typically do this:
$ some_command | pbcopy
And then I open a new MacVim window and hit Shift+P to paste (I use the system clipboard in Vim). Inelegant in comparison. So, while his back was turned, I googled and discovered that passing – to vim causes vim to read from stdin, so I can just do this:
$ some_command | mvim -
Works great! I breathed one sigh of relief, but still had a second item to tackle: we were trying to solve a build issue in which a required jar file was not being included in the 1.2GB deployment file (that’s not a typo — we really roll that large). I noticed that when he opened a zip file in Emacs, it churned for awhile (hey — 1.2GB is a lot of data!), and then showed the zip file’s table of contents. Cool! My best thought for how to do that in Vim, using what I’d just discovered, was this:
$ jar tf foo.zip | mvim -
Not bad, but not as cool as what my coworker was doing in Emacs. So, before getting too worked up, I tried opening the zip file in Vim to see what would happen. Immediately, the table of contents for the zip file appeared in Vim. No churning. No wait. And then I could press Enter on one of the file names to view the file contents.
So, my Emacs-loving coworker made me learn two cool things about Vim. I need to work with him more often!
Never leave the Vim session!
:read !jar tf foo.jar
Or, just visit any archive file with :Explore
or, just
:e foo.jar
For build issues I use command :make. You can configure that command for many pre-build, post-build steps and so on. I beleave you knows it.
Never leave the Vim session! 🙂
To be absolutely fair, my ecbuffer function is pretty complicated:
function ecbuffer {
TMP=”$(mktemp /tmp/emacsstdinXXX)”
cat > $TMP
emacsclient -n –alternate-editor /usr/bin/false –eval “(let ((b (create-file-buffer \”*stdin*\”))) (switch-to-buffer b) (insert-file-contents \”${TMP}\”))”
rm $TMP
}
Yeah, Sean, that’s WAY better than “vim -” 😉
The advantage to my approach is that it doesn’t open a new window, it inserts the contents into a new buffer of an already running emacs.
s/advantage/difference/