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?
How to automatically name session in tmux?
4.5k views Asked by dsx At
3
There are 3 answers
0
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
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
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 calledmake-session-name
:There may be a way to do something similar on the
tmux
command prompt, but I don't know it.