Evaluate Scala code in a REPL with Vim, Tmux and Slimux
I’m a Vim user and a Scala developer, and I usually write my code in Vim while running SBT in a Tmux split pane but I’ve never been completely satisfied with this setup since there is no real integration between Scala and Vim.
Because of that, I’ve been looking for a long time for a Vim plugin that would allow to interact with a Scala REPL in the same way as Tim Pope’s amazing vim-fireplace for Clojure: vim-fireplace
lets you evaluate Clojure expression in a REPL running in background and see the results in Vim, which this makes the feedback loop incredibly short and allows for small, fast iterations.
Last week I found out about Slimux, which lets you send snippets of text from a Vim buffer to a specific Tmux pane. Now, what happens if there is a REPL running in that Tmux pane? Yeah, you got it: you can evaluate code!
While the integration is not completely comparable to what you get with vim-fireplace
(results are shown in the REPL itself, not in Vim), slimux
is language-agnostic (in fact, it just sends text to a Tmux pane), so it can be used with just any language that comes with a REPL!
You might be thinking “Ok, how is this even useful to me?”…
Personally, I often want to quickly try out some random code in a REPL (think of Scala IDE’s worksheets) and slimux
allows me do it comfortably from vim
, which is a huge improvement compared to typing in the console.
After installing slimux
(instructions on their website), I recommend you to add mappings for the most common commands to your .vimrc
. For example, I currently have the following:
nnoremap <Leader>sl :SlimuxREPLSendLine<CR>
vnoremap <Leader>ss :SlimuxREPLSendSelection<CR>
nnoremap <leader>sb :SlimuxREPLSendBuffer<CR>
Let’s verify that everything works! Open a Tmux pane and start a Scala REPL:
scala
Now create a buffer in Vim and write any expression, say:
1 + 1
Put your cursor on that line and run the command :SlimuxREPLSendLine
(<Leader>sl
if you are using the above mappings): you should now see the expression being evaluated in the REPL. Have fun!