tmux - ovigia/distrosnetinstall GitHub Wiki

Tmux

set vi:ft=markdown

tmux -V

tmux is a terminal multiplexer, allowing a user to access multiple separate terminal sessions inside a single terminal window or remote terminal session. It is useful for dealing with multiple programs from a command-line interface, and for separating programs from the Unix shell that started the program.[2] It provides much of the same functionality as GNU Screen, but it is distributed under a BSD-like license.
tmux - Wikipedia

Tmux Wikipedia

índice

links

tmuxinator

dicas

loads the vi keymap

set-window-option -g mode-keys vi

editing mode
etc/.tmux.conf at 4c856eb8db63012b8fd319a404420f3a08f564ec · josuah/etc

set -g status-keys vi

Vi mode in tmux

list-keys -T copy-mode-vi

In version 2.3 and below, the syntax is different:

list-keys -t vi-copy

With this done, within a tmux instance, pressing Ctrl+B and then [ will enter copy mode, allowing you to copy text or view the history of the buffer, including searching with / and ?. Most of the basic vi movements work, including screenwise vertical movement commands like Ctrl+F and Ctrl+B. You can leave this mode just by pressing Enter, or you can start a selection by pressing Space on a character, moving to another, and then pressing Enter. If you have text copied like this you can paste it into any tmux window in that session by pressing Ctrl+B and then ].

If you don’t mind artifically introducing a few Vim-only features to the vi mode, you can set things up so that v starts a selection and y finishes it in the same way that Space and Enter do, more like Vim:

bind-key -T copy-mode-vi 'v' send -X begin-selection
bind-key -T copy-mode-vi 'y' send -X copy-selection-and-cancel

In version 2.3 and below, the syntax is very different:

bind-key -t vi-copy 'v' begin-selection
bind-key -t vi-copy 'y' copy-selection

If you want to always create two windows when a session is created, you can put this into your configuration

new-window -n some_name
new-window -n another_name

tmux com bash_completion
srsudar/tmux-completion: Better tmux tab completion

sudo cp Downloads/tmux /etc/bash_completion.d/

vi .bash_profile

source /etc/bash_completion.d/tmux

If you have a Tmux window divided into panes, you can use the synchronize-panes window option to send each pane the same keyboard input simultaneously:
Sync tmux panes | Arabesque

Ctrl-A
:setw synchronize-panes {on/off}

Copy mode:

