I am new to tmux, and find this really strange behaviour when I tried it.
I write a really simple script
tmux new-session -s "test" -d
tmux send-keys -t test hello Enter
tmux attach -t "test"
when I run it, the shell shows the following
hello
eric:bin$ hello
I only expect the "hello" command shows once inside the prompt, but the "hello" command will show twice: one outside the prompt, one inside the prompt
Does anyone know the reason?
I don't think this is a tmux problem. What happens is that tmux is sending the keys to the window before the shell finishes loading.
You can see the phenomenon by doing the following:
$ sleep 3
hello
$ hello
bash: hello: command not found
While the sleep 3 is running you can type "hello" + Enter and only when that program finishes running, bash interprets the input.
You can work around this by making your shell load faster.
You can prevent the text showing up twice by inserting sleep 1 between the new-session and send-keys lines. This way the script waits a second for the shell to finish starting up.
This of course makes the whole script take longer. It was a solution for me though because I found it annoying to see the text twice at startup.
Related
I am using alacritty and tmux together. Is it possible to make tmux run everytime when I launch alacritty?
Yes. In the shell option, you can set the program you want to run and even pass in the arguments. For example, in your alacritty.yml file, you can set this:
shell:
program: /usr/local/bin/tmux # <- set this to the path of your tmux installation
args:
- new-session
- -A
- -D
- -s
- main
Every time you start alacritty, tmux will start a new session and pass the other args.
Or go the easy route and launch your terminal this way:
alacritty -e tmux
i'm using this script:
https://cedaei.com/posts/ideas-from-my-dev-setup-always-tmux/
had it from https://lobste.rs/s/n5blid/ideas_from_my_development_setup_always
and use it in alacritty as:
shell:
program: /home/user/.local/bin/tmux_chooser.sh
I have a situation where in my script i keep appending some text to the original session name
EG: First time when i create a new session
today=`date +%Y-%m-%d-%H_%M_%S_%N`
tmux new-session -d -s "$today" zsh /home/path/to/script.sh "with_params"
IN my script.sh based on some condition i want add some text infront of the session name:
session_name=`tmux display-message -p "#S"` #this gets the session name in which the script is running
tmux rename-session -t ${session_name} ABC_${session_name}
....after some code again i rename it
session_name=`tmux display-message -p "#S"` #this gets the session name in which the script is running
tmux rename-session -t ${session_name} XYZ_${session_name}
So whats happening here is
my original session name is "2020-04-10-11_52_01_953906687"
Its gets renamed to XYZ_2020-04-10-11_52_01_953906687 or ABC_2020-04-10-11_52_01_953906687 or KLM_2020-04-10-11_52_01_953906687 etc based on certain conditions.
Now i want to capture the last five lines output of this session. I Know it has only one window and only one pane
$ tmux capture-pane -p -S- -E- -e -t *2020-04-10-11_52_01_953906687* |sed '/^$/d'|tail -5
zsh: no matches found: *2020-04-10-11_52_01_953906687*
So how can i do this.
You need to escape the *s if you want them to be processed by tmux rather than the shell. Either \* or put the whole thing in 's.
I'd like to setup my tmux session from a shell script.
essentially I want to automate something like:
Create a session, split the window vertically, create a new window, run command 1 in the first vertical split, command 2 in the second, and another command in the second window.
Is that possible? So far I am trying to send keys but I'm not sure if there is a better way.
tmux new-session -s foo -d
tmux new-window -t foo
tmux send -t foo.0 "echo 1" ENTER
tmux send -t foo.1 "echo 2" ENTER
tmux attach -t foo
If you want to be able to later kill the program you are running and be left with a shell, then send-keys is the best bet.
If you don't, you can just start the program directly as part of new-session or new-window, for example:
tmux new-session -sfoo -d -- top
tmux new-window -tfoo -- emacs
tmux attach -tfoo
The remain-on-exit option is also useful for running programs like this.
I'm trying to detect the presence of iTerm2 and if I run the following in iTerm2 (echo -n $'\e[5n'; read -s -t 0.1 line; printf '%q\n' "$line") the terminal responds with $'\033'\[ITERM2\ 3.2.1n$'\033'\[0n
However, if I am running a tmux session in the terminal, then tmux responds and gives me nothing.
Any idea how I can ask tmux to query the physical terminal to report its status?
Footnotes
Here is a description of [5n in the tmux source: https://github.com/tmux/tmux/blob/486ce9b09855ae30a2bf5e576cb6f7ad37792699/tools/ansicode.txt#L577
This might be iTerm2 only, since I haven't seen a response on any other terminal
According to ft in freenode's #tmux (and as seen in this Super User answer), you can use:
'\ePtmux;\e" STUFF_FOR_THE_TERMINAL_HERE "\e\\'
So, it would be something like:
echo -n $'\ePtmux;\e\e[5n\e\\'
Use case
I run command over a bastion in SSH and since the connection can be dropped we are using tmux or screen.
For short commands I'm using send-keys, let's say I'm using a command like kubectl get pods. I'd like to keep the output of this command but terminate the tmux session.
Using remain-on-exit is not what I'm looking for as it keeps a dead window.
I've seen those question already
https://unix.stackexchange.com/questions/17116/prevent-pane-window-from-closing-when-command-completes-tmux
TMUX: how to make new window stay when start shell-command quits?
I'd like to keep the output of this command but terminate the tmux session.
I use this in my ~/.tmux.conf, and now when I exit my running shell, pane output is saved to unique log file:
set -g remain-on-exit
set-hook pane-died 'capture-pane -S - -E - ; save-buffer "$HOME/logs/tmux/tmux-saved.#{host_short}-#{session_id}:#{window_id}:#{pane_id}-#{pane_pid}-#{client_activity}.log"; delete-buffer; kill-pane'
I'm not sure it's exactly what you are looking for, but you could use the capture-pane command to save the contents of a pane (subject to what is still in the pane's history) to a file.