How to automatically name session in tmux?

4.4k views Asked by At

Provided I have a shell script to figure out name for an arbitrary Tmux session, how to configure Tmux to call it when creating new session (using both tmux new and :new-session) and use its output as a session name?

3

There are 3 answers

1
chepner On

You don't need any tmux-specific features for this on the command line, just plain vanilla command substitution in your shell. Assuming your script is called make-session-name:

tmux new-session -s "$(make-session-name)"

There may be a way to do something similar on the tmux command prompt, but I don't know it.

0
Madhav Prabhoo On

Building on the answer by chepner, if you are inside tmux then this works

new-session ; run-shell "tmux rename-session $(echo testsession)"

You can bind this command to a key, say N, for ease like below

bind N new-session -s "startname" \; run-shell "tmux rename-session $(echo testsession)>/dev/null" 

Replace 'echo testsession' with your command. During tests tmux did not allow the obvious 'run "tmux new -s $(echo testsession)"' and returns an error

0
dynabaul On

Here's a little script to create a tmux session, name the first window "home", create a 2nd window and name it "download", change it to the download directory and list it's contents, then open tmux attached to this session. This should show you what you're after.

#!/bin/bash
SESSION=$USER

tmux -2 new-session -d -s $SESSION

# Setup a window for home dir
tmux rename-window 'home'

# Setup a download window
tmux new-window -t $SESSION:1 -n 'download' 
tmux send-keys "cd /home/user/download; ls -lh" C-m

# Set default window
tmux select-window -t $SESSION:0


# Attach to session
tmux -2 attach-session -t $SESSION