# tmux
## Config
For inspiration making your own `~/.tmux.conf`, take a look at <https://github.com/samoshkin/tmux-config>
| file | description |
| -------------------------- | ----------------------------------------------------- |
| `~/.tmux.conf` | symlink to `~/.tmux/tmux.conf` |
| `~/.tmux/tmux.conf` | main tmux conf file |
| `~/.tmux/tmux.remote.conf` | customizations when tmux is on a remote host over ssh |
### nested sessions
When using tmux as a local window manager with a remote ssh+tmux session, here are concrete things to make that setup more pleasant. This section assumes you have identical `~/.tmux.conf` on host and remote servers.
#### copy-n-paste
[samoshkin](https://github.com/samoshkin/tmux-config/blob/95efd543846a27cd2127496b74fd4f4da94f4a31/tmux/tmux.conf#L144-L194)'s config [handles](https://github.com/samoshkin/tmux-config/blob/master/tmux/yank.sh) visual selection copy and paste, but there are some things that need tweaked to make copy in the remote tmux reflect in the host's system clipboard.
In `~/.tmux/tmux.conf`
```
# OSC52 is an ANSI escape sequence for copying text
# from remote SSH sessions to the local system clipboard
set -g @copy_use_osc52_fallback on
set -g set-clipboard on
```
##### emacs
I use [doom](https://github.com/doomemacs/doomemacs). In `~/.doom.d/init.el`
```lisp
(doom!
;...
:os
(tty +osc) ; enables OSC52 via clipetty.el
```
##### vim
Adapted from [vim-tmux-yank](https://github.com/jabirali/vim-tmux-yank), just add this to your `~/.vimrc`
```
"" TMUX clipboard shenanigans
if !empty($TMUX)
" Function to yank to OSC-52.
function! TmuxYank()
let buffer=system('base64 -w0', @0)
let buffer=substitute(buffer, "\n
quot;, "", "")
let buffer='\e]52;c;'.buffer.'\x07'
silent exe "!echo -ne ".shellescape(buffer)." > ".system("tmux display -p '#{pane_tty}'")
endfunction
augroup TmuxYankAuto
autocmd!
autocmd TextYankPost * if v:event.operator ==# 'y' | call TmuxYank() | endif
augroup END
endif
```
#### toggling leader key between remote and local
Direct link to [tmux.conf](https://github.com/samoshkin/tmux-config/blob/95efd543846a27cd2127496b74fd4f4da94f4a31/tmux/tmux.conf#L358-L381) section that has `F12` toggle between sending leader to remote and local.
#### visually distinguishing the nested session
In `~/.tmux/tmux.conf`
```
if-shell 'test -n "$SSH_CLIENT"' \
'source-file ~/.tmux/tmux.remote.conf'
```
In `~/.tmux/tmux.remote.conf`
```
set -g status-position bottom
```
## Cookbook
#### Oh shoot! I started this process outside of tmux, but now I need to go away and it's been running a while, what can I do?
```bash
# [Ctrl-z] on the process
bg
echo $! # get the PID (1608)
disown
tmux
reptyr 1608
```
#### Oh shoot! I re-attached to this remote tmux session and now my ssh-agent forwarding is broken
To fix your session when re-attaching, make sure you are [refreshing appropriate SSH env vars](https://github.com/samoshkin/tmux-config/blob/95efd543846a27cd2127496b74fd4f4da94f4a31/tmux/tmux.conf#L314-L327).
To fix the pre-existing shells:
```shell
# put this in your ~/.favoriteshellrc as a function: fixssh
eval $(tmux show-env -s |grep '^SSH_')
```
![[MacOS#Touch ID with tmux]]