GNU Screen and Tmux: tips and tricks

So I am a big fan of the multiplexers like GNU Screen and Tmux, but even using Screen for years now I still recognize that there are many things there I don't know (and I only use Tmux for a couple of months so its worse).

What configs and tips and tricks you use on those software (both or on each one, doesn't matter)?

I use this as my screen config (I think I adapted this from Ubuntu's default screenrc):

shell -$SHELL
startup_message off
defscrollback 1000
hardstatus alwayslastline '%{= wk}%?%-Lw%?%{r}(%{k}%n*%f%t%?(%u)%?%{r})%{k}%?%+Lw%?%?%= %m/%d %C%A'
activity ''
termcapinfo xterm* 'hs:ts=\\E]2;:fs=\\007:ds=\\E]2;screen\\007'

It's a simple config to keep the status bar showing with some colors, but I am sure that there are more things I can do with screen and Tmux and would love to know it.


Solution 1:

This may be a bit simple for this crowd, but I found it useful to add this to the end of .profile, so that when I log in remotely via ssh, I get to read the MOTD, then switch to screen.

if [ -n "$SSH_TTY" ]
then
        read -p "Press <ENTER> to continue." enterkey
        exec /usr/bin/screen -D -R
fi

Solution 2:

I have this in my .bashrc. If you're not running screen this function just calls ssh as usual when executed. If you are running inside screen (and you haven't changed the default $TERM) it first sets the window name to the remote host (together with any optional parameters). It's very handy to keep track of remote connections when you have many open, which is frequent in our line of work I think.

function ssh () {
if [ $TERM = "screen-bce" ]
    then
    screen -X title "$*" 
    /usr/bin/ssh $*
else
    /usr/bin/ssh $*
fi
}

I also use bind ^A windowlist -b for easy access to the window list (just tap ctrl-A twice).

Easy ssh-agent support for all windows:

setenv SSH_AUTH_SOCK $HOME/.screen-ssh-agent
screen 10 ssh-agent -a $SSH_AUTH_SOCK $SHELL

Then in any window you do ssh-add just once when you start a new session.

Finally, when attaching to a remote screen session from within another screen session, you can use screen -x -e ^Vv to set the control sequence to CTRL-V (for example). This avoids having to chain CTRL-As to control the remote session.