Tuesday, 3 December 2013

tmux and vim for Scheme development

I’m currently working my way through Structure and Interpretation of Computer Programs, as a lot of people I know speak very highly of it as a text on the craft of programming.

It uses the Scheme programming language, a dialect of Lisp. Most tools for developing in Lisp and Scheme (e.g. SLIME) are based around the emacs text editor. Without going down the rabbit hole, suffice it to say that I’m a vim user, and wanted an alternative.

My first thought was to use slimv.vim, which apparently provides vim users with similar functionality to SLIME. Unfortunately, my attempts to get slimv.vim to play nice with mit-scheme on OSX lead to frustration; I may revisit that at a later date, but in the mean time, I needed to get something working.

Enter tmux. tmux is a terminal multiplexer in the vein of GNU screen. One of its main benefits over screen is that almost every action in tmux is scriptable via the command line, which lends itself nicely to automation and integration with other programs.

For example, we can load the contents of a file into a tmux paste buffer…

$ tmux loadb <filename>

… and then paste the contents of a paste buffer into a specific pane in tmux.

$ tmux pasteb [-t [<session>:]window[.<pane>]]

If a target isn’t specified, the currently active pane is used. Windows can be specified by name or by number.

To create a new pane:

$ tmux splitw [-h] [cmd args...]

(The -h creates a side-by-side split; by default, tmux splits are stacked vertically.)

Finally, we can create a mapping in vim, which saves the current file and then shells out to tmux, reads in the contents of the current file, and pastes it into a target pane.

:map ,t :w \| !tmux loadb % && tmux pasteb -t 0.1

I can now have a scheme REPL side-by-side with my editor, and whenever I hit ,t my code will be saved and run through the REPL.

A quick screencast to demonstrate:

Written with StackEdit.

No comments:

Post a Comment