commit b9fd087199be5ff818506b12d63ea2e434fd13b9
parent 855fe2f54f04fd40996a5bdcffc501157026babd
Author: pyratebeard <root@pyratebeard.net>
Date: Thu, 31 Mar 2022 20:35:33 +0100
this_ssux
Diffstat:
2 files changed, 49 insertions(+), 0 deletions(-)
diff --git a/entry/this_ssux.md b/entry/this_ssux.md
@@ -0,0 +1,49 @@
+Two of the commands I use the most on a daily basis are `tmux` and `ssh`.
+
+If you haven't heard of `tmux` it is a terminal multiplexer similar to `screen`, but with loads of extra functionality. My normal workflow sees me mostly in one or more `tmux` sessions.
+
+As a sysadmin I rely heavily on `ssh`. Even with moving a lot of my work to Infrastructure-as-Code with tools like Ansible, there is still many times throughout a given week when I have to `ssh`onto a system.
+
+A long time I was in the habit of starting a `screen` when I logged onto a system so that if my connection dropped I didn't lose what I was doing, as well as being able to open new shells within the same session.
+
+Then I moved to installing `tmux` on my systems and started a `tmux` session when I logged on. After a while this became an function in my `zsh` [alias file](https://git.pyratebeard.net/dotfiles/file/zsh/.zsh/aliases.zsh.html#l181){target="_blank" rel="noreferrer"}, which I use in place of `ssh`
+```
+ssux () {
+ TERM=screen ssh -t "$@" 'tmux attach || tmux new' || ssh "$@"
+}
+```
+
+The function opens an `ssh` connection to the server specified then attempts to attach an existing `tmux` session. If this fails it will try to start a new session. If both of those commands fail, if `tmux` isn't installed for example, then it falls back to normal `ssh`
+
+Running it is as simple as replacing `ssh`
+```
+ssux hostname
+```
+
+I have seen a couple of different methods of doing something similar using settings in ~/.ssh/config, but I found they didn't suit my needs.
+
+I was using this a lot in work and decided to add hostname autocompletion to make my life easier. You can see the completion script in my [dotfiles](https://git.pyratebeard.net/dotfiles/file/zsh/.zsh/completion/_ssux.html){target="_blank" rel="noreferrer"}.
+
+Living inside a `tmux` session then using `tmux` over `ssh` meant I had to use two different prefix options. I didn't like this. I use the backtick as my prefix in `tmux` because I am lazy. I wanted to use the same backtick prefix on my remote sessions. This worked if I hit backtick twice before the command I wanted to send to the remote, or "second" session.
+
+With a bit of internet searching I found a discussion on the [tmux Github page](https://github.com/tmux/tmux/issues/237){target="_blank" rel="noreferrer"} regarding a toggle option. I also came across [bnorick's tmux.conf](https://github.com/bnorick/tmux-config/blob/master/tmux/tmux.conf#L368){target="_blank" rel="noreferrer"} with a toggle for nested sessions.
+
+I have implemented the same thing in [my tmux.conf](https://git.pyratebeard.net/dotfiles/file/tmux/.tmux.conf.html#l168){target="_blank" rel="noreferrer"}
+```
+# see: toggle on/off all keybindings · Issue #237 · tmux/tmux - https://github.com/tmux/tmux/issues/237
+# Also, change some visual styles when window keys are off
+bind-key -T root F12 \
+ set-option -g prefix None \;\
+ setw -g window-status-current-format "#[bg=colour5,fg=colour0] #I #[bg=colour5,fg=colour0]#W #[bg=colour0,fg=colour5]▓░" \;\
+ refresh-client -S
+
+bind-key -T root F11 \
+ set-option -g prefix ` \;\
+ bind ` send-prefix \;\
+ setw -g window-status-current-format "#[bg=colour14,fg=colour0] #I #[bg=colour14,fg=colour0]#W #[bg=colour0,fg=colour14]▓░" \;\
+ refresh-client -S
+```
+
+When I hit F12 the prefix in my local session is set to None, then I change the colour of the active window tab in the status bar so I remember it is disabled. I can now use the backtick prefix freely in my remote session. To enable the local session again I hit F11.
+
+<img src="/img/ssux.mp4" class="fitwidth" alt="ssux demo" />
diff --git a/img/ssux.mp4 b/img/ssux.mp4
Binary files differ.