Esa-Matti Suuronen

I make web gadgets

Slimux - Tmux Plugin for Vim

| Comments

Lately I’ve gotten really in to tmux and I’ve been looking for ways to integrate it to my Vim workflow. As result I ended up creating this Vim plugin.

If you are unfamiliar with tmux, it’s best described as alternative to GNU screen or as Tiling Window Manger for console applications. For fast start I can recommend a book from The Pragmatic Bookshelf - tmux: Productive Mouse-Free Development

The plugin takes its inspiration from Emacs Slime as well as from other similar Vim plugins.

The idea behind it is to make interaction with Read Eval Print Loops (REPL) easier. You can select for example portion of Ruby code execute it in Interactive Ruby (irb) with just one command. Also another goal is to make running of build and test commands a breeze.

Usage goes like this :

  1. Create a tmux pane and start your REPL in it
  2. Select some code and hit the keyboard shortcut (you must configure this)

Slimux now prompts the pane from you:

Configuring Slimux

Here’s a list of all your tmux panes. The syntax is read like this

1
[session name].[window index].[pane index]

Slimux will then remember that selection and won’t ask it again unless you explicitly want to reconfigure it. After that Slimux sends the code to the REPL and executes it.

Here’s an ascii.io screencast of Slimux in use. I don’t use any custom keyboard shortcuts in it to make it bit more clear what is going on.

For detailed command and install instructions refer to project README on Github:

https://github.com/epeli/slimux

Wait, yet another tmux plugin?

Yes, there are quite a few tmux plugins for Vim already and this plugin got started pretty much by accident. I started by fixing some bugs from vim-slime. One thing I wanted for it was an interactive prompt for tmux pane selection. Typing the target pane manually is kinda pain. I started working on that feature on a empty plugin and soon I realized that it was actually easier to grab features from vim-slime to my plugin than to merge my work back to upstream. That was mainly because vim-slime supports GNU screen which I don’t care at all. Every this heavy GNU screen user should just upgrade to tmux anyway.

Then there is Vimux which on the surface seems to do every thing as Slimux, but there where few annoyances for me. It takes too much control of tmux. It creates the tmux pane for you where you must run your commands/REPLs. I want to manually configure my tmux. Slimux allows you to manually select the tmux pane. Very often I want to run the REPL or commands in whole another tmux session so that I can put it to my second monitor. This also means that Slimux works just fine with GVim.

Another issue is that Vimux doesn’t have hooks to preprocess code by its type before sending it to REPL. For example you have to remove the extra line breaks from Python code to make it work with its REPL.

But do read their introductory blog post. Good stuff anyways.

Also I just kinda wanted learn some Vim Scripting and this is my first proper Vim plugin I’ve written. Feedback is really appreciated.

How to Write CoffeeScript Efficiently

| Comments

I have found few tricks that makes writing CoffeeScript more efficient and fun, especially when learning it and I’d like to share it with you.

These tricks are for Vim, but the ideas can be carried out to other editors as well. I know that at least the TextMate CoffeeScript Bundle can do some of these.

Basics

Let’s get the basics out of way. Get syntax hilighting from vim-coffee-script plugin and automatic syntax checking from Syntastic. These will take you a long way, but with CoffeeScript we can do more.

Reading compiled code

Especially when starting out with CoffeeScript you are not always sure what the snippet you are reading or even the code you just wrote does. Chances are that you already know Javascript so we can use that to our advantage. vim-coffee-script makes that incredibly easy.

Lets take following snippet that might be confusing to CoffeeScript newbies:

1
{@foo} = bar

With vim-coffee-script you can just select the snippet in Visual Mode and type :CoffeeCompile which will open up a new scratch buffer with a compiled version of the snippet which will clearly tell what this syntax in CoffeeScript means. You can use this to verify that you understood the CoffeeScript syntax by using your Javascript knowledge!

I recommend creating a shortcut for this. It’s so useful. Put this to your .vimrc:

1
2
vmap <leader>c <esc>:'<,'>:CoffeeCompile<CR>
map <leader>c :CoffeeCompile<CR>

