May 16, 2013

Fancy tmux keybindings with iTerm

I wanted to bind ⌘-{ and ⌘-} to switch windows in tmux (window is to tmux as tab is to everything else). Turns out tmux won’t let you bind to ⌘. I was able to solve this with a little hack in iTerm.

In iTerm preferences, click Profiles in the toolbar, then the Keys tab. You’ll see a lieelte + button there:

Click in the “Keyboard Shortcut” field, and mash the shortcut you want to use (in my case command-shift-[).

Here is the real trick, choose “Send Hex Code” as the action.

Then in the field you want to enter the ascii code for what you want to send to tmux. In my case I used:

Key Binding Hex Code Keys Sent tmux command
⌘-{ 0x02 0x70 control-b,p previous window
⌘-} 0x02 0x6E control-b,n next window

The other thing that makes this easier to keep tmux and iterm working nicely is to use the prefix2 setting in tmux. That lets you keep control-b working as a prefix binding, but you can also use a more comfortable binding when you are typing.

# Set C-f to prefix, but leave C-b in place
set -g prefix2 C-f
bind C-f send-prefix
bind C-f last-window

I think this is a nice little trick that would work for more things than just tmux. Let me know if you find other clever uses for it.