Pressing PREFIX [ places us in Copy mode. We can then use our movement keys to move our cursor around the screen. By default, the arrow keys work. we set our configuration file to use Vim keys for moving between windows and resizing panes so we wouldn’t have to take our hands off the home row. tmux has a vi mode for working with the buffer as well. To enable it, add this line to .tmux.conf:

setw -g mode-keys vi

With this option set, we can use h, j, k, and l to move around our buffer.

To get out of Copy mode, we just press the ENTER key. Moving around one character at a time isn’t very efficient. Since we enabled vi mode, we can also use some other visible shortcuts to move around the buffer.

For example, we can use "w" to jump to the next word and "b" to jump back one word. And we can use "f", followed by any character, to jump to that character on the same line, and "F" to jump backwards on the line.

   Function                vi             emacs
   Back to indentation     ^              M-m
   Clear selection         Escape         C-g
   Copy selection          Enter          M-w
   Cursor down             j              Down
   Cursor left             h              Left
   Cursor right            l              Right
   Cursor to bottom line   L
   Cursor to middle line   M              M-r
   Cursor to top line      H              M-R
   Cursor up               k              Up
   Delete entire line      d              C-u
   Delete to end of line   D              C-k
   End of line             $              C-e
   Goto line               :              g
   Half page down          C-d            M-Down
   Half page up            C-u            M-Up
   Next page               C-f            Page down
   Next word               w              M-f
   Paste buffer            p              C-y
   Previous page           C-b            Page up
   Previous word           b              M-b
   Quit mode               q              Escape
   Scroll down             C-Down or J    C-Down
   Scroll up               C-Up or K      C-Up
   Search again            n              n
   Search backward         ?              C-r
   Search forward          /              C-s
   Start of line           0              C-a
   Start selection         Space          C-Space
   Transpose chars                        C-t

Configurations Options:

# Mouse support - set to on if you want to use the mouse
* setw -g mode-mouse off
* set -g mouse-select-pane off
* set -g mouse-resize-pane off
* set -g mouse-select-window off

# Set the default terminal mode to 256color mode
set -g default-terminal "screen-256color"

# enable activity alerts
setw -g monitor-activity on
set -g visual-activity on

# Center the window list
set -g status-justify centre

# Maximize and restore a pane
unbind Up bind Up new-window -d -n tmp \; swap-pane -s tmp.1 \; select-window -t tmp
unbind Down
bind Down last-window \; swap-pane -s tmp.1 \; kill-window -t tmp

Is there an easy way to “restart” a tmux pane?

So <prefix>+: and then enter respawn-pane -k and press Enter

Zooming tmux panes Zooming tmux panes

Ctrl-a z

Tmux environment variables Tmux environment variables

TERM=screen-256color
set-option -g default-terminal $TERM

TMUX_COLOUR_BORDER="colour237"
TMUX_COLOUR_ACTIVE="colour231"
TMUX_COLOUR_INACTIVE="colour16"

set-window-option -g window-status-activity-bg $TMUX_COLOUR_BORDER
set-window-option -g window-status-activity-fg $TMUX_COLOUR_ACTIVE
set-window-option -g window-status-current-format "#[fg=$TMUX_COLOUR_ACTIVE]#I:#W#F"
set-window-option -g window-status-format "#[fg=$TMUX_COLOUR_INACTIVE]#I:#W#F"

The explicit commands to work with environment variables in .tmux.conf are update-environment, set-environment, and show-environment, and are featured in the manual

Reloading tmux config Reloading tmux config

com o ctrl+a

	:source-file ~/.tmux.conf

ou na shel usando o .tmux.conf
	
	tmux source-file ~/.tmux.conf

Automatic tmux titles

in the tmux manual, the following escape code incantation is given to update the window title from within the terminal:

$ printf '\033kWINDOW_NAME\033\\'

In much the same way that you can update the title of an xterm-compatible terminal emulator with control codes as part of the $PS1 variable defining the prompt, you can update the title of a tmux window to the current hostname (or any other relevant text) automatically by prefixing this call to the $PROMPT_COMMAND. This is best done in your .bashrc. The below code assumes you are using either screen or screen-256color as your $TERM string in your .tmux.conf:

case "$TERM" in
    screen*)
	        PROMPT_COMMAND="printf '\033k$(hostname)\033\\';"${PROMPT_COMMAND}
			        ;;
					esac

Watching with tmux The reason this becomes so useful in a system administration context is that when used with a terminal multiplexer, watch can be used to track the progress of any particular task, or to monitor system resources, as you work and change things. Some examples of simple but useful monitoring tasks are:

Load average — watch uptime
Disk space — watch df -h
Process count — watch 'ps -ef | wc -l'
Memory usage — watch free -m
Size of a volatile file — watch du -sh filename.sh

You can set these windows up using the usual key combinations, but if you have a long string of text or a known command that you want to start running in another window, it’s often easier to call the relevant tmux commands directly from the shell:

$ watch uptime
$ tmux split-window -dh "!!"

This will split the window into two parts, and run watch uptime in the new one, while leaving your cursor in the original window.

If you find yourself doing this often, you could make it into a shell function in your .bashrc or similar:

function tmw {
    tmux split-window -dh "$*"
}

You can then quickly run things like the following, as a kind of general method to background a monitoring or long-running task quickly:

$ tmw watch uptime
$ tmw htop
$ tmw rsync -arvz source::mnt/location /home/tom/destination

Blink tmux instead of sound

set -g visual-bell on

visual-bell [on | off] If this option is on, a message is shown on a bell instead of it being passed through to the terminal (which normally makes a sound). Also see the bell-action option.

bell-action [any | none | current | other] Set action on window bell. any means a bell in any window linked to a session causes a bell in the current window of that session, none means all bells are ignored, current means only bells in windows other than the current window are ignored and other means bells in the current window are ignored but not those in other windows.

