9

I'm using two separated sessions in tmux, and I have the following entires in /etc/tmux.conf :

set -g base-index 1

new -s logi -n cmd
neww -n logi "cat /dev/logi | ccze -m ansi -p syslog -C"
splitw -t 1 -v -p 50
selectw -t 2
selectp -t 0

new -s standard -n htop "htop"
neww -n cmd
splitw -t 2 -v -p 50
selectw -t 2 
selectp -t 1

I start session standard by invoking the following command:

urxvtc -name 'tmux' -e bash -c 'tmux attach-session -t standard'

If there's no session, it creates one, if there is one, it attaches. As you can see I have two windows, one of which is divided into 2 panes. When I'm reloading the config file, I got 2 extra windows from the other session, and both have been added to the preexisted ones. Moreover, the previous windows received one extra pane. The two extra panes are clear, there's no executed commands (htop) in any of them.

Is there a way to reload the config file in the way that this would be applied only to attached session? Or do I have to forget about reloading the config file when I'm using sessions, and in order to apply new setting I should use tmux kill-server and start sessions anew?

5

Build a wrapper

I think your needs are best served by some form of wrapper script to setup custom sessions. Something like the answer to this one.

It would look something like this, but you should change it for your specific needs.

#!/bin/bash

# test if the session has windows
is_closed(){ 
    sess=$1
    n=$(tmux ls 2> /dev/null | grep "^$sess" | wc -l)
    [[ $n -eq 0 ]]
}

# either create it or attach to it
if is_closed logi ; then
  tmux new -d -s logi -n cmd
  tmux neww -t logi -n logi "cat /dev/logi | ccze -m ansi -p syslog -C"
  tmux splitw -t logi:1 -v -p 50
  tmux selectw -t logi:2
  tmux selectp -t logi:1
fi
if is_closed standard ; then
  tmux new -d -s standard -n htop "htop"
  tmux neww -n cmd -t standard
  tmux splitw -t standard:2 -v -p 50
  tmux selectw -t standard:2 
  tmux selectp -t standard:1
fi

To reload a configuration file

If you make an edit to the configuration file while using tmux, you can run this is the prompt

tmux source-file /path/to/conf

Or, you can bind it to a key in .tmux.conf

bind r source-file ${HOME}/.tmux.conf \; display-message "source-file reloaded"

Home directory configurations

Finally, you really shouldn't be adding significant customizations to /etc/tmux.conf because this would be unhelpful to others if you need to use a shared system. Instead, I suggest you add any customization to ~/.tmux.conf because it's local and specific to your personal needs.

  • I got an error executing the script: [[: not found (7th line) – Mikhail Morfikov Dec 17 '13 at 23:54
  • 1
    @MikhailMorfikov It's might be due to sh being a different version. Try changing the top line to #!/bin/bash. – scicalculator Dec 18 '13 at 1:32
  • Yep, that works. – Mikhail Morfikov Dec 18 '13 at 2:45
0

I've create this script. It does not need tmuxinator, ruby or others. It is just a bash script, configurable.

I configure mi config file like:

combo=()
combo+=('logs' 'cd /var/log; clear; pwd')
combo+=('home' 'cd ~; clear; pwd')

I can configure all my projects. The rest is done by the script:

#!/bin/bash

if [ -r config ]; then
    echo ""
    echo "Loading custom file"
    . config
else
    . config.dist
fi

tmux start-server

window=0
windownumber=-1

for i in "${combo[@]}"; do

    if [ $((window%2)) == 0 ]; then
        name=${i}
        ((windownumber++))
    else
        command=${i}
    fi

    if [ ${combo[0]} == "${i}" ]; then
        tmux new-session -d -s StarTmux -n "${name}"
    else
        if [ $((window%2)) == 0 ]; then
            tmux new-window -tStarTmux:$windownumber -n "${name}"
        fi
    fi

    if [ $((window%2)) == 1 ]; then
        tmux send-keys -tStarTmux:$windownumber "${command}" C-m
    fi

    ((window++))
done

tmux select-window -tStarTmux:0
tmux attach-session -d -tStarTmux
0

You don't have to use a wrapper script, you can do it with the source-file command.

I split my .tmux.conf in two pieces, and it only source those:

source-file ~/.config/tmux/options.conf
source-file ~/.config/tmux/session.conf

Then, session.conf contains the pane definitions:

new -s logi -n cmd
neww -n logi "cat /dev/logi | ccze -m ansi -p syslog -C"
splitw -t 1 -v -p 50
selectw -t 2
selectp -t 0

new -s standard -n htop "htop"
neww -n cmd
splitw -t 2 -v -p 50
selectw -t 2 
selectp -t 1

And options.conf only contains the option definitions:

bind R source-file ~/.config/tmux/options.conf \; display-message "Config reloaded..."
set -g base-index 1

This way, the bind R can only source the options.conf and everything will be reloaded, but no new panes will be created.
One small drawback is that if you want to change window layout, you need to quit and start a new session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.