Using tmux

my .tmux.conf:

Code:
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D

bind -n M-h split-window -h # Split panes horizontal
bind -n M-v split-window -v # Split panes vertically

bind -n C-M-Up resize-pane -U 1
bind -n C-M-Down  resize-pane -D 1
bind -n C-M-Left         resize-pane -L 1
bind -n C-M-Right         resize-pane -R  1

and a little script for open a couple of session in ssh:


Code:
tmux new-session 'ssh -l xx  xx.x.x.xx' \; \
    split-window -v 'ssh -l xx  xx.xx.xx.xx' \; \
    select-pane -t 0 \; \
    split-window -h 'ssh -l xx xx.xx.xx.xx' \; \
 
Here is also nice keybinding, it is very useful when you use tmux in tmux when connected via ssh:
Code:
# when tmux in tmux, ctrl+q as prefix for second session
bind-key -n C-q send-prefix
It adds ctrl+q as prefix for second tmux session
 
Last edited by a moderator:
What I didn't like in tmux, is its selection.
Because when you select something, after mouse button will be released,
output will be scrolled to bottom automatically... But it's very annoying IMHO (especially when you click
mouse accidentally when search something in 100 000 lines), and looks like a bug.

But here is a solution! install x11/xclip and add this to your ~/.tmux.conf:
Code:
# enable mouse
set -g mouse on

# use vi-like keybindings (required, or replace  "copy-mode-vi" with "copy-mode" from keybindings bellow)
set -wg mode-keys vi
# copy tmux selection to PRIMARY when selecting with mouse
bind -T copy-mode-vi	MouseDragEnd1Pane	select-pane\; send-keys -X copy-pipe 'xclip -in -selection primary'
bind -T copy-mode-vi	DoubleClick1Pane	select-pane\; send-keys -X select-word\; send-keys -X copy-pipe 'xclip -in -selection primary'
bind -T copy-mode-vi	TripleClick1Pane	select-pane\; send-keys -X select-line\; send-keys -X copy-pipe 'xclip -in -selection primary'

# mouse wheel scroll clears selection
bind -T copy-mode-vi	WheelUpPane		send-keys -X clear-selection\; send-keys -X -N 3 scroll-up
bind -T copy-mode-vi	WheelDownPane		send-keys -X clear-selection\; send-keys -X -N 3 scroll-down


# clear selection (and move cursor) with left click
bind -T copy-mode-vi	MouseDown1Pane		select-pane \; send-keys -X clear-selection

# paste tmux selection with middle click
bind -T root		MouseDown2Pane		paste-buffer

# space to start selection and copy it to PRIMARY
bind -T copy-mode-vi	Space			send-keys -X copy-pipe 'xclip -in -selection primary'\; send-keys -X begin-selection

# y - copy to CLIPBOARD
bind -T copy-mode-vi	y			send-keys -X copy-pipe 'xclip -in -selection clipboard'\; send-keys -X clear-selection

# q - clear selection
bind -T copy-mode-vi	q			send-keys -X clear-selection

# press escape to exit copy mode
bind -T copy-mode-vi	Escape			send-keys -X cancel

# home & end in copy mode
bind -T copy-mode-vi	Home			send -X start-of-line
bind -T copy-mode-vi	End			send -X end-of-line

# ctrl+right/left in copy mode
bind -T copy-mode-vi	C-Right			send-keys -X next-word
bind -T copy-mode-vi	C-Left			send-keys -X previous-word
Then execute % tmux source ~/.tmux.conf or restart tmux ( % tmux kill-session -a && tmux kill-session).
Now tmux selection will be added to PRIMARY selection automatically,
so it is possible to paste selected output with shift + insert/middle mouse click.
Also now, finally, output won't be scrolled to the bottom after mouse selection!
So now tmux selection works almost like in a regular terminal!
Tested with tmux-2.6.

Also do not forget that it is possible to search in tmux, and it is quite handy,
just press "/" when you're using "copy mode" in tmux. To start copy mode, press prefix - [.
Also it is possible to start copy mode with prefix - PageUp or mouse wheel scroll.
 
