Home » Posts tagged "tmux"

Tmux scripting

I’ve been using tmux for a while no to manage my terminal sessions. One thing I kept on doing was after starting tmux that I would be manually adding windows, splitting them and issuing commands in each pane such as echoing the contents of log files with tail -f.

I had heard about scripting tmux before but never really looked into it yet, until now. Since I solely use the key bindings I had to figure out how to issue these commands without them. Turns out this is pretty easy and it’s documented in the man page.

Here’s an example of a tmux script I just added to Maximus-Web.

#!/bin/bash
SESSION=$USER

tmux -2 new-session -d -s $SESSION

# Setup a window for tailing log files
tmux new-window -t $SESSION:1 -n 'Logs'
tmux split-window -h
tmux select-pane -t 0
tmux send-keys "tail -f /vagrant/maximus.log" C-m
tmux select-pane -t 1
tmux send-keys "tail -f /vagrant/maximus-worker.log" C-m
tmux split-window -v
tmux resize-pane -D 20
tmux send-keys "tail -f /vagrant/maximus-mojo.log" C-m
# Setup a CoffeeScript compiler/watchdog pane
tmux select-pane -t 0
tmux split-window -v
tmux resize-pane -D 20
tmux send-keys "coffee -o /vagrant/root/static/js/ -cw /vagrant/root/coffee/" C-m

# Setup a MySQL window
tmux new-window -t $SESSION:2 -n 'MySQL' 'mysql -uroot'

# Set default window
tmux select-window -t $SESSION:1

# Attach to session
tmux -2 attach-session -t $SESSION

You can view the (up to date) origin of this script at GitHub.

So what exactly does this script do?

  1. It creates a new tmux session.
  2. It creates a new window called ‘Logs’ which is split into a grid of 2×2 with the bottom 2 panes being smaller in size (height). In every pane a command is executed. For example in pane 0 the command tail -f /vagrant/maximus.log gets executed.
  3. A second window called ‘MySQL’ is created which runs the mysql -uroot command.
  4. Then we switch back to the first window (actually second, as tmux pane numbers start with 0) which is the window that shows us the contents of these log files.
  5. Finally we attach to the tmux session.

The added benefit of this small script is that from now on all I have to do is run it and my tmux session will be configured for this specific project (Maximus in this case).

I’ve also found some other useful tmux resources as well which are listed below:

Using tmux – shortcuts

Even though Windows 7 is my main OS I use terminals a lot. I don’t mean the Windows command prompt in this case, though I actually use that a lot as well. The terminal I’m talking about now is Bash, which I use on my Raspberry Pi, VPS’s and virtual machines. To connect to these systems I use PuTTY.

The biggest benefit tmux gives is not having to open several terminals. Instead you use tabs (windows) or you split the current window. When I’m working on a project which has its own dedicated virtual machine (through Vagrant) I usually log in, create a 2×2 grid, setup 2 panes for tailing log files, another connects to MySQL and the 4th is available for regular terminal stuff.

Another nice feature is being able to detach your current session and return to it later. I do that with my Raspberry Pi for instance. Inside my tmux session I run irssi (an IRC client) and instead of having to reconnect all the time I just detach the session when I’m done, and recover it whenever I need to hop on IRC again.

Key in using tmux effectively is knowing the shortcuts. Memorizing them improves productivity a lot. So to help you get started I listed the main ones below.

  • ctrl+b % or ctrl+b " for pane splitting
  • ctrl+b space for changing pane orientation
  • ctrl+b n for next window
  • ctrl+b arrow keys for pane selection (older versions only support up/down)
  • ctrl+b , for renaming window
  • ctrl+b d for detaching session (close without shutting it down)
  • tmux a to resume a detached sessions

Am I missing any neat shortcuts? Let me know by dropping it in the comments.

Making PuTTY prettier and nicer to work with

PuTTY has been my go-to SSH client on Windows for years but I’ve found that it does require a couple of tweaks to turn it into the (almost?) perfect SSH client. Below are some tweaks to improve PuTTY.

Display characters the way it’s meant

For years I’ve put up with PuTTY displaying certain accented characters or other symbols in a bad and nasty way. This will cause tools like aptitude and tmux to look really awful. It was not long ago that I found that this behavior could be fixed by setting the proper charset. Go to Window > Translation > Remote character set and change it to UTF-8. Lines drawn by tmux have never looked so good. It also removed an annoying display glitch I was having with irssi.

If this doesn’t work make sure Bash is set to use UTF-8 by adding

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8

to your .bashrc.

More about setting up an UTF-8 environment.

Change the default font

By default PuTTY uses Courier New but I’m not really fond of it. Instead I use Consolas bold at 10pt. Using this font with these settings gives fuller text that’s better readable and nicer on the eyes. At least in my opinion.

Use PuTTYtray

I use PuTTYtray so I can set a transparent background. Just set alpha to 220 and the transparency is just perfect. PuTTYtray also has support for hiding your terminal in the system tray, but I personally don’t use that. If you don’t need either then I suppose standard PuTTY will be good enough. I do recommend to install PuTTY along side PuTTYtray though as it ships with some useful tools for key generation.

Change right mouse-button behavior

By default a click on the right mouse-button pastes anything you’ve got on your clipboard to the terminal. If you’re aware of this than it’s fine, but sometimes it can cause some inconvenience. Instead, change its behavior so a right mouse click will show a context menu which aside from pasting from the clipboard also adds some other useful shortcuts. You can find this setting under

Window > Selection > Action of mouse buttons and set it to Windows. The default is Compromise.

Full screen

If you use multiple screens (or not) you may already be resizing your terminal to fit the screen. Instead of full screen with the window border, title and scrollbar there’s also a full screen mode that removes this and uses the entire screen as your terminal space. To do this you need to enable this in

Window > Behaviour > Full screen on Alt-Enter. Doing so will let you use Alt + Enter to go to full screen.

Other settings?

In case I’ve missed any useful tweaks that improve usage of PuTTY I’d like to hear about them in the comments.