gnunn1 / tilix

A tiling terminal emulator for Linux using GTK+ 3
https://gnunn1.github.io/tilix-web
Mozilla Public License 2.0
5.38k stars 293 forks source link

tmux-inspired send-keys functionality for more sophisticated creation of sessions via scripts #2110

Open Hiradur opened 2 years ago

Hiradur commented 2 years ago

On some servers, I use shell scripts similar to the example below to quickly recreate a familiar workspace in tmux for myself after reboots and to provide customized tmux sessions for other users on these servers.

#!/bin/sh

SESSION_NAME=sess

if ! tmux attach-session -t "${SESSION_NAME}"
then
    tmux new-session -s "${SESSION_NAME}" \; \
    send-keys 'sudo -i' \; \
    split-window -v \; \
    send-keys 'sudo -i' \; \
    select-pane -t 0 \; \
    new-window  \; \
    rename-window 'dbg_glr' \; \
    send-keys 'sudo -i -u gitlab-runner' \; \
    split-window -v \; \
    send-keys 'sudo -i -u gitlab-runner' \; \
    select-pane -t 0 \; \
    new-window  \; \
    rename-window 'status' \; \
    send-keys 'SYSTEMD_COLORS=1 watch -c -n 10 systemctl status unit*' C-m \; \
    new-window  \; \
    rename-window 'jctl' \; \
    send-keys 'journalctl -f -n 30 -u unit1 -u unit2 -u unit3' C-m \; \
    new-window  \; \
    rename-window 'log_xx' \; \
    send-keys 'multitail -f /var/log/dir/log1.log /var/log/dir/log2.log' C-m \; \
    new-window \; \
    rename-window 'mariadb' \; \
    send-keys 'mysql database -u "$USER" -p' \; \
    new-window  \; \
    rename-window 'htop' \; \
    send-keys 'htop' \; \
    new-window  \; \
    rename-window 'logs' \; \
    send-keys 'cd /var/log; ls' C-m \; \

fi

I would like to see similar functionality in Tilix for similar reasons. I'm aware that session layouts can be saved and that it is possible to execute tilix-specific commands and execute child processes in tilix and in saved sessions. However, unless I overlooked something, these solutions are not as flexible or powerful as the solution provided by tmux. tmux provides the send-keys argument to send arbitrary sequences of key presses to the active terminal session.

One particularly useful featue of tmux' send-keys is that command lines can be pre-written without executing them, e.g.:

# execute command by sending the return key at the end (C-m)
send-keys 'mysql database -u root -p' C-m \; \
# do not execute command by not sending the return key
send-keys 'mysql database -u root -p' \; \

This pre-writing can be useful for commands that should only be executed on demand, e.g. because they require interactive input or launch resource-hungry processes. Also, commands launched in tmux using send-keys do not make the terminal session/pane disappear after they terminated, which is useful to keep the layout the same at all times.