TMUX – The Terminal Multiplexer (Part 2)

In part 1 I went over the basics of tmux and how to utilize its basic features. In this portion I’m going to dive a bit more into customizing tmux to make it easier and prettier to work with. I’ll also give some examples on how to utilize the client/server model.

Since this part is less of a tutorial and more of a tips / reference I decided a table of contents based layout would be a bit easier to use:

Note: If you have any tips, tricks or pre-made configurations you want to contribute to this post please do so in the comments and I’ll append them to this post.

Note**: You can type the commands into a live tmux session or place them in ~/.tmux.conf file which is automatically loaded when tmux is started.

Modify tmux bindings

Modify tmux looks & style

Misc tips & tricks

Modify tmux bindings

By default tmux has a decent default layout for hotkeys however there are a few keys that either made my fingers contort into ways they shouldn’t have or keys I simply always forgot. Luckily tmux offers a very simple syntax to rebind any key.

Rebinding the Action Key

Here is how you can rebind the Ctrl-b prefix to be something a bit easier to type. I personally use Ctrl-a similar to screen:

set-option -g prefix C-a

Note: The “-g” switch stands for global so the binding will affect every window. Alternatively the set-option command accepts arguments to specify the session and target of that particular binding if you like.

Now every subsequent command will be prefixed with Ctrl-a instead of Ctrl-b.

Binding a key for “last-window”

One of my favorite screen hotkeys is the last window hotkey which allows you to quickly switch between the current window and last window that was active. By default tmux doesn’t have a binding for this however it can easily be achieved by using the following:

bind-key C-a last-window

What this does is binds Ctrl-a to switch between the last active window. To use this binding you would hit Ctrl-a twice (since the prefix is set to Ctrl-a and the binding Ctrl-a).

Rebinding the pane splitting bindings

One of the most powerful features of tmux is the ability to natively split your windows into several panes. Unfortunately the default bindings to achieve this are simply unintuitive. As a result I’ve rebound the splitting commands to something I could remember:

unbind % # Remove default binding since we’re replacing
bind | split-window -h
bind – split-window -v

At first this may look odd but for me it’s easier to remember that “|” splits the screen vertically while “-” splits the screen horizontally.  To achieve the below window structure you would type the following sequence:

Ctrl-a |
Ctrl-a -
Ctrl-a -

The first Ctrl-a |command splits the current window in half. The active window is the pane on the right so the subsequent Ctrl-a - splits that window into two vertically. Hitting Ctrl-a - once more now splits that active window into two more by splitting it vertically.

Note: Depending on the active layout you’re using the behavior of how the windows split may be different.

Modify tmux look & style

The default colors and style of tmux isn’t awful however it’s somewhat bland for my liking. I personally only have modified tmux slightly however it allows you to do quite a bit. In this section I’ll go over some of tmux’s style features.

Modifying tab color & looks

By default the tab colors are pretty bland and it makes it difficult to distinguish the active window from the other windows. Here is the default tmux tabs vs. the modified ones (snippet below):

As you can see the latter version is much more clear and uses some variable expansions tmux provides (hostname, etc). You can achieve this output with the following:

# Set status bar
set -g status-bg black
set -g status-fg white
set -g status-left ‘#[fg=green]#H’

The first two commands set the background to black and the text to white. The third command is where the magic happens: the status-left command tells tmux to display the following text to the left of the terminal. The [fg=green]#H portion tells tmux to display the hostname of localhost and make it green. The #H portion is part of tmux variable expansion – please refer to the man pages for more information on other ones you can use.

That alone doesn’t account for highlighting the active window. If you want to do that use the following snippet:

# Highlight active window
set-window-option -g window-status-current-bg red

The set-window-option -g window-status-current-bg red command tells tmux to change the background of the current active window to red. The set-window-option command has several other options you can pass to it to achieve similar affects.

Adding information to your session

