Tmux

Miscellaneous tools
Post Reply
Message
Author
aragon
Posts: 1698
Joined: Mon 15 Oct 2007, 12:18
Location: Germany

Tmux

#1 Post by aragon »

Home: http://tmux.sourceforge.net/
Version: 1.7
tmux is a "terminal multiplexer", it enables a number of terminals (or windows) to be accessed and controlled from a single terminal. tmux is intended to be a simple, modern, BSD-licensed alternative to programs such as GNU screen.
it is highly recommended to read the manpage in /usr/share/doc .

extract:
Tmux may be controlled from an attached client by using a key combination of a prefix key, `C-b' (Ctrl-b) by default, followed by a command key.

Some of the default key bindings are:

Code: Select all

c    Create a new window. 
d    Detach the current client. 
l    Move to the previously selected window. 
n    Change to the next window. 
p    Change to the previous window. 
&    Kill the current window. 
,    Rename the current window. 
?    List all key bindings. 
Compiled and tested in 4.31.

aragon
Attachments
tmux-1.7.pet
(168.33 KiB) Downloaded 1012 times
Last edited by aragon on Mon 18 Feb 2013, 13:39, edited 1 time in total.

aragon
Posts: 1698
Joined: Mon 15 Oct 2007, 12:18
Location: Germany

#2 Post by aragon »

uploaded new version. see first post.

aragon

User avatar
sc0ttman
Posts: 2812
Joined: Wed 16 Sep 2009, 05:44
Location: UK

#3 Post by sc0ttman »

How do I change the modifier used to resize windows? It is ALT by default...

Currently, I can only resize windows in tmux in a terminal emulator, not when I actually drop out of X - because the ALT key is used to switch ttys (or something!) ... So resizing doesn't work, instead, I am sent to a login page ... (Hope I'm being clear!)
[b][url=https://bit.ly/2KjtxoD]Pkg[/url], [url=https://bit.ly/2U6dzxV]mdsh[/url], [url=https://bit.ly/2G49OE8]Woofy[/url], [url=http://goo.gl/bzBU1]Akita[/url], [url=http://goo.gl/SO5ug]VLC-GTK[/url], [url=https://tiny.cc/c2hnfz]Search[/url][/b]

aragon
Posts: 1698
Joined: Mon 15 Oct 2007, 12:18
Location: Germany

#4 Post by aragon »

hi scottman,please look at this exmple

https://github.com/myfreeweb/dotfiles/b ... /tmux.conf

for the resize-pane, to get an impression, how it works.

aragon

User avatar
sc0ttman
Posts: 2812
Joined: Wed 16 Sep 2009, 05:44
Location: UK

#5 Post by sc0ttman »

thats great, cheers.
[b][url=https://bit.ly/2KjtxoD]Pkg[/url], [url=https://bit.ly/2U6dzxV]mdsh[/url], [url=https://bit.ly/2G49OE8]Woofy[/url], [url=http://goo.gl/bzBU1]Akita[/url], [url=http://goo.gl/SO5ug]VLC-GTK[/url], [url=https://tiny.cc/c2hnfz]Search[/url][/b]

User avatar
rufwoof
Posts: 3690
Joined: Mon 24 Feb 2014, 17:47

Bump

#6 Post by rufwoof »

Bumping a (very) old thread.

tmux can be great for running terminal sessions on a server, disconnecting, and then reattaching again from the same or another machine - back into the exact same session on the server.

tmux activation keycode is typically ctrl-b, so after you've ssh into a remote server and ran tmux to present a terminal window, ctrl-b d will detatch the client from that. Later you might ssh back into the server again from the same or a different device, and run tmux attach ... to restore/attach back to the session again.

tmux also caters for opening up several different terminal windows, so you might for instance have one showing htop, another connected to IRC, another to news or mail ...etc. Provided the client device can ssh, then it can be a very lightweight device.
[size=75]( ͡° ͜ʖ ͡°) :wq[/size]
[url=http://murga-linux.com/puppy/viewtopic.php?p=1028256#1028256][size=75]Fatdog multi-session usb[/url][/size]
[size=75][url=https://hashbang.sh]echo url|sed -e 's/^/(c/' -e 's/$/ hashbang.sh)/'|sh[/url][/size]

User avatar
rufwoof
Posts: 3690
Joined: Mon 24 Feb 2014, 17:47

tmux collaboration

#7 Post by rufwoof »

Say you have a lightweight (low electrical power consumption) always on device, that's hard wired to the net, maybe connected to your family TV as a monitor and that also has a keyboard attached. If that is running something like openbsd that comes with mail, web ...etc. along with X all included and sensibly configured out of the box, then one function might be to open up ssh port forwarding to that device so you or other family members might remotely ssh into it. Assuming the PC also has a sound output (speakers connected) then a family member might ssh into the box from anywhere, on any device that supports ssh (ssh supporting handhelds, phone ...etc.), and play a sound file on that 'at home' PC to draw attention to family members at home - somewhat similar to ringing a phone. They might then switch the TV over to show the terminal screen and using tmux you can both setup and use the same terminal session.

You set up a tmux session using something like tmu new-session -s somesessionname
On the terminal session that runs that they will be entered into that session.
Another family member who has ssh into the same box using the same userid can show current sessions by running tmux ls ... to list any sessions. Seeing session somesessionname is running they can then attach to that using tmux attach -t somesessionname Thereafter both are seeing the exact same terminal session, anything than any one types/runs, so the other sees that. A third or more family members could also join in a similar manner. A form of group/collaboration instant messaging type function.

If you have tmux installed, you can try it out for yourself on the same single PC/laptop. Open two terminal windows and setup a tmux session as above in one, and then connect to that tmux session using the other.

If you have a always on server that might be running a httpd web server and using the TV as its monitor, then also constantly running a tmux session for others to connect to at any time is a low additional overhead. A form of low-cost/easy message board, where you at home can leave messages in the tmux session for other family members to ssh/tmux into and see; Or where remote family members can ssh/tmux into and "ring" the home PC system to draw attention to you asking to 'chat'.

One of the primary benefits of tmux is that sessions persist. After you ssh into a box and start a tmux session then you can detatch from that (or if the link fails), and later reattach again perhaps from another location using another device ... and the session will still be running as before. Kick off a compile for instance that might take many hours using your phone ssh/tmux into your 'server', disconnect and later reconnect again and the compile will still be running (assuming it hadn't completed).
[size=75]( ͡° ͜ʖ ͡°) :wq[/size]
[url=http://murga-linux.com/puppy/viewtopic.php?p=1028256#1028256][size=75]Fatdog multi-session usb[/url][/size]
[size=75][url=https://hashbang.sh]echo url|sed -e 's/^/(c/' -e 's/$/ hashbang.sh)/'|sh[/url][/size]

User avatar
rufwoof
Posts: 3690
Joined: Mon 24 Feb 2014, 17:47

#8 Post by rufwoof »

If you run your browser under user (restricted), and tmux under root ... and use tmux for your docs, data, diary ....etc. then that provides a element of data protection in the event of a browser flaw/hack. It's also quite nice/usable to have just the two running, a browser full of tabs that you flip between, and alt-tab to a tmux full of windows that you flip between. calcurse calander/appointments/todo is a nice little tmux program ... as seen/used in this video https://youtu.be/EMgOIbS4_Ms
[size=75]( ͡° ͜ʖ ͡°) :wq[/size]
[url=http://murga-linux.com/puppy/viewtopic.php?p=1028256#1028256][size=75]Fatdog multi-session usb[/url][/size]
[size=75][url=https://hashbang.sh]echo url|sed -e 's/^/(c/' -e 's/$/ hashbang.sh)/'|sh[/url][/size]

User avatar
rufwoof
Posts: 3690
Joined: Mon 24 Feb 2014, 17:47

#9 Post by rufwoof »

Run mc in a X session and the file manager/editor supports mouse controls. tmux however doesn't, but if you add the following to your .tmux.conf

Code: Select all

set-option -g mouse on

# make scrolling with wheels work
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
bind -n WheelDownPane select-pane -t= \; send-keys -M
then you can click on tmux window names to switch between windows ...etc.

The attached is pretty much my default first boot desktop (except for Firefox and mtpaint aren't running). The console also looks pretty much like the terminal pane (tmux with htop, mc, sh) - same colours etc. It's nice having the desktop (X) version also supporting mouse actions in both tmux and mc.

I used to use Libre writer mostly for documentation. Now I'm more inclined to use mc's internal editor to add text in a non distractive way, then run that through ispell to correct spellings, then add <p align="justify"> to each paragraph, and wrap that within a html table type structure (add <html><body><table><tr><td width=5%></td><td> at the top and /</td><td width=5%></td></tr></table></body></html> at the bottom. I have both of those as files so I can simple insert them. Having converted the file to html format I use a browser to view that file and then print it to a pdf file format.

tmux and mc work very well over the net, so even if you're running them remotely on a low power handheld or phone you can still access/edit/create documents in a consitent manner. With tmux you can even detatch from a tmux session and then reattach again later - even from a different device. And for collaboration two or more of you can even attach to the same session.
Attachments
s1.jpg
(91.63 KiB) Downloaded 608 times
[size=75]( ͡° ͜ʖ ͡°) :wq[/size]
[url=http://murga-linux.com/puppy/viewtopic.php?p=1028256#1028256][size=75]Fatdog multi-session usb[/url][/size]
[size=75][url=https://hashbang.sh]echo url|sed -e 's/^/(c/' -e 's/$/ hashbang.sh)/'|sh[/url][/size]

User avatar
rufwoof
Posts: 3690
Joined: Mon 24 Feb 2014, 17:47

#10 Post by rufwoof »

Added a additional tmux window that loads on startup, and split that window to show top in the top pane, and a cpu activity chart (script) in the bottom pane. For simplicity I've made the cpu-chart.sh script integral as part of my twin script ... where the twin script that loads up tmux is ...

Code: Select all

#!/bin/sh
#
# My tmux initialisation script to initialise tmux windows
#

export TERM=linux

# Start a X on ctrl-alt-F3 for our Xephyr/pflask container
X :1 -br -nolisten tcp -nolisten local vt3 &
sleep 3

# I've contained the terminal cpu chart script here to keep things together
cat <<'EOF' >/root/cpu-chart.sh
#!/bin/bash

# Original : https://github.com/mnmnc/cpuchart/blob/master/cpuchart.sh

# (Rufwoof) First time run if no cpu.log then shows message, ensure clean ...
if [ ! -f cpu.log ]; then
    touch cpu.log
fi

# VARIABLES
  columns=76

# FUNCTIONS #
function draw()
{
        echo -en "\033[s"
        tput cup $1 $2
        echo -en "\033[1;3$3m$4\033[0m"
        echo -en "\033[u"
}
function wipe()
{
        echo -en "\033[s"
        tput cup $1 $2
        echo -en "\033[1;31m \033[0m"
        echo -en "\033[u"
}
#############

# HIDE CURSOR AND CLEAR SCREEN #
  tput civis & clear

while true; do

    # COLLECT UPDATE
    #

    idle=`mpstat 1 1 | grep "Average" | tail -1 | sed 's/ \+/ /g' | cut -d " " -f 11 | tr -d "\n"`;

    usage=`echo "scale=0;(100-$idle)/10" |bc`;

    # BACKUP PREVIOUS DATA #
    # CUT THE OLDEST LINE  #
    cat cpu.log | tail -$columns >> temp.tmp

    # UPDATE STATS #
    if [[ $usage -eq 0 ]];
    then
            # IF USAGE LESS THEN 20% DRAW SINGLE BAR #
            echo "1" >> temp.tmp
    else
            echo $usage >> temp.tmp
    fi

    # UPDATE LOG #
    cat temp.tmp > cpu.log
    rm temp.tmp

    # DRAW GRAPH #

    var=6;
    # BEGIN FROM COLUMN 1 #
    j=1

    while read usage
    do
            # DRAW USAGE #
            for i in `seq 1 $usage`
            do
                    top=`echo "11-$usage" | bc`
                    var=`echo "11-$i"     | bc`

                    if [[ $usage -gt 3 ]]; then
                            if [[ $usage -gt 6 ]]; then
                                    if [[ $var -eq $top ]];then
                                            draw    $var $j "1" "+"
                                    else
                                            draw    $var $j "1" "|"
                                    fi
                            else
                                    if [[ $var -eq $top ]];then
                                            draw    $var $j "3" "+"
                                    else
                                            draw    $var $j "3" "|"
                                    fi
                            fi
                    else
                            if [[ $var -eq $top ]]; then
                                    draw     $var $j "2" "+"
                            else
                                    draw     $var $j "2" "|"
                            fi
                    fi
            done

            # WIPE PREVIOUS BAR REMAINNING IF THEY EXIST #
            usage=`echo "$usage+1" | bc`

            for k in `seq $usage 11`;
            do
                    var=`echo "11-$k" | bc`
                    wipe $var $j
            done

            # PROCEED TO NEXT COLUMN #
            j=`echo "$j+1" | bc`

            # ADD LATENCY IF NEEDED #
            sleep 0.1

    done < "cpu.log"
    tput cup 11 0
#
done
EOF
# Make sure it's executable
chmod +x /root/cpu-chart.sh


########
# TMUX #
########

# start a tmux session, detach
# then send commands to that before reattaching to it
# Note that C-m is carriage return (enter)

# create a tmux session called work, and deattach so we can send keys to it
# send keys to rename the first window to admin (my tput menu)

tmux new -s work -d
tmux rename-window -t work admin
tmux send-keys -t work '/root/admin' C-m

# Add another tmux window, rename it to xephyr and load Xephyr
tmux new-window -t work
tmux rename-window -t work xephyr
tmux send-keys -t work 'ewin' C-m

# Add another system monitor window, split horizontally with top and cpu-chart
tmux new-window -t work
tmux rename-window -t work Mon
tmux send-keys -t work 'top' C-m # set it to display top
tmux split-window -v
tmux send-keys -t work '/root/cpu-chart.sh' C-m # lower half set to show cpu

# and finally select which window to show first and attach to the tmux work session
tmux select-window -t work:1
tmux attach -t work
I've compiled/installed fbcat into my EasyOS now, so the following image was captured using

Code: Select all

fbgrab -s 4 tmux.png
(i.e. entered in another tmux window and had a 4 second delay before the snapshot was taken ... i.e. enough time to flip to the tmux pane showing the top/chart before the snapshot occurred).
Attachments
tmux.png
(6.7 KiB) Downloaded 508 times
[size=75]( ͡° ͜ʖ ͡°) :wq[/size]
[url=http://murga-linux.com/puppy/viewtopic.php?p=1028256#1028256][size=75]Fatdog multi-session usb[/url][/size]
[size=75][url=https://hashbang.sh]echo url|sed -e 's/^/(c/' -e 's/$/ hashbang.sh)/'|sh[/url][/size]

User avatar
rufwoof
Posts: 3690
Joined: Mon 24 Feb 2014, 17:47

#11 Post by rufwoof »

If you set your local tmux Prefix key to backtick, then that doesn't conflict with a remote tmux (i.e. ssh into another box) that is using the standard ctrl-b tmux Prefix. I also set my local tmux menu to show at the top of screen, the opposite of the default bottom of screen
Image
clickable thumbnail
[size=75]( ͡° ͜ʖ ͡°) :wq[/size]
[url=http://murga-linux.com/puppy/viewtopic.php?p=1028256#1028256][size=75]Fatdog multi-session usb[/url][/size]
[size=75][url=https://hashbang.sh]echo url|sed -e 's/^/(c/' -e 's/$/ hashbang.sh)/'|sh[/url][/size]

User avatar
rufwoof
Posts: 3690
Joined: Mon 24 Feb 2014, 17:47

#12 Post by rufwoof »

As a alternative to tmux, tilda terminal emulator combined with screen multiplexor works well IMO http://www.murga-linux.com/puppy/viewtopic.php?t=116073
[size=75]( ͡° ͜ʖ ͡°) :wq[/size]
[url=http://murga-linux.com/puppy/viewtopic.php?p=1028256#1028256][size=75]Fatdog multi-session usb[/url][/size]
[size=75][url=https://hashbang.sh]echo url|sed -e 's/^/(c/' -e 's/$/ hashbang.sh)/'|sh[/url][/size]

User avatar
rufwoof
Posts: 3690
Joined: Mon 24 Feb 2014, 17:47

#13 Post by rufwoof »

tmux prefix key (usually Ctrl-b by default, but that I like to set to backtick) followed by a window number usually flips to that window. In some setup's however doing that for window 0 doesn't work, whilst 1 onward are fine.

To skip starting from window 0 to instead start from window 1 add

Code: Select all

set -g base-index 1
setw -g pane-base-index 1
to your .tmux.conf, and after a restart your window numbers will start from 1

Image
[size=75]( ͡° ͜ʖ ͡°) :wq[/size]
[url=http://murga-linux.com/puppy/viewtopic.php?p=1028256#1028256][size=75]Fatdog multi-session usb[/url][/size]
[size=75][url=https://hashbang.sh]echo url|sed -e 's/^/(c/' -e 's/$/ hashbang.sh)/'|sh[/url][/size]

User avatar
rufwoof
Posts: 3690
Joined: Mon 24 Feb 2014, 17:47

#14 Post by rufwoof »

If rather than running tmux at the local end in addition to running tmux on a ssh server end, you instead run Screen at the local end - that uses ctrl-a as its default command key, then to redefine that in Screen to be ctrl ` you need to add a entry of escape ^`` into your screenrc file (/etc/screenrc for global, or ~/.screenrc for user specific).

