https://wiki.archlinux.org/api.php?action=feedcontributions&user=Jorges&feedformat=atomArchWiki - User contributions [en]2024-03-29T11:15:06ZUser contributionsMediaWiki 1.41.0https://wiki.archlinux.org/index.php?title=Tmux&diff=211500Tmux2012-06-29T01:21:16Z<p>Jorges: /* Clients simultaneously interacting with various windows of a session */</p>
<hr />
<div>[[es:Tmux]]<br />
[[ru:Tmux]]<br />
[[tr:Tmux]]<br />
[[Category:Terminal emulators]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|This article explains how to install and configure tmux.}}<br />
{{Article summary heading|Related}}<br />
{{Article summary wiki|GNU Screen}}<br />
{{Article summary end}}<br />
<br />
[http://tmux.sourceforge.net/ Tmux] is a "terminal multiplexer: it enables a number of terminals (or windows), each running a separate program, to be created, accessed, and controlled from a single screen. tmux may be detached from a screen and continue running in the background, then later reattached." <br />
<br />
Tmux is notable as a BSD-licensed alternative to [[Screen Tips|GNU Screen]]. Although similar, there are many differences between the programs, as noted on the [http://tmux.svn.sourceforge.net/viewvc/tmux/trunk/FAQ tmux FAQ page]. Most notably, tmux is currently under active development, in contrast to screen, which has not had a stable release since August 8, 2008.<br />
<br />
==Installation==<br />
[[pacman|Install]] {{Pkg|tmux}}, available in the [[Official Repositories]].<br />
<br />
==Configuration==<br />
A user-specific configuration file should be located at {{ic|~/.tmux.conf}}, while a global configuration file should be located at {{ic|/etc/tmux.conf}}. Default configuration files can be found in {{Ic|/usr/share/tmux/}}. <br />
<br />
===Key bindings===<br />
{| style="float:right;border:1px #cccccc solid;margin:5px;padding:5px;width:200px;"<br />
|+ ''Prefix all commands with'' {{Ic|Ctrl-b}}<br />
!Cmd<br />
!Action<br />
|-<br />
|c<br />
|Create a new window<br />
|-<br />
|n<br />
|Change to next window<br />
|-<br />
|p<br />
|Change to previous window<br />
|-<br />
|"<br />
|Split pane horizontally<br />
|-<br />
|%<br />
|Split pane vertically<br />
|-<br />
|,<br />
|Rename current window<br />
|-<br />
|o<br />
|Move to next pane<br />
|}<br />
<br />
By default, command key bindings are prefixed by Ctrl-b. For example, to vertically split a window type {{Ic|Ctrl-b %}}.<br />
<br />
After splitting a window into multiple panes, you can resize a pane by the hitting prefix key (i.e. {{Ic|Ctrl-b}}) and, while continuing to hold Ctrl, press Left/Right/Up/Down. Swapping panes is achieved in the same manner, but by hitting ''o'' instead of a directional key.<br />
<br />
{{Tip|To mimic screen key bindings copy {{ic|/usr/share/tmux/screen-keys.conf}} to either of the configuration locations.}}<br />
<br />
Key bindings may be changed with the bind and unbind commands in {{ic|tmux.conf}}. For example, you can change the prefix key (i.e. {{Ic|Ctrl-b}}) to {{Ic|Ctrl-a}} by adding the following commands in your configuration file:<br />
{{bc|<br />
unbind C-b<br />
set -g prefix C-a<br />
}}<br />
<br />
Additional ways to move between windows include:<br />
Ctrl-b l (Move to the previously selected window)<br />
Ctrl-b w (List all windows / window numbers)<br />
Ctrl-b <window number> (Move to the specified window number, the default bindings are from 0 – 9)<br />
Ctrl-b q (Show pane numbers, when the numbers show up type the key to goto that pane)<br />
<br />
What if you have 10+ windows open? Tmux has a find-window option & keybinding. <br />
Ctrl-b f <window name> (Search for window name)<br />
Ctrl-b w (Select from interactive list of windows)<br />
<br />
===Browsing URL's===<br />
To browse URL's inside tmux you must have {{aur|urlview}} installed and configured.<br />
<br />
Inside a new terminal:<br />
bind-key u capture-pane \; save-buffer /tmp/tmux-buffer \; run-shell "$TERMINAL -e urlview /tmp/tmux-buffer"<br />
<br />
Or inside a new tmux window (no new terminal needed):<br />
bind-key u capture-pane \; save-buffer /tmp/tmux-buffer \; new-window -n "urlview" '$SHELL -c "urlview < /tmp/tmux-buffer"'<br />
<br />
=== Setting the correct term===<br />
If you are using a 256 colour terminal, you will need to set the correct term in tmux. You can do this in either the {{ic|tmux.conf}}:<br />
<br />
set -g default-terminal "screen-256color" <br />
<br />
or in your {{ic|.bashrc}} with a test like:<br />
<br />
# for tmux: export 256color<br />
[ -n "$TMUX" ] && export TERM=screen-256color<br />
<br />
If you enable xterm-keys in your {{ic|tmux.conf}}, then you need to build a custom terminfo to declare the new escape codes or applications will not know about them. Compile the following with {{ic|tic}} and you can use "xterm-screen-256color" as your TERM:<br />
<br />
# A screen- based TERMINFO that declares the escape sequences<br />
# enabled by the tmux config "set-window-option -g xterm-keys".<br />
#<br />
# Prefix the name with xterm- since some applications inspect<br />
# the TERM *name* in addition to the terminal capabilities advertised.<br />
xterm-screen-256color|GNU Screen with 256 colors bce and tmux xterm-keys,<br />
<br />
# As of Nov'11, the below keys are picked up by<br />
# .../tmux/blob/master/trunk/xterm-keys.c:<br />
kDC=\E[3;2~, kEND=\E[1;2F, kHOM=\E[1;2H,<br />
kIC=\E[2;2~, kLFT=\E[1;2D, kNXT=\E[6;2~, kPRV=\E[5;2~,<br />
kRIT=\E[1;2C,<br />
<br />
# Change this to screen-256color if the terminal you run tmux in<br />
# doesn't support bce:<br />
use=screen-256color-bce,<br />
<br />
=== Other Settings ===<br />
Set scrollback to 10000 lines with <br />
set -g history-limit 10000<br />
<br />
==Session initialization==<br />
You can have tmux open a session with preloaded windows by including those details in your {{ic|~/.tmux.conf}}:<br />
<br />
new -n WindowName Command<br />
neww -n WindowName Command<br />
neww -n WindowName Command<br />
<br />
To start a session with split windows (multiple panes), include the splitw command below the neww you would like to split; thus:<br />
<br />
new -s SessionName -n WindowName Command<br />
neww -n foo/bar foo<br />
splitw -v -p 50 -t 0 bar<br />
selectw -t 1 <br />
selectp -t 0<br />
<br />
would open 2 windows, the second of which would be named foo/bar and would be split vertically in half (50%) with foo running above bar. Focus would be in window 2 (foo/bar), top pane (foo).<br />
<br />
{{Note|Numbering for sessions, windows and panes starts at zero, unless you have specified a base-index of 1 in your {{ic|.conf}} }}<br />
<br />
To manage multiple sessions, source separate session files from your conf file:<br />
<br />
# initialize sessions<br />
bind F source-file ~/.tmux/foo<br />
bind B source-file ~/.tmux/bar<br />
<br />
==Troubleshooting==<br />
===Scrolling issues===<br />
If you have issues scrolling with Shift-PageUp/Shift-PageDown in your terminal, try this:<br />
<br />
set -g terminal-overrides 'xterm*:smcup@:rmcup@'<br />
<br />
=== shift-f6 not working in midnight commander ===<br />
<br />
If this key combination is not working with either TERM=screen or screen-256color, then from inside tmux, do<br />
infocmp > screen (or screen-256color)<br />
<br />
open the file in a text editor, and add the following to the bottom of that file:<br />
kf16=\E[29~,<br />
<br />
Then compile the file with {{ic|tic}}. The keys should be working now.<br />
<br />
==ICCCM Selection Integration==<br />
It is possible to copy a tmux paste buffer to an ICCCM selection, and vice-versa, by defining a shell command which interfaces tmux with an X11 selection interface. The following tmux config file snippet effectively integrates {{Ic|CLIPBOARD}} with the current tmux paste buffer using xclip:<br />
<br />
{{hc|~/.tmux.conf|<br />
...<br />
##CLIPBOARD selection integration<br />
##Requires prefix key before the command key<br />
#Copy tmux paste buffer to CLIPBOARD<br />
bind C-c run "tmux show-buffer <nowiki>|</nowiki> xclip -i -selection clipboard"<br />
#Copy CLIPBOARD to tmux paste buffer and paste tmux paste buffer<br />
bind C-v run "tmux set-buffer -- \"$(xclip -o -selection clipboard)\"; tmux paste-buffer"<br />
}}<br />
<br />
If you get an output similar to {{ic| \346\227\245\346\234\254\350\252\236\343\201\247 }} when pasting utf-8 characters, try changing this line:<br />
{{bc|bind C-c run "tmux show-buffer <nowiki>|</nowiki> xclip -i -selection clipboard"}}<br />
to this:<br />
{{bc|bind C-p run "tmux save-buffer - <nowiki>|</nowiki> xclip -i -selection clipboard"}}<br />
<br />
==Tips and tricks==<br />
<br />
===Start tmux in urxvt===<br />
Use this command to start urxvt with a started tmux session. I use this with the exec command from my .ratpoisonrc file.<br />
{{bc|<nowiki>urxvt -e bash -c "tmux -q has-session && exec tmux attach-session -d || exec tmux new-session -n$USER -s$USER@$HOSTNAME"</nowiki>}}<br />
<br />
===Start tmux on every shell login===<br />
<br />
Simply add the following line of bash code to your .bashrc before your aliases; the code for other shells is very similar:<br />
{{bc|<nowiki>[[ $TERM != "screen" ]] && exec tmux</nowiki>}}<br />
<br />
{{hc|~/.bashrc|<nowiki><br />
# If not running interactively, do not do anything<br />
[[ $- != *i* ]] && return<br />
[[ $TERM != screen* ]] && exec tmux</nowiki>}}<br />
<br />
{{note|At first you may read screen as if we were using screen and not tmux, but tmux also uses screen for the TERM enviroment variable.}}<br />
<br />
This snippet does the same thing, but also checks tmux is installed before trying to launch it. It also tries to reattach you to an existing tmux session at logout, so that you can shut down every tmux session quickly from the same terminal at logout.<br />
{{bc|<nowiki># TMUX<br />
if which tmux 2>&1 >/dev/null; then<br />
# if no session is started, start a new session<br />
test -z ${TMUX} && tmux<br />
<br />
# when quitting tmux, try to attach<br />
while test -z ${TMUX}; do<br />
tmux attach || break<br />
done<br />
fi</nowiki>}}<br />
<br />
{{note|Instead of using the bashrc file, you can launch tmux when you start your terminal emulator. (i. e. urxvt -e tmux)}}<br />
<br />
===Split window and retain current directory===<br />
<br />
{{Note|1=With [http://tmux.svn.sourceforge.net/viewvc/tmux?view=revision&revision=2647 revision 2647] this behavior became standard.}}<br />
<br />
====Fast method====<br />
<br />
{{Note|If you have set default-path to something for another convince; it will be reset to ~/}}<br />
<br />
This command simply sets the default-path to your current path, splits the window, and resets it to your home directory. Can be easily bound to a key if you wish.<br />
<br />
tmux set default-path $(pwd) \; split-window\; set default-path ~/<br />
<br />
To bind it to | and - respectively:<br />
<br />
bind - set default-path $PWD \; split-window\; set default-path ~/<br />
bind | set default-path $PWD \; split-window -v\; set default-path ~/<br />
<br />
====cd method====<br />
<br />
{{Note|This trick tries to inject a key into your session, which only works if it is at a command prompt (i. e. fails within a program like vim, emacs, …).}}<br />
<br />
Create a excutable file as follows, for example {{ic|~/.scripts/tmux-split}}:<br />
<br />
#!/usr/bin/env bash<br />
PWD=`pwd`<br />
tmux split-window $1<br />
tmux send-keys " cd $PWD;clear"<br />
tmux send-keys "Enter"<br />
<br />
and change the configure from:<br />
<br />
bind v split-window -h<br />
bind n split-window -v<br />
<br />
to<br />
<br />
bind v send-keys " ~/mbin/split-tmux -h" \; send-keys "Enter"<br />
bind n send-keys " ~/mbin/split-tmux -v" \; send-keys "Enter"<br />
<br />
====/proc method====<br />
<br />
{{Note|This script, [http://chneukirchen.org/dotfiles/bin/tmux-neww-in-cwd borrowed from] Christian Neukirchen, reads the present pane's CWD from {{Ic|/proc/[pane's top program's $PID]/cwd}}. Some shells like zsh will not always properly update that entry.}}<br />
<br />
Pasted into e. g. {{ic|~/.scripts/tmux-split-in-cwd}}:<br />
<br />
#!/bin/sh<br />
# tmux-split-in-cwd - open a new shell with same cwd as calling pane<br />
<br />
SIP=$(tmux display-message -p "#S:#I:#P")<br />
PTY=$(tmux server-info |<br />
egrep flags=\|bytes |<br />
awk '/windows/ { s = $2 }<br />
/references/ { i = $1 }<br />
/bytes/ { print s i $1 $2 } ' |<br />
grep "$SIP" |<br />
cut -d: -f4)<br />
PTS=${PTY#/dev/}<br />
PID=$(ps -eao pid,tty,command --forest | awk '$2 == "'$PTS'" {print $1; exit}')<br />
DIR=$(readlink /proc/$PID/cwd)<br />
<br />
case "$1" in<br />
h) tmux splitw -h "cd '$DIR'; $SHELL"<br />
;;<br />
v) tmux splitw -v "cd '$DIR'; $SHELL"<br />
;;<br />
*) tmux neww "cd '$DIR'; $SHELL"<br />
;;<br />
esac<br />
<br />
{{ic|~/.tmux.conf}} could thus contain:<br />
<br />
bind | run '~/.scripts/tmux-split-in-cwd h' # horizontal split in cwd<br />
bind _ run '~/.scripts/tmux-split-in-cwd v' # vertical split in cwd<br />
bind m run '~/.scripts/tmux-split-in-cwd' # new window in cwd<br />
<br />
===Use tmux windows like tabs===<br />
<br />
The following settings added to {{ic|~/.tmux.conf}} allow to use tmux windows like tabs, such as those provided by the reference of these hotkeys — [[rxvt-unicode#urxvtq_with_tabbing|urxvt's tabbing extensions]]. An advantage thereof is that these virtual “tabs” are independent of the terminal emulator.<br />
<br />
#urxvt tab like window switching (-n: no prior escape seq)<br />
bind -n S-down new-window<br />
bind -n S-left prev<br />
bind -n S-right next<br />
bind -n C-left swap-window -t -1<br />
bind -n C-right swap-window -t +1<br />
<br />
Of course, those should not overlap with other applications' hotkeys, such as the terminal's. Given that they substitute terminal tabbing that might as well be deactivated, though.<br />
<br />
It can also come handy to supplement the EOT hotkey {{Keypress|Ctrl}}+{{Keypress|d}} with one for tmux's detach:<br />
<br />
bind-key -n C-j detach<br />
<br />
===Clients simultaneously interacting with various windows of a session===<br />
<br />
In “[http://mutelight.org/articles/practical-tmux Practical Tmux]”, Brandur Leach writes:<br />
<br />
{{Box||Screen and tmux's behaviour for when multiple clients are attached to one session differs slightly. In Screen, each client can be connected to the session but view different windows within it, but in tmux, all clients connected to one session must view the same window.<br />
<br />
This problem can be solved in tmux by spawning two separate sessions and synchronizing the second one to the windows of the first, then pointing a second new session to the first.<br />
<br />
However, this usage of tmux results in the problem that detaching from these mirrored sessions will start to litter your system with defunct sessions which can only be cleaned up with some pretty extreme micromanagement.}}<br />
<br />
{{Note|Since tmux 1.4 mirrored sessions may be set to auto-destroy with the {{Ic|destroy-unattached}} session option}}<br />
<br />
To avoid these issues he wrote the script “{{Ic|tmx}}” — the version below is slightly modified to execute “{{Ic|tmux new-window}}” if “1” is its second parameter. Invoked as {{Ic|tmx <base session name> [1]}} it launches the base session if necessary. Otherwise it will kill any “zombie” sessions, launch a new “client” session linked to the base, optionally add a new window and attach. Then it waits for detachment and kills its session.<br />
<br />
{{hc|tmx|2=<nowiki><br />
#!/bin/bash<br />
<br />
#<br />
# Modified TMUX start script from:<br />
# http://forums.gentoo.org/viewtopic-t-836006-start-0.html<br />
#<br />
# Store it to `~/bin/tmx` and issue `chmod +x`.<br />
#<br />
<br />
# Works because bash automatically trims by assigning to variables and by <br />
# passing arguments<br />
trim() { echo $1; }<br />
<br />
if [[ -z "$1" ]]; then<br />
echo "Specify session name as the first argument"<br />
exit<br />
fi<br />
<br />
# Only because I often issue `ls` to this script by accident<br />
if [[ "$1" == "ls" ]]; then<br />
tmux ls<br />
exit<br />
fi<br />
<br />
base_session="$1"<br />
# This actually works without the trim() on all systems except OSX<br />
tmux_nb=$(trim `tmux ls | grep "^$base_session" | wc -l`)<br />
if [[ "$tmux_nb" == "0" ]]; then<br />
echo "Launching tmux base session $base_session ..."<br />
tmux new-session -s $base_session<br />
else<br />
# Make sure we are not already in a tmux session<br />
if [[ -z "$TMUX" ]]; then<br />
# Kill defunct sessions first<br />
old_sessions=$(tmux ls 2>/dev/null | egrep "^[0-9]{14}.*[0-9]+\)$" | cut -f 1 -d:)<br />
for old_session_id in $old_sessions; do<br />
tmux kill-session -t $old_session_id<br />
done<br />
<br />
echo "Launching copy of base session $base_session ..."<br />
# Session is is date and time to prevent conflict<br />
session_id=`date +%Y%m%d%H%M%S`<br />
# Create a new session (without attaching it) and link to base session <br />
# to share windows<br />
tmux new-session -d -t $base_session -s $session_id<br />
if [[ "$2" == "1" ]]; then<br />
# Create a new window in that session<br />
tmux new-window<br />
fi<br />
# Attach to the new session<br />
tmux attach-session -t $session_id<br />
# When we detach from it, kill the session<br />
tmux kill-session -t $session_id<br />
fi<br />
fi<br />
</nowiki>}}<br />
<br />
A useful setting for this is<br />
<br />
setw -g aggressive-resize on<br />
<br />
added to {{ic|~/.tmux.conf}}. It causes tmux to resize a window based on the smallest client actually viewing it, not on the smallest one attached to the entire session.<br />
<br />
An alternative taken from [http://sourceforge.net/mailarchive/forum.php?thread_name=CAPBqLKEC0MAFR%2BWUYqCuyd%3DKB47HK8CFSuAf%3Dd%3DW2H3F4fpMZw%40mail.gmail.com&forum_name=tmux-users] is to put the following ~/.bashrc:<br />
<br />
{{hc|.bashrc|2=<nowiki><br />
function rsc() {<br />
CLIENTID=$1.`date +%S`<br />
tmux new-session -d -t $1 -s $CLIENTID \; set-option destroy-unattached \; attach-session -t $CLIENTID<br />
}<br />
<br />
function mksc() {<br />
tmux new-session -d -s $1<br />
rsc $1<br />
}<br />
</nowiki>}}<br />
<br />
Citing the author:<br />
{{Box||"mksc foo" creates a always detached permanent client named "foo". It also<br />
calls "rsc foo" to create a client to newly created session. "rsc foo"<br />
creates a new client grouped by "foo" name. It has destroy-unattached<br />
turned on so when I leave it, it kills client.<br />
<br />
Therefore, when my computer looses network connectivity, all<br />
"foo.something" clients are killed while "foo" remains. I can then call<br />
"rsc foo" to continue work from where I stopped.}}<br />
<br />
===Changing the configuration with tmux started===<br />
<br />
By default tmux reads {{ic|~/.tmux.conf}} only if it was not already running. To have tmux load a configuration file afterwards, execute:<br />
<br />
tmux source-file <path><br />
<br />
This can be added to {{ic|~/.tmux.conf}} as e. g.:<br />
<br />
bind r source-file <path><br />
<br />
You can also do ^: and type :<br />
source .tmux.conf<br />
<br />
===Template script to run program in new session resp. attach to existing one===<br />
<br />
This script checks for a program presumed to have been started by a previous run of itself. Unless found it creates a new tmux session and attaches to a window named after and running the program. If however the program was found it merely attaches to the session and selects the window.<br />
<br />
#!/bin/bash<br />
<br />
PID=$(pidof $1)<br />
<br />
if [ -z "$PID" ]; then<br />
tmux new-session -d -s main ;<br />
tmux new-window -t main -n $1 "$*" ;<br />
fi<br />
tmux attach-session -d -t main ;<br />
tmux select-window -t $1 ;<br />
exit 0<br />
<br />
A derived version to run ''irssi'' with the ''nicklist'' plugin can be found on [[Irssi#irssi_with_nicklist_in_tmux|its ArchWiki page]].<br />
<br />
== See also ==<br />
* [http://mutelight.org/articles/practical-tmux Practical Tmux] by Brandur Leach, providing a number of configuration tips<br />
* [http://www.openbsd.org/faq/faq7.html#tmux Tmux tutorial] section from the OpenBSD FAQ<br />
* [http://www.dayid.org/os/notes/tm.html Screen and tmux feature comparison] page by Dayid Alan<br />
* [http://blog.hawkhost.com/2010/06/28/tmux-the-terminal-multiplexer/ Tmux tutorial Part 1] & [http://blog.hawkhost.com/2010/07/02/tmux-%E2%80%93-the-terminal-multiplexer-part-2 Part 2] blog posts on Hawk Host<br />
* [https://raw.github.com/kooothor/Dotfiles/master/.tmux.conf tmux.conf] example with CPU bar and shortcut to search man pages and display them vertically<br />
<br />
'''Forum threads'''<br />
* 2009-11-06 - Arch Linux - [https://bbs.archlinux.org/viewtopic.php?id=84157&p=1 Anyone loving Tmux in place of Screen? Info/Tips etc. URLs I've found]</div>Jorges