Tmux set color of changed window

The activity and silence monitoring functions share a set of display configuration options:

    window-status-activity-attr
    window-status-activity-fg
    window-status-activity-bg

The default value of the “attr” is reverse, so you may want to set it to something else if you are also changing the colors (so that …-fg will set the effective foreground color instead of being reversed into the effective background color); see the list of attribute names in the man page description of message-attr.

The colors default to default, which just leaves at whatever they were last set to while building the status line; see the list of colors in the man page description of message-bg.

set-option -gw window-status-activity-attr bold
set-option -gw window-status-activity-bg black
set-option -gw window-status-activity-fg red

The other “alerts” (content monitoring and the bell) have their own display options (replace activity with content or bell in the option name).

There 3 solutions I know, none really ideal. You may put those commands in your ~/.tmux.conf or just run them directly as Tmux command via Ctrl-B :.

Monitor and alert whenever the output changes (you may then redirect output somewhere else so that output changes only after the command is complete):
    :set -g visual-activity on
    :setw -g monitor-activity on

    Monitor and alert whenever the output did not change for a while (30 seconds here):

    :set -g visual-silence on
    :setw -g monitor-silence 30

(deprecated and someday replaced by a better option) Monitor and alert whevener the output contains a string matching given pattern (and possibly run your command like my-command; echo foobar):

    :set -g visual-content on
    :setw -g monitor-content foo*bar

$ some-command; tmux display-message "Task 1 done". However the message will only show for a short duration defined via :set -g display-time 4000.

If you keep the visual-* to off (default), it'll only highlight the name of the window in which the alert occurred without showing a global alert status message.

Use zsh as my default shell when opening new panes. Tools I use: Tmux

set-option -g default-shell /usr/bin/zsh

Set the title of the current window to something descriptive for the entire tmux session. This results in the current window being called "1. neon-vm:0.0.0 emacsclient justinlilly@neon-vm:~"

 set-option -g set-titles on
 set-option -g set-titles-string '#H:#S.#I.#P #W #T' # window number,program name, active(or not)

Next are a bunch of options related to my status line. We enable the possibility for utf-8 characters in the status left and status right displays. The left side of the status line displays the hostname and the session number like "neon-vm:0 |". The right side shows a simple date and time. The center status line is centered such that it's distinct from the left and right.

 set-option -g status-utf8 on
 set -g status-bg black
 set -g status-fg green
 set -g status-left-length 15
 set -g status-left ' #[fg=cyan,bright]#10H#[fg=green]:#[fg=white]#S#[fg=green] | #[default]'
 set -g status-right '| #[fg=yellow]%y-%m-%d %H:%M '
 set -g status-justify centre

By default, windows only keep 2000 lines of history. Let's increase that to 100k. Nothing worse than something important being JUST off of screen.

 set -g history-limit 100000

Some nifty key bindings I've picked up here and there. I like the | and - variants which do window splits, which I picked up from . The S command will prompt for a hostname

bind-key r source-file ~/.tmux.conf
bind-key S command-prompt -p ssh: "new-window -n %1 'ssh %1'"
bind-key | split-window -h
bind-key - split-window -v

testa suporte terminal para utf8

echo -e '\xe2\x82\xac'

ssh remoto

tmux -CC on the remote instance will create a tmux session  
tmux -CC a after making an SSH connection will attach to a current tmux session 

How to copy and paste with a mouse with tmux - Unix & Linux Stack Exchange

xclip - Use system clipboard in vi-copy mode in tmux - Unix & Linux Stack Exchange

This was also answered here, but it took me a while to understand how to use it, so I'll explain for anyone else that was confused.

This is basically the setting you're going for:

(for tmux versions <2.5)

bind -t vi-copy y copy-pipe 'xclip -in -selection clipboard'

(for tmux versions >=2.5)

bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard'