Last edited by a moderator:
Also it is possible to configure sysutils/tmux to change your terminal emulator titles.
Man page says that you just need to add
Code:
set -g set-titles on
set -g set-titles-string "#{session_name}: [#{window_name}] #{pane_current_path}"
to ~/.tmux.conf. But this doesn't work as expected.
To make it work with xterm and urxvt, you also need to add
Code:
set -g terminal-overrides ',xterm*:XT,rxvt*:XT'
 
Last edited by a moderator:
Just thought I'd let people know, in case they didn't, that there is a pair of tools which, together, provide similar functionality to screen / tmux: dvtm and abduco.

dvtm is more of a "tiling window manager for the console", while abduco is strictly a session management tool.
 
dvtm is more of a "tiling window manager for the console", while abduco is strictly a session management tool.
And for tabs management you, also, need terminal emulator with tabs support,
so why not to use one tool, that should replace 3? Considering that it is still very lightweight and very useful.

Aslo, tmux comes preinstalled on OpenBSD, and, even, as far as I remebmer, on Ubuntu server(s) (and, it seems, on new RHEL too).
 
Last edited by a moderator:
Here is my current sysutils/tmux configuration, I like it much,
it changes xterm and urxvt titles to some useful info, also it changes
cursor color, it uses x11/xclip to copy highlighted text to PRIMARY or CLIPBOARD.
Also some useful keybindings present and nice colors :)
wag5fpn.png

~/.tmux.conf:
Code:
set -s default-terminal screen-256color
set -s history-file ~/.tmux_history
set -s escape-time 0
set -g default-command "${SHELL}"
set -g history-limit 100000
set -g destroy-unattached off
set -g mouse on
set -g base-index 1
set -g display-time 3000
set -g focus-events on
set -g renumber-windows on
set -g set-titles on
set -g set-titles-string "#{session_name}: [#{window_name}] #{pane_current_path}"
set -g terminal-overrides ',xterm*:Cr=\E]12;green\007:XT,rxvt*:Cr=\E]12;green\007:XT'
set -g status on
set -g status-keys vi
set -g status-interval 5
set -g status-justify left
set -g status-position bottom
set -wg mode-keys vi
set -wg xterm-keys on
set -wg monitor-bell on
set -wg alternate-screen on
set -wg automatic-rename on
set -wg word-separators ' ()<>[]{}|=,;:&?*@^"'
set -wg clock-mode-colour green
# Colors
set -g status-style "bg=#000000"
set -g message-command-style "bg=#000000,fg=#00d700"
set -g message-style "fg=#000000,bg=#00d700"
set -g mode-style "fg=#ff0000,bold"
set -g pane-border-style 'fg=#ffffff'
set -g pane-active-border-style 'fg=#ff0000'
set -g status-left ' #[fg=#ff0000,bold]#S:#[default]'
set -g window-status-format ' #[fg=#b2b2b2]#I-#W#[fg=#ffff00,bold]#F#[fg=#000000]#[default]'
set -g window-status-current-format ' #[fg=#005fff,bold]#I-#W#F#[default]'
set -g window-status-bell-fg "#ffffff"
set -g window-status-bell-bg "#ff0000"
set -g status-right '#[fg=#ffd700,bold] #(sysctl vm.loadavg | cut -d \  -f 3-5) #[default]'
# Keybindings
# use ctrl+space as prefix and ctrl+a as prefix for second tmux session
set -g prefix C-space
bind-key -n C-a send-prefix
unbind  C-b
# reload tmux configuration with prefix+r
bind r source ~/.tmux.conf\; display ' Configuration RELOADED'
# split panes with prefix+v or h using current path
bind v split-window -h -c "#{pane_current_path}"
bind h split-window -v -c "#{pane_current_path}"
# prefix+N to create new session
bind N new-session
# prefix+d to close current tab
bind d kill-window
# prefix+k/K to kill current/all except current session
bind k  kill-session
bind K  kill-session -a
# prefix+m to turn on/off mouse
bind m  set-option -g mouse\; display ' Mouse #{?mouse,ENABLED,DISABLED}'
# prefix+/ to search
bind / copy-mode\; send-key ?
# alt+shift+up/down to create new/close tab
bind -n M-S-up new-window
bind -n M-S-down kill-window
# alt+shift+left/right to select prev/next tab
bind -n M-S-left prev
bind -n M-S-right next
# ctrl+shift+left/right to move tab
bind -n C-S-left swap-window -t -1
bind -n C-S-right swap-window -t +1
# ctrl+shift+pageup/pgdown to move tabs
bind -n C-S-Pageup swap-window -t -1
bind -n C-S-Pagedown swap-window -t +1
# ctrl+alt+n/s to create/select session
bind -n C-M-n new-session
bind -n C-M-s choose-session
# ctrl+alt+t/w to create new/close tab
bind -n C-M-t new-window
bind -n C-M-w kill-window
# ctrl+alt+pageup/pgdown to scroll tabs
bind -n C-M-Pageup prev
bind -n C-M-Pagedown next
# ctrl+alt+a to toggle status line
bind -n C-M-a set -g status

