Tmux configuration

Very simply, you could run

tmux new-session -d "cmd1"; tmux new-window "cmd2"; tmux new-window "cmd3"; tmux a

You could get more complex with this, notice the commands are contextless and assume the latest session. tmux commands accept targets and more, the man page for tmux is well layed out with all the details and all the commands and their arguments.
 
For the autostart part I use /etc/local.rc and just prefix everything you want to run with tmux. You will also assign it a pane number so you can attach and detach from it or where it is positioned in your screen arrangement.

Example here:
 
As an example I have a script as below to show a few log files.
Code:
#!/bin/sh
tmux new-session -n logs -d 'tail -F /var/log/maillog' \; \
split-window -d 'tail -F /var/log/messages'\; \
split-window -d 'tail -F /var/log/unbound.log'\; \
split-window \; \
select-layout tiled \; \
attach
 
Does tmux have configuration options for starting windows 1,2,3 with specific commands with parameters autostarting in each?
 
Another example. Start a 6-node Redis cluster in six tmux windows. It's for Mac, but should be easy to modify
Code:
#!/bin/bash

# Prerequisites: brew install tmux redis

# Set Session Name
SESSION="Redis local cluster"
SESSIONEXISTS=$(tmux list-sessions 2> /dev/null | grep "$SESSION")

if [ $(uname -p) = "arm" ]
then
    BINPATH="/opt/homebrew/bin"
else
    BINPATH="/usr/local/bin"
fi
CONFIGPATH=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")

# Only create tmux session if it doesn't already exist
if [ "$SESSIONEXISTS" = "" ]
then
    # Start New Session with our name
    tmux new-session -d -s "$SESSION"
    tmux send-keys "cd $CONFIGPATH/7001" C-m "$BINPATH/redis-server $CONFIGPATH/7001/redis.conf" C-m

    CLUSTERCMD="redis-cli --cluster create 127.0.0.1:7001 "
    for i in $(seq 2 6)
    do
        tmux split-window -t "$SESSION"
        tmux send-keys -t "$SESSION" "cd $CONFIGPATH/700$i" C-m "$BINPATH/redis-server $CONFIGPATH/700$i/redis.conf" C-m
        tmux select-layout -t "$SESSION" tiled
        CLUSTERCMD="$CLUSTERCMD 127.0.0.1:700$i "
    done

    CLUSTERCMD="$CLUSTERCMD --cluster-replicas 1"
    echo $CLUSTERCMD
    echo "Run the command above to create the cluster. Only has to be run once."
fi

# Attach Session, on the Main window
tmux attach-session -t "$SESSION"
 
View: https://www.youtube.com/watch?v=-f9rz7joEOA


That showed me more than any other video on tmux, although comments mention:

I think an overview of the key bindings for copy-paste along with some examples of interaction with the system clipboard are required since its a must have feature for anyone using tmux in a productive workflow.

Also this looks useful:-


Also this especially for FreeBSD users.

 
Last edited:
One thing I could never figure out was how to open a new window, split it vertically with one app such as emacs in one half and something else like the output of 'ifconfig' in the other.

This is the shell script I run on startup which goes some way towards what I wanted to achieve.

Bash:
#!/usr/bin/env sh
tmux new-session -d emacs -nw
tmux new-window -d ncdu
#tmux split-window -h emacs -nw
tmux new-window -d mc
#tmux split-window
tmux new-window -d  'env SHELL=/usr/local/bin/bash mc'
tmux attach

To be honest, I'm not sure whether it's line 5 or 7 which starts up mc. but mc does start.
 
Returning to an old post... I could never understand the meanings of bind-key, bind and bind -n

Presumably

bind-key v split-window -v
means send prefix + v.

What does the '-v' at the end signify?

bind @ display-message "hello, world!."
means send prefix + @

How do I interpret this?
bind -n C-e neww -nemacs 'exec emacs -nw'
 
I use xfce workspaces each with several running xfce-terminals, which I may bring to full screen with F11 key. Some of these xfce-terminal sessions being attached to other devices via ssh have tmux sessions running. I've wondered if it wouldn't be more efficient to open them all in one tmux and use it to do the switching and windowing instead of xfce. I've not yet successfully penetrated the intricacies of tmux and tend to quit when things like copy and paste don't work quite exactly as I've become accustomed.
 
I use xfce workspaces each with several running xfce-terminals, which I may bring to full screen with F11 key. Some of these xfce-terminal sessions being attached to other devices via ssh have tmux sessions running. I've wondered if it wouldn't be more efficient to open them all in one tmux and use it to do the switching and windowing instead of xfce. I've not yet successfully penetrated the intricacies of tmux and tend to quit when things like copy and paste don't work quite exactly as I've become accustomed.
Personally, I am struggling to master tmux and wished it was the first thing I learnt when I started playing with FreeBSD ten years ago. It would have save me so much time. I would advise anyone starting out with FreeBSD to learn tmux and Doom Emacs as a priority.

As for copy and paste I still can't get comfortable with it.
 
Back
Top