Sometimes it might be useful to add some information from your local machine to the tmux screen. Earlier we played with the status-left command which sets the left portion of the status bar. Let’s use the status-right command to add some information to the right side:

As you can see I’ve added the amount of users logged in and the current load average for my computer. This was achieved with the following snippet:

set -g status-right ‘#[fg=yellow]#(uptime | cut -d “,” -f 2-)’

Similar to before the #[fg=yellow] portion tells tmux to make the font yellow. The #(uptime | cut -d “,” -f 2-) portion tells tmux to run that cmd and output it on the right of the status bar.

Note: If you’re not familiar with shell scripting this command is very simple. It runs the uptime command and then passes it to the cut command which splits it at the commas (,). The -f 2- portion says to print out everything from the second comma onward.

Note**: By default the status bar is redrawn every 15 seconds however you can modify this by setting the status-interval command.

Misc tips & tricks

Using shell scripting to setup your tmux enviroment

Tmux allows you to easily run commands for your different sessions through the command line without having to ever login to the session. This also allows us to make a simple bootstrap script which will setup your tmux environment and log you into it. Here is an example script I personally use:

#!/bin/sh
tmux new-session -d -s hawkhost

tmux new-window -t hawkhost:1 -n 'Server1' 'ssh root@10.x.x.x'
tmux new-window -t hawkhost:2 -n 'Server2' 'ssh root@10.x.x.x'
tmux new-window -t hawkhost:3 -n 'Server3' 'ssh root@10.x.x.x'
tmux new-window -t hawkhost:4 -n 'Server4' 'ssh root@10.x.x.x'
tmux new-window -t hawkhost:5 -n 'Server5' 'ssh root@10.x.x.x'

tmux select-window -t hawkhost:1
tmux -2 attach-session -t hawkhost

The command new-session -d -s hawkhost creates a new tmux session, detaches it (so it doesn’t open in your current terminal) and names it hawkhost in this case.

The following set of new-window commands create five new windows and executes the command at the end. The arguments are broken down as follows: -t hawkhost :1 tells tmux to “target” the session hawkhost and the window with the id of 1. The -n ‘Server1′ ‘ssh root@10.x.x.x’ portion tells tmux to name the window Server1 and execute the ssh root@10.x.x.x command in it.

The last two commands are pretty straight forward. The select-window -t hawkhost:1 command tells tmux that you want the active window the session to be hawkhost and window 1. The -2 attach-session -t hawkhost tells tmux you want to attach the terminal with 256 colors and attach to the session hawkhost.

Note: This is the command that brings the tmux session to the foreground. If you recall earlier when the script created the session we specified the -d switch which prevents it from loading in your terminal.

Notify you when a window has activity

This quick snippet will have tmux notify you in the status area when a window has activity:

# Set window notifications
setw -g monitor-activity on
set -g visual-activity on

As you can see in the first image it lets me know that there was activity in window 0 (in this case sleep 10 && echo “narwhal” and switching the window).

Automatic window rename

You can have tmux rename the window to the command that is currently running. This is useful when you load up something such as irssi and the window is labeled accordingly:

# Automatically set window title
setw -g automatic-rename

This entry was posted in General, Random, Tips and tagged , , . Bookmark the permalink.