# copy tmux selection to PRIMARY when selecting with mouse
bind -T copy-mode-vi    MouseDragEnd1Pane    select-pane\; send-keys -X copy-pipe 'xclip -in -selection primary'
bind -T copy-mode-vi    DoubleClick1Pane     select-pane\; send-keys -X select-word\; send-keys -X copy-pipe 'xclip -in -selection primary'
bind -T copy-mode-vi    TripleClick1Pane     select-pane\; send-keys -X select-line\; send-keys -X copy-pipe 'xclip -in -selection primary'
# mouse wheel scroll clears selection
bind -T copy-mode-vi    WheelUpPane          send-keys -X clear-selection\; send-keys -X -N 3 scroll-up
bind -T copy-mode-vi    WheelDownPane        send-keys -X clear-selection\; send-keys -X -N 3 scroll-down
# clear selection (and move cursor) with left click
bind -T copy-mode-vi    MouseDown1Pane       select-pane \; send-keys -X clear-selection
# paste tmux selection with middle click
bind -T root    MouseDown2Pane               paste-buffer
# v - start selection and auto copy it to PRIMARY
bind -T copy-mode-vi    v                    send-keys -X copy-pipe 'xclip -in -selection primary'\; send-keys -X begin-selection
# y - copy to CLIPBOARD when in copy mode
bind -T copy-mode-vi    y                    send-keys -X copy-pipe 'xclip -in -selection clipboard'\; send-keys -X clear-selection
# q - quit copy mode
bind -T copy-mode-vi    q                    send-keys -X cancel
# escape - clear selection in copy mode
bind -T copy-mode-vi    Escape               send-keys -X clear-selection
# home & end in copy mode
bind -T copy-mode-vi    Home                 send -X start-of-line
bind -T copy-mode-vi    End                  send -X end-of-line
# ctrl+right/left in copy mode
bind -T copy-mode-vi    C-Right              send-keys -X next-word
bind -T copy-mode-vi    C-Left               send-keys -X previous-word
 
Last edited by a moderator:
It would be good to read the configs and the experience of a user of tmux and fvwm2. I have fvwm2 as my desktop manager but it is proving difficult to use. ATM, tmux appears capable of achieving most of the tasks I would want fvwm2 to do - opening terminals, launching apps/clients, etc.

I would rather want to use one tmux instance to run multiple terminals at different fvwm2 decks/pages. Yet, I would want to use the same keybindings for both of them. I have learnt and forgotten so many keybindings as I used (preferably jumped from) yakuake, terminator, emacs to fvwm2+tmux
 
It would be good to read the configs and the experience of a user of tmux and fvwm2. I have fvwm2 as my desktop manager but it is proving difficult to use. ATM, tmux appears capable of achieving most of the tasks I would want fvwm2 to do - opening terminals, launching apps/clients, etc.

I would rather want to use one tmux instance to run multiple terminals at different fvwm2 decks/pages. Yet, I would want to use the same keybindings for both of them. I have learnt and forgotten so many keybindings as I used (preferably jumped from) yakuake, terminator, emacs to fvwm2+tmux

FVWM doesn't related anyhow to tmux and tmux doesn't related anyhow to FVWM,
it is two completely different applications, tmux is a BSD licensed terminal multiplexer,
and FVWM is a GPL licensed window manager, there is no connection between them.