If using Screen at the local end (laptop running screen), then say you're ssh connected to hashbang (a ssh server) that runs tmux by default, and your connected to irc within that, and you want to copy a url/link from a irc posting and paste that url/link into you android phone (that you have wifi connected to your laptop perhaps using sshelper android app), then with the above ctrl ` tweak you

ctrl-` [ ... to initiate copy mode
use the arrows to navigate to the start of the text/link you want to copy
press enter (or spacebar) to start copying
arrow to the end of the text to be copied
press Enter (or spacebar) again to end the selection

That text will then have been copied into the copy buffer. Switch to where you have your android phone mounted (I prefer mc as the file manager) on your laptop and paste the buffered text using

ctrl-` ] ... control and backtick pressed together, released and then press ]

I tend to paste text into a html file in my Downloads folder on the android, so opening that file on the phone (using html viewer) reveals the copied/pasted link and a long press of that link will offer to open it in chrome.

Fundamentally having tmux set to use ` as its control key (rather than ctrl-b)
and screen set to use ctrl-` as its control key (rather than ctrl-a)
sits well together IMO

Scrolling/copy/paste using the control key along with [, arrow keys, ] and Enter (or spacebar) feels a little awkward at first, but with use soon becomes second nature. And using a common backtick key - ctrl ` for screen, ` alone for tmux is more consistent than separate ctrl a and ctrl b keys. With the definitions I outline in earlier posts the only inconvenience is having to press the backtick key twice to actually add a backtick character into a document/file you're editing on the remote (tmux) site.