Then hit Ctrl+b [ to enter copy mode. Then hit Space followed by whatever vi movement keys to make a selection. Then, instead of hitting Enter, hit y and the selection will be copied to the clipboard.

Note: this assumes you're using tmux's default bindings with vi keys.

Tmux has different key binding tables for different modes. So, bind-key -t vi-copy y sets the action for the y key in copy mode. Initially, I was confused because I was used to hitting Enter after making a selection. Enter is actually just the default key binding for the copy-selection command (when in copy mode). The copy-pipe command allows us to bind a new key to pipe the selection to a command, which in this case is xclip.

You can see all key bindings for copy mode by running list-keys -t vi-copy.

tmux cheat sheet

(C-x means ctrl+x, M-x means alt+x)

Prefix key

The default prefix is C-b. If you (or your muscle memory) prefer C-a, you need to add this to ~/.tmux.conf:

# remap prefix to Control + a
set -g prefix C-a
# bind 'C-a C-a' to type 'C-a'
bind C-a send-prefix
unbind C-b

I'm going to assume that C-a is your prefix.

Sessions, windows, panes

Session is a set of windows, plus a notion of which window is current.

Window is a single screen covered with panes. (Once might compare it to a ‘virtual desktop’ or a ‘space’.)

Pane is a rectangular part of a window that runs a specific command, e.g. a shell.

Getting help

Display a list of keyboard shortcuts:

C-a ?

Navigate using Vim or Emacs shortcuts, depending on the value of mode-keys. Emacs is the default, and if you want Vim shortcuts for help and copy modes (e.g. j, k, C-u, C-d), add the following line to ~/.tmux.conf:

setw -g mode-keys vi

Any command mentioned in this list can be executed as tmux something or C-a :something (or added to ~/.tmux.conf).

Managing sessions

Creating a session:

tmux new-session -s work

Create a new session that shares all windows with an existing session, but has its own separate notion of which window is current:

tmux new-session -s work2 -t work

Attach to a session:

tmux attach -t work

Detach from a session: C-a d.

Switch between sessions:

C-a (          previous session
C-a )          next session
C-a L          ‘last’ (previously used) session
C-a s          choose a session from a list

Other:

C-a $          rename the current session
C-a

Managing windows

Create a window:

C-a c          create a new window

Switch between windows:

C-a 1 ...      switch to window 1, ..., 9, 0
C-a 9
C-a 0
C-a p          previous window
C-a n          next window
C-a l          ‘last’ (previously used) window
C-a w          choose window from a list

Switch between windows with a twist:

C-a M-n        next window with a bell, activity or
               content alert
C-a M-p        previous such window

Other:

C-a ,          rename the current window
C-a &          kill the current window

Managing split panes

Creating a new pane by splitting an existing one:

C-a "          split vertically (top/bottom)
C-a %          split horizontally (left/right)

Switching between panes:

C-a left       go to the next pane on the left
C-a right      (or one of these other directions)
C-a up
C-a down
C-a o          go to the next pane (cycle through all of them)
C-a ;          go to the ‘last’ (previously used) pane

Moving panes around:

C-a {          move the current pane to the previous position
C-a }          move the current pane to the next position
C-a C-o        rotate window ‘up’ (i.e. move all panes)
C-a M-o        rotate window ‘down’
C-a !          move the current pane into a new separate
               window (‘break pane’)
C-a :move-pane -t :3.2
               split window 3's pane 2 and move the current pane there

Resizing panes:

C-a M-up, C-a M-down, C-a M-left, C-a M-right
               resize by 5 rows/columns
C-a C-up, C-a C-down, C-a C-left, C-a C-right
               resize by 1 row/column

Applying predefined layouts:

C-a M-1        switch to even-horizontal layout
C-a M-2        switch to even-vertical layout
C-a M-3        switch to main-horizontal layout
C-a M-4        switch to main-vertical layout
C-a M-5        switch to tiled layout
C-a space      switch to the next layout

Other:

C-a x          kill the current pane
C-a q          display pane numbers for a short while

Other config file settings

Force a reload of the config file on C-a r:

unbind r
bind r source-file ~/.tmux.conf

Some other settings that I use:

setw -g xterm-keys on
⚠️ **GitHub.com Fallback** ⚠️