Display welcome message in new tmux session - bash

In a bash script, a new tmux session is started, several processes are launched in separate panes, then the session is attached to the current terminal. I would like to display a short message that gives a few helpful hints how to navigate panes, exit the session etc.
I tried to just echo, but that is not executed inside the session. Is there a good approach how to do this?
Edit: my use case is to have everything contained in a repository. I want to avoid adding config to dotfiles or anything in the user's home dir.

There are a few ways you could print your message perhaps:
Make tmux enter copy mode with some output, for example: tmux new \; run 'echo hello'
Run a script in a new window that prints the output: tmux neww 'echo hello; sleep 5'
Make one of your panes (if your tmux is new enough) open a popup: tmux new 'tmux popup -KER "echo hello; sleep 5"; exec $SHELL"

You can check to see if there are tmux specific environment variables set in your .bashrc and print a message if they exist
eg. in your .bashrc
if [ ! -z "$TMUX" ]; then
echo "Your message"
fi

Related

tmux: attach to existing session in new grouped session

I want to start tmux when opening a new terminal window via shortcut (ALT+F1).
tmux should start a new session when there is no session that I can attach to.
If there is an existing session, but I am not attached to it, simply attach.
If there is an existing session and I am already attached to it (i.e., I am opening a 2nd terminal window), I want to attach to the same session and be able to work on another window.
Currently, I am using the following script called tmux-init for the first two points:
#!/bin/sh
# Attach TMUX, or else open new session
if [[ -x "$(command -v tmux)" ]] && [[ -n "${DISPLAY}" ]] && [[ -z "${TMUX}" ]]; then
tmux attach || tmux >/dev/null 2>&1
fi
This automatically attaches to my existing session, or creates a new one when it does not exist.
However, I want to be able to open multiple windows and work on all of them at the same time.
Currently, all of the opened windows would be attached to the same session and mirror everything. The idea was to use tmux groups (tmux new-session -t 0), but my knowledge of bash scripting and tmux is limited. Here is what I currently have (tmux-init-2):
#!/bin/sh
tmuxtest=$(tmux ls | grep attached | grep "group 0")
if [[ ! -z "$tmuxtest" ]]
then
tmux new-session -t 0
tmux set-option destroy-unattached on
tmux new-window
else
tmux-init
fi
This works sometimes, sometimes it does not open a new window, sometimes it does not destroy the grouped session on detach... I can't seem to find a pattern. In most cases, the new window opens after I detach from the session. It seems that I should pass the options to tmux in a different way - but I have not found a way to create a new session and set options for that session immediately. Is there a better way to do it?

Send echo command to an external xTerm

I have a bash script, and I want to be able to keep a log in an xterm, and be able to send echo to it anytime.
How would I do this?
Check the GPG_TTY variable in your xterm session. It should have the value similar to
GPG_TTY=/dev/pts/2
This method should be available for terminals that support GNU Pinentry.
Another option to determine the current terminal name is to use
readlink /proc/self/fd/0
The last method applies only to Linux
Now if your bash script implements a command
echo "Hello, world!" > /dev/pts/2
This line should appear on the xterm screen.
I managed to make a console by running an xterm with a while loop clearing the screen, reading the contents of the log file, pauseing for a second, then looping again. Here was the command:
xterm -T Console -e "while true: do cls && cat ${0}-LOG.txt && sleep 1; done"
Then to send something to the console:
echo -e "\e[91;1mTest" >> ${0}-LOG.txt
And the console will update each second.

bash script - how to execute command after starting new tmux session

For example: start new tmux session, launch webserver (sends log into console), and launch second tmux window for editor.
I had trying do this by different ways - but nothing works.
Take a look at tmuxify (disclaimer: I wrote it).
It's to automate the launch of tmux sessions by defining a layout file with windows, panes and commands.
For commands executions, take a look at this answer.
But you can also predefining layouts of tmux sessions, try tmuxinator/tmuxinator
.
You can run shell commands from ~/.tmux.conf:
if-shell shell-command export I_AM_USING_TMUX=1
But it often does not seem to work as expected...
The environment variable TMUX is set when you run tmux. Then you can conditionally run shell commands from ~/.bash_profile:
if [[ ! -z "$TMUX" ]]; then
# run shell commands
fi
Or, you can export your own environment variable from ~/.tmux.conf file (as I already exported one from the beginning) and use it for if condition from ~/.bash_profile again:
if [[ ! -z "$GHCHOI_IS_USING_TMUX" ]]; then
# run shell commands
fi