42 Responses to TMUX – The Terminal Multiplexer (Part 2)

  1. Tim Harman says:

    You can’t use the examples you’ve provided, because they’re using SmartQuotes.

    So attempting to cut and paste what you’ve posted breaks and doesn’t work. You might want to fix it so users don’t get angry :)

  2. Cody says:

    Good catch – I despise automagic formatting for reasons such as this. I’ll see if I can modify it a bit as not to butcher my quotes :).

  3. awhan says:

    good post … nicely explained … thanks

  4. Would you mind doing a write up on the vi bindings? I didn’t see that covered in part1 or 2

  5. Cody says:

    I’ll see what I can do :).

  6. Justin Blake says:

    Not just the quotes, but the dash got changed to a “smart” hyphen in part 1. Assuming it’s probably the same in these examples as well.

    Anyway, great series. I had tried tmux a while ago but ended up falling back to screen. These posts have made me decide to give it another shot. I don’t think I fully grasped the concept of panes vs. windows before. It’s quite different from screen.

  7. Solomon White says:

    Here are some “vim-ish” bindings I use:


    # use "v" and "s" to do vertical/horizontal splits, like vim
    bind s split-window -v
    bind v split-window -h

    # use the vim motion keys to move between panes
    bind h select-pane -L
    bind j select-pane -D
    bind k select-pane -U
    bind l select-pane -R

    # use vim motion keys while in copy mode
    setw -g mode-keys vi

    # use the vim resize keys.
    # the number at the end is how much the pane will be resized,
    # and 1 is fairly small -- you might want to tweak this.
    bind resize-pane -R 1
    bind - resize-pane -D 1
    bind + resize-pane -U 1

  8. ugh, formatting munged that last block. should be:


    bind < resize-pane -L 1
    bind > resize-pane -R 1

  9. Joe says:

    Does anyone know how to get tmux to display a message on startup, or set a particular panel configuration. I want to be able to see a list of useful keybindings (preferably in a seperate panel) when I start.

  10. jsmith says:

    Hey Joe,

    would adding another new-window line with a “cat current_keybindings.txt” command do the job?

  11. NYCBest says:

    I was wondering if it’s possible to have tmux open a new window, split the window and ssh to a different server in each pane.

    I got
    tmux neww “exec ssh $server”
    tmux splitw -v

    but i can’t get it to do a second ssh in the second pane.

    Thanks in advance.

  12. NYCBest says:

    So tmux split-window -v “exec /ms/dist/aurora/bin/ssh -q $server2 worked.

  13. Howard says:

    Awesome, thanks for the info! I’ve been using screen for years now, and wanted to get into tmux (because it is installed by default on OpenBSD, my second favorite *nix flavor), but really haven’t had the time or inclination. Reading through your little tutorials not only got me started, they armed me up with lots of good info!

    Btw, I posted links to these blog posts to my LUG Yahoo group, hope that’s ok.

    Howard

  14. maba says:

    Hi,
    is there someone that has a problem with automatic-rename on tmux installed via homebrew on osx?
    Setting stays ON even if it’s set to OFF in the tmux config file or even it’s set manual for a window.

    Thanks :)

  15. Is there a way to save and restore session like screen? I’m with this doubt.

    Thanks

  16. I got the answer …. C-b d to deattach and “tmux attach” to restore :-)

    Great :)

  17. Jim Power says:

    Thanks for the tmux rundown! One thing, tmux complains it doesn’t know what key ‘-’ is when I try to add that binding, haven’t found a fix yet!

  18. mark says:

    I have problem to switch from one window to other. I splitted window and now my active panel is on right. How to switch between both?

    thanks

  19. Baylink says:

    Suggestion: You might want to include, somewhere, a copy-pastable version of *your entire setup file*… so those who like most of your ideas can grab it, and just cut out the line or few they don’t like.

    Instead of having to glue it all together.

    Nice tutorial.

  20. Lewis says:

    Is there a way to save the settings you’ve typed in in a live tmux session to the conf file?

  21. Alex Hart says:

    Hey,

    I seem to be having a problem where Tmux is blatantly ignoring my config file, even though it exists. Thoughts?

    ~Alex

  22. fengshaun says:

    @Alex Hart:

    make sure you kill *all* tmux instances before you open a new tmux session to see your changes. Alternatively, just do

    C-b : source-file ~/.tmux.conf
    

    to reload your config file.

  23. fengshaun says:

    @Baylink: with a few modifications for my own setup.

    ## status bar
    # all
    set -g status-fg white
    set -g status-bg black
    
    # left
    set -g status-left-fg white
    set -g status-left-bg blue
    set -g status-left '#H:#S'
    
    # right
    set -g status-right-fg white
    set -g status-right-bg blue
    set -g status-right '%a %b %d, %Y %H:%M'
    
    ## window options
    # color
    set-window-option -g window-status-current-fg white
    set-window-option -g window-status-current-bg blue
    
    # notifications
    setw -g monitor-activity on
    setw -g visual-activity on
    
    # auto rename
    set -g automatic-rename
    

    Thank you Cody for a such an awesome post. This post single-handedly got me into tmux and I’m *loving* it!

    P.S. sorry for double-post. I couldn’t edit my previous post.

  24. Hey, it should be:
    setw -g automatic-rename on

  25. Nimphtus says:

    Anyone know if it’s possible to get net upload/download transfer speeds in the status line? If I could get that displayed along with Load Averages and acpi temperature, calender and uptime, I could abandon dzen2 altogether and save a few pixels of vertical space on my netbook.

    Help~!!

  26. Great write up Hawk – it’s blog posts like these that enthuse me about new stuff. I’ve switched from screen to tmux and loving it.

  27. kshitij says:

    I loved tmux and your simple useful customizations. I use guake with only 2 -3 tabs open (1 for emacs, 1 for remote host and third for my local machine) and wanted split panes in 3rd one. tmux solved the problem!

    Thnx a lot.

    –kshitj

  28. slny says:

    “set -g status-bg black” doesn’t seem to work when I put it in the tmux.conf
    “set-option -g status-bg black” doesn’t work either.

    I tried your split window bindings which both work:
    bind | split-window -h
    bind _ split-window -v

    Any idea what is wrong? I’m using tmux from macports on OS X 10.7.1

  29. slny says:

    Never mind, needed to reload the config, hadn’t learned about that yet :)

  30. red says:

    Would you know how to “swap” panes?

    For example, let’s say I do ‘Ctrl-a |’ so I have two panes. I want to replace one of the panes with window 2.

  31. Gregor says:

    Your post is what got me to replace tmux with screen, thank you. I have a question, whenever I listen to music with Music on Console, the tab will display the song’s title, it is long, and it takes up the entire width of my screen. I want to limit X amount of characters displayed on the tabs, what should I put in my configuration file?

  32. What triggered my interest to tmux was its ability to send input to all panes simultaneously. Considering switchover from screen to tmux.

  33. richo says:

    You may want to take a look into tmuxinator[1], if you want to start a complex session from the shell.

    [1][https://github.com/aziz/tmuxinator]

  34. anon says:

    To really make ^a work like it does it screen so that ^aa sends a literal ^a you really need the following:

    set-option -g prefix C-a
    bind-key C-a last-window
    bind-key a send-key C-a

  35. taiko says:

    Hi ,
    just started with tmux ( on freebsd 9 , i386) ,
    i like that font on your statusbar quite a bit , would you mind to tell the name ?
    did you apply that in Xdefaults ?
    thanks ,
    T

  36. Pingback: Script to enter commands in a Tmux window

  37. goldlabel says:

    Hello,

    is there a tut where i can read how to install tmux coz i get to the screen with the green bar and the [0] 0:bash* but if i press ctrl and b nothing happends thats for all ctrl functions. what m i doing wrong im using ubuntu server and im very new at this
    thx

    grtz gold

  38. William Furr says:

    Thanks! Very useful stuff. This post takes tmux from sane alternative to screen to a *great* and intuitive alternative to screen.

  39. Pingback: How to pass commands to 'screen'

  40. Pingback: Emile "iMil" Heitor 's home » Blog Archive A new multiplexed world » Emile "iMil" Heitor 's home

  41. Gary says:

    I have not been able to find documentation on the -n option described. I looked here:
    http://www.openbsd.org/cgi-bin/man.cgi?query=tmux&section=1
    Where did you find the docs for this option?

  42. Vinicius says:

    Hello, everybody! How are you?
    I’d like to know if is it possible to start TMUX splitted (vert and horiz)? If yes, how do I do this?
    Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>