Difference between revisions of "Tmux"

From ArchWiki
Jump to: navigation, search
m (codeline -> ic)
m (Split window and retain current directory: sourceforge carps at the trailing /)
Line 192: Line 192:
 
===Split window and retain current directory===
 
===Split window and retain current directory===
  
{{Note|1=With [http://tmux.svn.sourceforge.net/viewvc/tmux?view=revision&revision=2647/ revision 2647] this behavior became standard.}}
+
{{Note|1=With [http://tmux.svn.sourceforge.net/viewvc/tmux?view=revision&revision=2647 revision 2647] this behavior became standard.}}
  
 
====Fast method====
 
====Fast method====

Revision as of 18:24, 12 December 2011

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

Template:Article summary start

Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary end

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."

Tmux is notable as a BSD-licensed alternative to GNU Screen. Although similar, there are many differences between the programs, as noted on the 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.

Install

A stable version of tmux may be installed using pacman:

# pacman -S tmux

Alternatively, tmux-git is available from the AUR.

Configure

A user-specific configuration file should be located at Template:Filename, while a global configuration file should be located at Template:Filename. Default configuration files can be found in /usr/share/tmux/.

Key bindings

Prefix all commands with Ctrl-b
Cmd Action
c Create a new window
n Change to next window
p Change to previous window
" Split pane horizontally
% Split pane vertically
, Rename current window
o Move to next pane

By default, command key bindings are prefixed by Ctrl-b. For example, to vertically split a window type Ctrl-b %.

After splitting a window into multiple panes, you can resize a pane by the hitting prefix key (i.e. 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.

Tip: To mimic screen key bindings copy Template:Filename to either of the configuration locations.

Key bindings may be changed with the bind and unbind commands in Template:Filename. For example, you can change the prefix key (i.e. Ctrl-b) to Ctrl-a by adding the following commands in your configuration file:

unbind C-b
set -g prefix C-a

Additional ways to move between windows include:

Ctrl-b l (Move to the previously selected window)
Ctrl-b w (List all windows / window numbers)
Ctrl-b <window number> (Move to the specified window number, the default bindings are from 0 – 9)
Ctrl-b q  (Show pane numbers, when the numbers show up type the key to goto that pane)

What if you have 10+ windows open? Tmux has a find-window option & keybinding.

Ctrl-b f <window name> (Search for window name)
Ctrl-b w (Select from interactive list of windows)

Browsing URL's

To browse URL's inside tmux you must have urlview installed and configured:

bind-key u capture-pane \; save-buffer /tmp/tmux-buffer \; run-shell "$TERMINAL -e 'cat /tmp/tmux-buffer | urlview'"

Setting the correct term

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 Template:Filename:

set -g default-terminal "screen-256color" 

or in your Template:Filename with a test like:

# for tmux: export 256color
[ -n "$TMUX" ] && export TERM=screen-256color

If you enable xterm-keys in your Template:Filename, 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 Template:Filename and you can use "xterm-screen-256color" as your TERM:

# A screen- based TERMINFO that declares the escape sequences
# enabled by the tmux config "set-window-option -g xterm-keys".
#
# Prefix the name with xterm- since some applications inspect
# the TERM *name* in addition to the terminal capabilities advertised.
xterm-screen-256color|GNU Screen with 256 colors bce and tmux xterm-keys,

# As of Nov'11, the below keys are picked up by
# .../tmux/blob/master/trunk/xterm-keys.c:
	kDC=\E[3;2~, kEND=\E[1;2F, kHOM=\E[1;2H,
	kIC=\E[2;2~, kLFT=\E[1;2D, kNXT=\E[6;2~, kPRV=\E[5;2~,
	kRIT=\E[1;2C,

# Change this to screen-256color if the terminal you run tmux in
# doesn't support bce:
	use=screen-256color-bce,

Session initialization

You can have tmux open a session with preloaded windows by including those details in your Template:Filename:

new  -n WindowName Command
neww -n WindowName Command
neww -n WindowName Command

To start a session with split windows (multiple panes), include the splitw command below the neww you would like to split; thus:

new  -s SessionName -n WindowName Command
neww -n foo/bar foo
splitw -v -p 50 -t 0 bar
selectw -t 1 
selectp -t 0

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).

Note: Numbering for sessions, windows and panes starts at zero, unless you have specified a base-index of 1 in your Template:Filename

To manage multiple sessions, source separate session files from your conf file:

# initialize sessions
bind F source-file ~/.tmux/foo
bind B source-file ~/.tmux/bar

Scrolling issues

If you have issues scrolling with Shift-PageUp/Shift-PageDown in your terminal, try this:

set -g terminal-overrides 'xterm*:smcup@:rmcup@'

ICCCM Selection Integration

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 CLIPBOARD with the current tmux paste buffer using xclip:

Template:File

Tips & Tricks

Start tmux on every shell login

Simply add the following line of bash code to your .bashrc before your aliases; the code for other shells is very similar:

 [[ $TERM != "screen" ]] && tmux && exit

Example .bashrc:

 #
 # ~/.bashrc
 #
 
 # If not running interactively, do not do anything
 [[ $- != *i* ]] && return
 [[ $TERM != "screen" ]] && tmux && exit
 
 # Irrelevant from here on, but does show that things here are working from within tmux..
 alias svile="sudo vile"
 alias updatedb="sudo updatedb"
 alias sprunge="curl -F 'sprunge=<-' http://sprunge.us"
 pacman() {
         case $1 in
                 -S | -S[^sih]* | -R* | -U*)
                         /usr/bin/sudo /usr/bin/pacman-color "$@" ;;
                 *)      /usr/bin/pacman-color "$@" ;;
         esac
 }
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.

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.

 # TMUX
 if which tmux 2>&1 >/dev/null; then
     # if no session is started, start a new session
     if test -z ${TMUX}; then
         tmux
     fi
     # when quitting tmux, try to attach
     while test -z ${TMUX}; do
         tmux attach || break
     done
 fi
Note: Instead of using the bashrc file, you can launch tmux when you start your terminal emulator. (i. e. urxvt -e tmux)

Split window and retain current directory

Note: With revision 2647 this behavior became standard.

Fast method

Note: If you have set default-path to something for another convince; it will be reset to ~/

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.

 tmux set default-path $(pwd) \; split-window\; set default-path ~/

cd method

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, …).

Create a excutable file as follows, for example Template:Filename:

 #!/usr/bin/env bash
 PWD=`pwd`
 tmux split-window $1
 tmux send-keys " cd $PWD;clear"
 tmux send-keys "Enter"

and change the configure from:

 bind v split-window -h
 bind n split-window -v

to

 bind v send-keys " ~/mbin/split-tmux -h" \; send-keys "Enter"
 bind n send-keys " ~/mbin/split-tmux -v" \; send-keys "Enter"

/proc method

Note: This script, borrowed from Christian Neukirchen, reads the present pane's CWD from /proc/[pane's top program's $PID]/cwd. Some shells like zsh will not always properly update that entry.

Pasted into e. g. Template:Filename:

#!/bin/sh
# tmux-split-in-cwd - open a new shell with same cwd as calling pane

SIP=$(tmux display-message -p "#S:#I:#P")
PTY=$(tmux server-info |
        egrep flags=\|bytes |
        awk '/windows/ { s = $2 }
             /references/ { i = $1 }
             /bytes/ { print s i $1 $2 } ' |
        grep "$SIP" |
        cut -d: -f4)
PTS=${PTY#/dev/}
PID=$(ps -eao pid,tty,command --forest | awk '$2 == "'$PTS'" {print $1; exit}')
DIR=$(readlink /proc/$PID/cwd)

case "$1" in
  h) tmux splitw -h "cd '$DIR'; $SHELL"
     ;;
  v) tmux splitw -v "cd '$DIR'; $SHELL"
     ;;
  *) tmux neww "cd '$DIR'; $SHELL"
     ;;
esac

Template:Filename could thus contain:

bind | run '~/.scripts/tmux-split-in-cwd h' # horizontal split in cwd
bind _ run '~/.scripts/tmux-split-in-cwd v' # vertical split in cwd
bind m run '~/.scripts/tmux-split-in-cwd' # new window in cwd

Use tmux windows like tabs

The following settings added to Template:Filename allow to use tmux windows like tabs, such as those provided by the reference of these hotkeys — urxvt's tabbing extensions. An advantage thereof is that these virtual “tabs” are independent of the terminal emulator.

#urxvt tab like window switching (-n: no prior escape seq)
bind -n S-down new-window
bind -n S-left prev
bind -n S-right next
bind -n C-left swap-window -t -1
bind -n C-right swap-window -t +1

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.

It can also come handy to supplement the EOT hotkey Template:Keypress+Template:Keypress with one for tmux's detach:

bind-key -n C-j detach

Clients simultaneously interacting with various windows of a session

In “Practical Tmux”, Brandur Leach writes:

Template:Box

To avoid these issues he wrote the script “tmx” — the version below is slightly modified to execute “tmux new-window” if “1” is its second parameter. Invoked as 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.

Template:File

A useful setting for this is

setw -g aggressive-resize on

added to Template:Filename. 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.

Changing the configuration with tmux started

By default tmux reads Template:Filename only if it was not already running. To have tmux load a configuration file afterwards, execute:

tmux source-file <path>

This can be added to Template:Filename as e. g.:

bind r source-file <path>

See also

Forum threads