How to launch tmux –automatically– when konsole/yakuake start?

I discovered tmux possibility recently and I'm using it inside yakuake/konsole (quake-like terminal). However, I have to launch tmux manually every time I start my laptop or restart yakuake.
How to launch tmux –automatically– when yakuake/konsole start ?
A friend advice to use <terminal_emulator> -e tmux.
Konsole
It works with konsole.
I modified the property in the menu to:
konsole -e tmux
Yakuake
However it doesn't work with yakuake.
Based on the Start tmux on every shell login article from the Archlinux wiki, you can start tmux on your shell with the following code at the
Zsh or Bash
Add in your zsh or bash configuration (usually ~/.zshrc or ~/.bashrc) the following code and restart your session:
function start_tmux() {
if type tmux &> /dev/null; then
#if not inside a tmux session, and if no session is started, start a new session
if [[ $HOST == "laptop" && -z "$TMUX" && -z $TERMINAL_CONTEXT ]]; then
(tmux -2 attach || tmux -2 new-session)
fi
fi
}
start_tmux
Fish
Add in your fish configuration (usually ~/.config/fish/config.fish) the following code and restart your session:
function start_tmux
if type tmux > /dev/null
#if not inside a tmux session, and if no session is started, start a new session
if test -z "$TMUX" ; and test -z $TERMINAL_CONTEXT
tmux -2 attach; or tmux -2 new-session
end
end
end
start_tmux
I solved this by creating a Konsole/Yakuake profile (they're one and the same) + made it the default, in which I set up the Command to:
/usr/bin/sh -ilc "tmux attach || tmux new"
Manage profiles + where the profile is located, in case Yakuake/Konsole doesn't start up anymore:
When yakuake is running:
qdbus org.kde.yakuake /yakuake/sessions runCommandInTerminal 0 "tmux"
I haven't tried with Yakuake but I have a one liner shell scripting approach to make it work with Konsole Terminal Emulator.
Konsole emulator set KONSOLE_<something> environment variable on start.
Knowing this fact, we can add this to .zshrc file
[ -z "$KONSOLE_VERSION" ] || tmux
And this will start all KONSOLE windows attached to active tmux session or create one if its the first window.

How to set iterm2 tab title to that of a running tmux session name?

i know tmux display-message -p '#S' will display the current tmux session name, but i donno how to set the current tmux session name to the iterm2 tab title?
This would really help me to distinguish the various tmux sessions that i am running concurrently and jump to the correct tab rightaway.
add these to your ~/.tmux.conf:
set-option -g set-titles on
set-option -g set-titles-string "#{session_name} - #{host}"
My workflow is usually centered around panes, and I don't use tmux, so I used a slight variation of #mislav answer:
set_terminal_tab_title() {
print -Pn "\e]1;$TABTITLE:q\a"
}
precmd_functions=($precmd_functions set_terminal_tab_title)
I threw that into my zshrc; then, in each pane, I export TABTITLE='FOO'. That way, when I switch panes, I get the title I want on the tab.
Stick this in your ~/.zshrc:
set_terminal_tab_title() {
print -Pn "\e]1;$1:q\a"
}
indicate_tmux_session_in_terminal() {
set_terminal_tab_title "$(tmux display-message -p '#S')"
}
precmd_functions=($precmd_functions indicate_tmux_session_in_terminal)
precmd_functions is an array that in zsh contains the list of functions to call prior to showing the prompt. If you add your own function to the list, it will get called whenever the prompt is shown, making it a good place to periodically update the terminal tab title.
Bash Version to display Hello World as a title:
echo -ne "\033]0; Hello World \007"
And if you want title refreshed each time bash print your prompt:
export PROMPT_COMMAND='echo -ne "\033]0;${USER}#${HOSTNAME%%.*}: ${PWD/#$HOME/~}\007"'
Found it on http://hints.macworld.com/article.php?story=20031015173932306

Resources