But if you really want to use these applications, start with FVWM and tmux manual pages,
launch it with % man fvwm and % man tmux, these manual pages are pretty complete,
so it includes a lot of useful information.
 
FVWM doesn't related anyhow to tmux and tmux doesn't related anyhow to FVWM,
it is two completely different applications, tmux is a BSD licensed terminal multiplexer,
and FVWM is a GPL licensed window manager, there is no connection between them.
Thanks ILUXA, I got a better picture of TMUX awhile ago before reading this reply. There are however some interesting extensions in TMUX to check out - scct, tmux-cpu-mem-load and wemux.
 
I am liking this tool. It will reduce the number of decks/panels/pages needed. But at the moment, as I build & install it with postmaster, it is adding perl 5.24 to the existing Perls(5.26 & site_*).

I am wanting to rebuild it with the default version set to 5.26 set in make.conf. Any warnings from the community?
 
I am liking this tool. It will reduce the number of decks/panels/pages needed. But at the moment, as I build & install it with postmaster, it is adding perl 5.24 to the existing Perls(5.26 & site_*).

I am wanting to rebuild it with the default version set to 5.26 set in make.conf. Any warnings from the community?
I might have found an answer to this enquiry. It must be one of the tmux extensions requiring the old Perl.
 
I am wondering of it is possible to autostart tmux on boot as a service. I have tried it.
This is an excerpt of my rc.d/customtmux script.

Code:
rcvar=customtmux_enable

load_rc_config "$name"


: ${customtmux_enable:="NO"}

startcommand="/bin/sh -c '/usr/local/bin/tmux new-session -s host'"
But it won't run. Placing a line like 'tmux doas....' in .cshrc/.zshrc/.profile/.bash_profile won't work too.

I have also considered sysutils/wemux but I don't think it would do it. Isn't it possible to start a terminal as a service on boot without a human intervention? I would like have a tmux session running on (re)starting my box.
 
But it won't run. Placing a line like 'tmux doas....' in .cshrc/.zshrc/.profile/.bash_profile won't work too.
Add
Code:
[[ -z $TMUX ]] && exec tmux
to your ~/.bashrc, if your login shell is bash,
or to ~/.zshrc, if your login shell is zsh,
or add
Code:
if (! $?TMUX) exec tmux
to ~/.cshrc, if you use tcsh.
To change your login shell, execute % chsh, then edit "Shell:" line and relogin.
 
To make your tmux config usable on different operating systems,
it is possible to use "if-shell" in your ~/.tmux.conf file,
For example, to use different "status-right" on different OS-es,
add this to your config:
Code:
# different right status line for FreeBSD and Linux
if-shell 'test "$(uname)" = "FreeBSD"' 'set -g status-right "#[fg=#ffd700,bold] #(sysctl vm.loadavg | cut -c 14-28) #[default]"'
if-shell 'test "$(uname)" = "Linux"' 'set -g status-right "#[fg=#00d700,bold]#(cat /proc/loadavg | tr . , | cut -c 1-14) #[default]"'
It is possible to do the same with prefix, etc.
Code:
# use ctrl+a as prefix for FreeBSD and ctrl+space for Linux
if-shell 'test "$(uname)" = "FreeBSD"' 'set -g prefix C-a'
if-shell 'test "$(uname)" = "Linux"' 'set -g prefix C-space'
unbind  C-b
Different prefixes are useful when you're using tmux in tmux when using ssh.
LkCam6b.png
 
Last edited by a moderator:
Add
Code:
[[ -z $TMUX ]] && exec tmux
to your ~/.bashrc, if your login shell is bash,
or to ~/.zshrc, if your login shell is zsh,
or add
Code:
if (! $?TMUX) exec tmux
to ~/.cshrc, if you use tcsh.
To change your login shell, execute % chsh, then edit "Shell:" line and relogin.
It did not do the trick. TMUX does not start on booting or logging in. I use cshrc and did as you instructed. I also tried the security/doas for different users but no luck. TMUX only starts when I manually start it.
 
