Jach's personal blog

(Largely containing a mind-dump to myselves: past, present, and future)
Current favorite quote: "Supposedly smart people are weirdly ignorant of Bayes' Rule." William B Vogt, 2010

Vim and [language] Interpreter

I've seen emac's lisp mode, and I'll admit it made me somewhat jealous since it wasn't as easy (and at first glance seemed impossible) to do in vim. But I've learned a few more neat tools, and now combining vim with a bunch of other *nix utils results in an awesome split-screen window with vim and any language interpreter you like. This is one reason I like vim: it doesn't try to be everything. You use other tools to enhance it, sure, but it doesn't include them by default.

What you'll need (you should be able to get these from your Linux distro's package manager):

vim (compiled with xterm support, Gentoo USE flag is vim-with-x)

For additional information, I'm using gnome-terminal, with the following versions of the above software:

kevin@jachoo ~ $ screen -v
Screen version 4.00.03 (FAU) 23-Oct-06
kevin@jachoo ~ $ xsel --version
xsel version 1.1.0 by Conrad Parker <>
kevin@jachoo ~ $ vim --version
VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Sep 5 2009 21:45:59)

So now let's run the commands. Open up a shell, and run the following sequence.

$ screen

$ screen # after other screen has started
Ctrl+a S # this will split the window
Ctrl+a <tab> # this will move focus to the other side
Ctrl+a " # this will give you a list of screens to select. Select the one not in use on top.

So you can now use ctrl+a (n | p | <tab>) to switch between the splits. Need to resize one? ctrl+a :resize +/- n will resize plus or minus n lines.

If you're not impressed already, open up some file in the top half with vim and open a Python interpreter in the bottom half. I typed this code in the file:

def greet():

print 'Hello!'


And copied it to the OS clipboard with the command gg"+yG The gg goes to the beginning of the file, the "+y is for yanking to the OS clipboard, and G goes to the end of file. You can also specify n lines or 'mark, or you can enter visual mode and type "+y after selecting text to yank.

Switch to the bottom half, and right click and paste (unfortunately the Python interpreter seems to override gnome-console's ctrl+shift+v command to paste):

>>> def greet():

... print 'Hello!'
>>> greet()

Cool? Oh yeah! Try some Scheme now. Open up t.scm with vim in the top, and run a Scheme interpreter in the bottom. (I use guile, but it's so minimal it's almost frustrating.)

(Top half)

(define dx 0.0001)
(define (der fn x)
(/ (- (fn x) (fn (- x dx))) dx))
(der (lambda (x) (* x x)) 4)

(Bottom half)

guile> (define dx 0.0001)
(define (der fn x)
(/ (- (fn x) (fn (- x dx))) dx))
(der (lambda (x) (* x x)) 4)
guile> guile> 7.99990000000861

So you can see guile isn't the best at this, but hey, it works fine and would probably work better in your scheme interpreter of choice. (I've yet to discover a flag to turn off the prompt in guile.)

Now maybe you're thinking "That's a lot of work!" I agree. So let's edit the .screenrc file!



This is just a series of commands to get the split screen functionality of two separate terminals. So now you just have to type "screen" by itself, and voila!

We're not done! (I haven't mentioned xsel yet.) It would sure be nice if we could go the other way; that is, copy stuff out of the bottom interpreter screen and paste it into vim, without using the mouse. Screen lets you go into a copy/buffer mode by pressing ctrl+a [ and it will let you move around with vim keys or arrow keys.

In order to copy text, you highlight a start character and press >, and move around to highlight more text, and press > again to finish the copy. Unfortunately this copies to screen's own buffer, not the OS's clipboard, so I think it's kind of worthless. But we can fix that again with .screenrc.

bindkey -m > eval "stuff ' '" writebuf "exec sh -c 'xsel -b < ~/.screen_exchange'"

I put that at the top. What it does is remaps the > key to save the buffer somewhere (for me, it's ~/.screen_exchange. It might be somewhere different for you). Thus when you press > to set the end marker, it runs that command and copies the text to your actual OS clipboard. Now you can press ctrl+shift+v in another terminal to paste, or ctrl+v in something else to paste, or "+p in vim to paste.

That's pretty much it now. With vim, the goal should be to rarely need to use the mouse. Using GNU screen helps that goal tremendously, and xsel completes the package. Vim is fairly minimal compared to emacs, but it makes use of the native *nix apps much more nicely, and so it doesn't need to have tons of features. Enjoy!

Posted on 2009-09-06 by Jach

Tags: awesome, programming, vim


Trackback URL:

Back to the top

Back to the first comment

Comment using the form below

(Only if you want to be notified of further responses, never displayed.)

Your Comment:

LaTeX allowed in comments, use $$\$\$...\$\$$$ to wrap inline and $$[math]...[/math]$$ to wrap blocks.