This allows you to invoke the compiler with Leader-key + c. The leader key is backslash by default, but usually it is redefined to comma.

Stack Traces

I don’t like manually compiling CoffeeScript files for my Node.js apps. Instead I use the coffee command directly or use plain js wrapper app that starts my CoffeeScript apps. This is clean and simple, but can be painful when you get an exception. There is a stack trace, but it refers to the compiled Javacript file which does not exist! You could look up the original CoffeeScript file and try to guess what line the stack trace means by looking variable names or manually compile the file when exception occurs. Not so fun.

vim-coffee-script to the rescue!

When you execute the CoffeeCompile Vim command in Command Mode you will get the whole file compiled into the scratch buffer. In that you can scroll the line referred by the stack trace and see what code exactly rose it. This is bit clumsy since normally you can jump to a certain line by typing :<number>. We can do better! Put this to .vimrc:

1
command -nargs=1 C CoffeeCompile | :<args>

And then try typing :C<number>. Whoah! This takes you to the given line number in the compiled Javascript of the CoffeeScript file you are editing. Using it is just one character longer than normally jumping lines!

Python-like Decorators in CoffeeScript

| Comments

If you are not familar what decorators are in Python you should skim through this and this. In short they are a nice syntax for wrapping functions/methods with other functions in Python.

I really like decorators in Python and I sometimes miss them when working in other languages. Today at work it hit me when I was working on CoffeeScript project. It is really easy to implement Python-like decorators cleanly in CoffeeScript.

Decorators in Python

Here’s an example usage of Python decorator. Let’s pretend that this is a class for reading values from some device. It will give us values between 0 and 100, but in this app we want put a roof for the values it gives. We can create a decorator that limits the values given by the getter.

Decorator in Python (decorator_example.py) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python

# Our magic device
import random

def roof(amount):
    def decorator(method):
        def wrapper(*args):

            value = method(*args)
            if value > amount:
                return amount
            else:
                return value

        return wrapper
    return decorator


class Device(object):

    @roof(50)
    def get_value(self):
        # Read value from the device
        return random.randint(0, 100)


if __name__ == '__main__':
    reader = Device()
    for i in range(10):
        print reader.get_value()

Decorators in CoffeeScript

So that was an advanced configurable decorator for Python. Lets see how CoffeeScript handles the same situation.

Decorator in CoffeeScript [] (decorator_example.coffee) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env coffee

roof = (amount) -> (method) -> ->

  value = method.apply @, arguments

  if value > amount
    amount
  else
    value

class Device

    getValue: roof(50) ->
      parseInt Math.random() * 100


if require.main is module
  device = new Device
  for i in [0..10]
    console.log device.getValue()

Wow! That is a lot less syntax and no extra nesting!

This really shows how powerful anonymous functions and implicit returns are in CoffeeScript. Also the usage syntax would not be so clean if CoffeeScript didn’t have ability to call functions without the parenthesis.

The usage syntax is though better in Python, because you can stack decorators cleanly with it.

Stacking decorators in Python
1
2
3
4
5
class Device(object):
    @roof(50)
    @floor(10) # Checks the bottom of the value
    def get_value(self):
        return random.randint(0, 100)

In CoffeeScript must put them after each others which can get nasty if you have many decorators.

Piping decorators in CoffeeScript
1
2
3
class Device
    getValue: roof(50) floor(10) ->
      parseInt Math.random() * 100

But wait! There were no specific decorator syntax in Python in the old days. One could apply decorators just by calling it to the target and replacing the original method.

Oldschool decorator usage
1
2
3
4
5
6
class Device(object):
    def get_value(self):
        return random.randint(0, 100)

    get_value = roof(50)(get_value)
    get_value = floor(10)(get_value)

So you can do this in CoffeeScript

Piping decorators in CoffeeScript
1
2
3
4
5
6
class Device
  getValue: roof(50) ->
    parseInt Math.random() * 100

  getValue: roof(50) Device::getValue
  getValue: floor(10) Device::getValue

Pretty ugly, yeah, but might be better if you have tons of decorators.