20220331-this_ssux.md (3705B)
1 Two of the commands I use the most on a daily basis are [tmux](https://github.com/tmux/tmux/wiki) and ssh. 2 3 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. 4 5 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. 6 7 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. 8 9 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 10 ``` 11 ssux () { 12 TERM=screen ssh -t "$@" 'tmux attach || tmux new' || ssh "$@" 13 } 14 ``` 15 16 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 17 18 Running it is as simple as replacing ssh 19 ``` 20 ssux hostname 21 ``` 22 23 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. 24 25 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"}. 26 27 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, I find it to be really convenient. 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. 28 29 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. 30 31 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"} 32 ``` 33 # see: toggle on/off all keybindings · Issue #237 · tmux/tmux - https://github.com/tmux/tmux/issues/237 34 # Also, change some visual styles when window keys are off 35 bind-key -T root F12 \ 36 set-option -g prefix None \;\ 37 setw -g window-status-current-format "#[bg=colour5,fg=colour0] #I #[bg=colour5,fg=colour0]#W #[bg=colour0,fg=colour5]▓░" \;\ 38 refresh-client -S 39 40 bind-key -T root F11 \ 41 set-option -g prefix ` \;\ 42 bind ` send-prefix \;\ 43 setw -g window-status-current-format "#[bg=colour14,fg=colour0] #I #[bg=colour14,fg=colour0]#W #[bg=colour0,fg=colour14]▓░" \;\ 44 refresh-client -S 45 ``` 46 47 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. 48 49 <iframe src="/img/ssux.webm" alt="ssux demo" width="540" height="315" frameborder=0 />