I guess it would be nice to set up a key/launcher that automated the process of copying a url from my remote servers irc session so that it automatically opened on my android phones screen once selected, or some other quick/simple clipboard text type sharing between android phone, laptop and remote ssh servers. Something to add to my back-burner todo list.

PS With Screen (and assuming the default Ctrl+A control key) you can also use Ctrl+A > filename to paste the selection to a filename and Ctrl+A < filename to read a file into the selection buffer so you can then paste it out again using Ctrl+A ] ... or the same - but with Ctrl ` as the control key if redefined as per above.
[size=75]( ͡° ͜ʖ ͡°) :wq[/size]
[url=http://murga-linux.com/puppy/viewtopic.php?p=1028256#1028256][size=75]Fatdog multi-session usb[/url][/size]
[size=75][url=https://hashbang.sh]echo url|sed -e 's/^/(c/' -e 's/$/ hashbang.sh)/'|sh[/url][/size]

User avatar
rufwoof
Posts: 3690
Joined: Mon 24 Feb 2014, 17:47

#15 Post by rufwoof »

Cool trick with sshelper (android ssh server app).

On a linux box that is using 'screen' (terminal) you can copy a url (or whatever) using keystrokes of ...

Code: Select all

ctrl-a [
arrow to the start of text
spacebar to start selection of text
arrow to end of text
spacebar to end selection (copy to buffer/clipboard)
that text can then be pasted (wherever) using ...

Code: Select all

ctrl-a ]
If you have sshelper installed/running on your android then you can use sshelper's (android phone) clipboard load option by running something like the following on your linux box ...

Code: Select all

wget  --post-data="testing pasting to android" -O /dev/null -o /dev/null 192.168.1.10:8081
sshelper's clipboard read/write server runs on port 8081 by default. In this case for me the phone's IP when sshelper was running 192.168.1.10, yours will likely/maybe be different so change the 192.168.1.10 in the above accordingly.

So with perhaps a url link selected/copied in 'screen' (linux terminal session on your laptop) as per above, you can paste that directly into your androids clipboard, and then on the phone with chrome loaded you can simply long press the url bar, select the PASTE option and the phones browser is ready to load/show that url/link.
[size=75]( ͡° ͜ʖ ͡°) :wq[/size]
[url=http://murga-linux.com/puppy/viewtopic.php?p=1028256#1028256][size=75]Fatdog multi-session usb[/url][/size]
[size=75][url=https://hashbang.sh]echo url|sed -e 's/^/(c/' -e 's/$/ hashbang.sh)/'|sh[/url][/size]

User avatar
jplt3
Posts: 118
Joined: Mon 08 Apr 2019, 20:40
Location: Planet Earth

#16 Post by jplt3 »

Hello termnial/bash lovers :lol: ,

i use tmux 2.6 installed on my bionicpup64, i must have to add to /root/.profile to have my bashrc to be sourced

Code: Select all

source /root/.bashrc
find this help here
JpLt

Post Reply