(On 2011-07-17, I upgraded tmux to version 1.5, and I updated this post)
If you have heard of or have used GNU/Screen, then you know what tmux is for; if you haven’t, you probably do not want to read the following content, or you have wasted lots of time in doing terminal window cycling.
Contents
1 Quick overview
What’s the major difference between these two? The splitting:
That maybe over-splitting. Anyway, it’s the main reason that I decided to switch to tmux. I know somewhere on the internet has patch for Screen. But I didn’t manually compile program, it would be not easy for me to maintain. If you have a good alternative, why bother to keep to the one lacking the functionality which you need. And Screen is no longer to add new feature, it’s in bugfix-only. tmux is new, highly active.
tmux’s current version is 1.1. I had encountered a problem with version 1.0 when with 256 colors. Some color escape code doesn’t do what it suppose to do.
It only took me an hour to create my own configuration to get similar key bindings and status bar.
The one on top is tmux. They are nearly identical and tmux is actually better.
The memory footprint is much smaller than Screen if you usually use several sessions. On my Gentoo amd64, when you start a tmux server, it takes 2.35 MB without counting Bash’s; Screen takes 2.6 MB. If you fire up a second session, tmux use 1.0 MB more but Screen uses another 2.6 MB.
2 My configuraion ~/.tmux.conf
Note
The following settings were used with tmux 1.1 when this post first published, you can download my latest settings.
# Last modified: 2009-11-12T05:59:41+0800 # Change prefix key to Ctrl+a unbind C-b set -g prefix C-a # Last active window unbind l bind C-a last-window # Copy mode unbind [ bind Escape copy-mode # Use Vi mode setw -g mode-keys vi # Make mouse useful in copy mode setw -g mode-mouse on # More straight forward key bindings for splitting unbind % bind | split-window -h bind h split-window -h unbind '"' bind - split-window -v bind v split-window -v # History set -g history-limit 1000 # Pane unbind o bind C-s down-pane # Terminal emulator window title set -g set-titles on set -g set-titles-string '#S:#I.#P #W' # Status Bar set -g status-bg black set -g status-fg white set -g status-interval 1 set -g status-left '#[fg=green]#H#[default]' set -g status-right '#[fg=yellow]#(cut -d " " -f 1-4 /proc/loadavg)#[default] #[fg=cyan,bold]%Y-%m-%d %H:%M:%S#[default]' # Notifying if other windows has activities setw -g monitor-activity on set -g visual-activity on # Highlighting the active window in status bar setw -g window-status-current-bg red # Clock setw -g clock-mode-colour green setw -g clock-mode-style 24
There is really no much need to explain. The first thing I did was to change the prefix key to C-a, C-b is really hard to press.
The other thing I like is you can specify the terminal window’s title with the format you like.
3 Panes — Splitting, cycling, etc
Basically, you only need to know four keys for controlling:
Key | Default | Action |
---|---|---|
h,| | % | Split window horizontally |
v,- | “ | Split window vertically |
C-s | o | Go to next pane |
x | Kill the active pane | |
q | Show pane numbers | |
A-Arrow Key | Resize the active pane | |
C-Arrow Key | Resize the active pane by one line or on character |
I think h, v are more straight forward in thinking then %, ", and |, - are more clear but it’s harder to press because you have to use Shift key for |.
C-s is better than o, it would be even better if you use C-a as prefix key. You can press in this sequence: C-a (still hold Ctrl) s`s. :kbd:`A key is next to S, it’s much easier to go next pane.
One thing I am not satisfied with panes is it’s not easy to know which pane is active if you do not pay attention and the programs do not have blinking cursor. I have the terminal title to indicate the active pane number, so I am able to tell which pane I am in. Scripting Sometimes you will want to have presetting windows to be prepared automatically. For example, you may want to have foo program in window 0, window 1 has two split panes, and have bar program in pane 1. Because tmux is highly script-able.
I was actually hoping to use alternative configuration using -f argument when fire up tmux, but I didn’t get it well after some time point. I don’t why it didn’t seem to work.
Anyway, if you create a script, there is no problem at all.
Here is an example of how I bring up centerim:
#!/bin/sh tmux new-session -d -s centerim centerim tmux new-window -t centerim:1 CIM_status_setter.py tmux select-window -t centerim:0 tmux -2 attach-session -t centerim
The first command creates a detached (-d) new session named (-s) centerim and also run centerim in the first window of this new session. It then creates a new window and assigns (-t) to session centerim’s window 1, and also run a Python script. The third one selects the window 0 as the active window.
The last one attaches to session centerim, from here, we would be using this session centerim. The -2 forces tmux to use 256 colors.
Note that if you need to run a program with arguments, you will need to use quotes, for example:
tmux new-session -d -s session_name 'program arg1 arg2'
4 Vim
Note
This section doesn’t seem to be needed when using version between 1.1 and 1.4. I am certainly that the visual selection enlarges as the mouse drags in version 1.4.
After I switched to tmux, I found out the mouse support in Vim didn’t work. My original setting for mouse is:
set mouse=a set ttymouse=xterm2
Now I have to use:
set mouse=a set ttymouse=xterm
I still can move the cursor using mouse but the visual selection is different than when ttymouse=xterm2.
xterm2 Works like "xterm", but with the xterm reporting the mouse position while the mouse is dragged. This works much faster and more precise.
5 Mouse Drag event
tmux 1.5 now supports xterm mouse mode 1002, which is required for mouse drag event. Not many programs utilizes that event but you can try with Urwid’s input_text.py. (Note: run $ ./input_text.py r)
Also a few new options enables you to resize panes (mouse-resize-pane) by dragging the dividers or to select window (mouse-select-window) by click on status line.
6 Hardcopy and Logging
Currently I don’t see tmux has both. In Screen, you can use them by press C-a h and C-a H, respectively.
A related operation is to use copy mode, manually select the area, copy to buffer, then use save-buffer command to save to file.
7 Clock
You can show the current time in active pane, default key is t.
The only customization of this clock mode is the color, not really useful feature for me.
8 Other keys
I think the following keys are most useful for me:
Key | Default | Action |
---|---|---|
C-a | l | Last active window |
Escape | [ | Enter copy-mode |
PageUp | PageUp | Same as above |
: | : | Enter command |
? | ? | Show keybindings |
s | s | Choose session to attach |
d | d | Detach from current session |
9 Conclusion
tmux is easy to learn, you only need the manual of it. The man page is written clear and very useful. Only few things that I could not have tmux to do for me as I had in Screen. I have been using tmux for days, I really don’t have a big problem with it. I may uninstall Screen very soon.
Awesome post! Thanks for your ~/.tmux.conf, I'm using a slightly customized version of it now and it's great!
ReplyDelete1. window-status-* should do what you want.
ReplyDelete2. I am not aware of any way that you can set a name to a pane or set
the background color.
Hi,
ReplyDeleteI started using tmux today.
I am still trying to find out many things from this.
So, it would be of great help, if you can suggest.
1. Is there any way to set the color for the background of the tab names?
2. I have created many panes inside my windows.
But, is there any way, I can set a name for these panes and also background color for these names?
Regards,
Narendra
I tried tmux today. But a few doubts.
ReplyDeleteIn screen, I can set the name of a window by executing this:
echo -e "\033k$(echo something)\033\134"
I always use this feature to set window name to the host name I am logged in. Is it possible to to achieve this in tmux?
Run `tmux set-window-option window-status-current-format $(echo
ReplyDeletesomething)` at a window (in shell prompt), it will set the window name
(not terminal window's title) to something. Hope I read your question
right and this is what you want.
On the second thought I think this is not exactly what you want. I think what you want is after login on remote server and show that hostname. Currently I am not aware of that and I believe I have read a discussion on Arch Linux forums, I didn't follow up the discussion. But I think no solution so far.
ReplyDeleteSo the best workaround I can think of is to change it before you remote login. Sorry, couldn't be more helpful.
Thanks for your quick response.
ReplyDeleteIt worked. :-)
Here is my ssh() in .bashrc
function ssh() {
tmux set-window-option window-status-current-format "#I:$(echo $@)"
/usr/bin/ssh "$@"
tmux set-window-option window-status-current-format "#I:$(hostname -s)"
}
And I added this to .tmux.conf
setw -g window-status-format "#I:#(hostname -s)"
setw -g window-status-current-format "#I:#(hostname -s)"
This is very neat and clean.
Thanks.
the above solution doesnt work well when one window is split into panels. :-(
ReplyDeletepanes share same window, same window name, so which ssh last sets the
ReplyDeletewindow name. And pane doesn't have name, so I guess there is no
solution for that case.
re: One thing I am not satisfied with panes is it's not easy to know which pane is active
ReplyDeleteI had the same issue (I don't use a blinking cursor in terminals). Take a look at the following settings. they can make it a lot easier to see which panel is active, especially if you go with a reverse video config of the active.
pane-border-fg
pane-border-bg
pane-active-border-fg
pane-active-border-bg
re: One thing I am not satisfied with panes is it's not easy to know which pane is active
ReplyDeleteI had the same issue (I don't use a blinking cursor in terminals). Take a look at the following settings. they can make it a lot easier to see which panel is active, especially if you go with a reverse video config of the active.
pane-border-fg
pane-border-bg
pane-active-border-fg
pane-active-border-bg
the above solution doesnt work well when one window is split into panels. :-(
ReplyDeleteI know it comes a bit late, but using this http://anti.teamidiot.de/static/nei/*/Code/tmux/ I could get the xterm2 mouse to work
ReplyDeleteI know this post is a little old but you helped me fix my vim mouse problems in tmux.
ReplyDeleteYou can still use:
set mouse=a
set ttymouse=xterm2in vim but in your tmux.conf you needsetw -g xterm-keys onNow everything works great and fast! :)