It did not do the trick. TMUX does not start on booting or logging in. I use cshrc and did as you instructed. I also tried the security/doas for different users but no luck. TMUX only starts when I manually start it.
There is no need in doas or sudo or something else,
because tmux shouldn't be launched as root,
create EMPTY ~/.cshrc with only one line inside --
Code:
if (! $?TMUX) exec tmux
Then start terminal session. Tmux session will be launched 100%.
Or install zsh, set it as your login shell (execute % chsh -s /usr/local/bin/zsh)
and re-login, then add "[[ -z $TMUX ]] && exec tmux" to ~/.zshrc.

To find out what shell you're using now, execute % echo $0.
 
Last edited by a moderator:
I use this in my .bashrc on FreeBSD and .profile on OpenBSD.
I don't actually know what the side effect of using exec is in a .profile but it seems to work for now.

Code:
if [ -z "$TMUX" ]; then
  tmux has-session >/dev/null 2>&1
  RC=$?

  if [ $RC = 0 ]; then
    exec tmux attach
  else
    exec tmux
  fi
fi

Basically it goes like:
  1. If not running in tmux already ($TMUX variable)
  2. Check if there is a session running by storing error code from the "has-session" parameter to tmux
  3. If there was no "error" (RC = 0) then attempt to attach
  4. If there was an "error" (RC != 0) then start a fresh tmux
You might also like to do this only for the first tty so something like:

Code:
if [ "`tty`" = "/dev/tty1" ]; then
  # do the tmux stuff
fi

Though I personally just call startx if I log in on my first TTY.
 
IMO it's not very practical to attach to existing tmux session every
time when starting new terminal session. If you don't want to have
many tmux sessions running, better use $TMOUT variable
with your zsh or bash shell:
Code:
# autologout when no commands were entered for one hour
export TMOUT=3600
With tcsh shell use:
Code:
set autologout = 60        # autologout after one hour of inactivity
Then tmux sessions will be automatically closed, if you don't use it.
Also it is very easy to switch sessions when using tmux, use "prefix+s",
or to destroy existing session — use prefix+s to show session list,
then highlight session and use prefix + :. Then enter "kill-session"
and confirm.
 
I actually use "sh". So, I modified the above to:
Code:
if [ -z "$TMUX" ]; then
  exec tmux new-session -s host
fi
 
I actually use "sh".
It is not the best idea to use /bin/sh as interactive shell,
because it is a scripting shell, so use it for scripts.
You're missing a lot of nice features while using sh,
For example, to manipulate files in zsh, bash and tcsh
it is possible to use: % cp /usr/local/dir/{file1,file2},
this command will copy file1 to file2, or to rename file,
use % mv /usr/local/dir/file1{,.bak} this command
will rename file1 to file1.bak. You're missing a lot of other features
also. I suggest you to try shells/zsh, it is very customizable and usable.
You can try to use it with my config -
https://forums.freebsd.org/threads/share-your-zshrc-file.62653/
 
It is not the best idea to use /bin/sh as interactive shell,
because it is a scripting shell, so use it for scripts.
You're missing a lot of nice features while using sh,
For example, to manipulate files in zsh, bash and tcsh
it is possible to use: % cp /usr/local/dir/{file1,file2},
this command will copy file1 to file2, or to rename file,
use % mv /usr/local/dir/file1{,.bak} this command
will rename file1 to file1.bak. You're missing a lot of other features
also. I suggest you to try shells/zsh, it is very customizable and usable.
You can try to use it with my config -
https://forums.freebsd.org/threads/share-your-zshrc-file.62653/
Thanks ILUXA, I like those shortcuts. I will soon give zsh with your config a shot.
 
I don't believe anything in my shell code snippet above needed changing for /bin/sh because that is what I always target for my scripts.
Remember, things like '[' and ']' are actually separate programs that sh runs.
 
IMO, using /bin/sh as interactive shell is like using old single-speed bicycle, while using zsh is like using new, fully featured Japanese superbike (tcsh is like a classic chopper :cool: it's not new and too fast, but it's pretty usable, and bash is like a hipster moped :)). Moreover, it is even possible to use zsh to start bash scripts, just create zsh to bash symlink and use it with your bash scripts.
 
Back
Top