blog: Quick and easy testing of Python code with Slime for Vim
Like many Python developers out there, I’m a vim user. Once you get past the initial hurdle of the key mappings and understanding how vim works, it’s pretty great not only for rapid prototyping of code but also for most projects. I haven’t really used an IDE in a few years.
One annoyance I had was testing Python code. You’d have a separate tab open running a Python shell, most likely copying and pasting code into the shell as you retry things. Sure, unit tests are great but for some quick logic pondering it wasn’t the most optimal.
With this in mind, let me introduce you to Slime. Slime has an awkward name but is very cool. It’s a simple plugin that allows you to quickly send any code to a separate screen process. You can send a selection of code within your file or the entire file. It’s very easy. So how do you use it?
First, you will want to check out this blog post to fetch the slime.vim file. Once downloaded, plop it into your ~/.vim/plugin/ folder.
At this point, you’re almost done. You will need to setup a screen session. Ever heard of screen? It’s great for many things, but I won’t get too much into that. We’ll setup a new screen session to house our Python shell like so:
# Open a new Terminal tab to run the screen session in.
# "devshell" is just my example name. You can call it whatever you like
screen -S devshell
python
And that’s basically it for the screen part! All you did was setup a screen process with a python shell that is now running until you kill it. If you close your tab, no problem — You can re-attach yourself to that screen process using screen -r devshell. You can go a lot further with it and even connect remotely to a screen process (I use screen with irssi all the time).
Next, we want to actually use Slime from Vim. You have your Python shell running, now all you need to do is send some code there. Open up a vim instance, type in some code.
# Obligatory fibonacci code.
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
# Testing the python code.
fib(0), fib(1), fib(2), fib(3)
Now send it to the screen.
- If you have other code in your file, select this code using a visual block to only send the selection to the shell.
- Press C-c C-c (CTRL-C Twice)
- You will be asked for the screen name, enter “devshell” as per the example.
- Window can be left as 0 unless you changed it.
- Next time you do this, you won’t be asked for the screen name. It remembers it per each run
Now look into your screen tab, you will see the Python code has been executed the results have returned as follows:
... fib(0), fib(1), fib(2), fib(3)
(0, 1, 1, 2)
And that’s it! You can see how much easier that is then copying and pasting code into the shell, or just playing in the shell, since you get the power of vim in this case.
Finally, if you’re quick you will realize you can use this exact same thing for any interpreted language. Try it with ruby, clojure, bash, etc.
Resources & References:
- Like Slime, for Vim (Author of slime.vim, I take no credit for it.)
- screen quickref