Skip to content

Instantly share code, notes, and snippets.

@thomascharbonnel
Last active September 12, 2024 07:12
Show Gist options
  • Save thomascharbonnel/7fe3c1c26c6bad1d7758d1f5f6e18f76 to your computer and use it in GitHub Desktop.
Save thomascharbonnel/7fe3c1c26c6bad1d7758d1f5f6e18f76 to your computer and use it in GitHub Desktop.
Set tmux pane title to short hostname on ssh connections for Fish
# Copy that into your ~/.config/fish/config.fish
function ssh
set ps_res (ps -p (ps -p %self -o ppid= | xargs) -o comm=)
if [ "$ps_res" = "tmux" ]
tmux rename-window (echo $argv | cut -d . -f 1)
command ssh "$argv"
tmux set-window-option automatic-rename "on" 1>/dev/null
else
command ssh "$argv"
end
end
@Shtucer
Copy link

Shtucer commented Oct 21, 2020

replace

set ps_res (ps -p (ps -p %self -o ppid= | xargs) -o comm=)
if [ "$ps_res" = "tmux" ]

with
if test -n $TMUX
or
if set -q TMUX

@nippyin
Copy link

nippyin commented Jun 13, 2021

Hi Mate, I know you asked to copy this code into fish terminal, I am using kitty, can you help to make it work here? All I need when I ssh to remote server I want to see title or status bar with remote hostname and when exit it, it should return to my local computer name.

@melissaboiko
Copy link

On modern tmux line 8 seems to be rather:

    tmux set -w automatic-rename "on"

@melissaboiko
Copy link

@nippyin This code is not dependent on the terminal; it's for tmux (which you run inside the terminal), using the fish shell. If you're using tmux and fish inside kitty, you can use the code above.

If you don't use tmux, you can use kitty @ set-window-title and kitty @ set-tab-title to configure the kitty window. You'll have to add allow_remote_control to your kitty.conf file first.

If you use tmux but bash or other shell, adapt the function to your shell, or look online (people have made a number of versions).

@nippyin
Copy link

nippyin commented Aug 3, 2021

Thanks it works.

@darren-muon
Copy link

I added some of the recs from the comments and also made this support ssh arguments such as -X or -L.

function ssh
  ### automatically renames tmux pane with hostname when using SSH
  
  # check if tmux is running
  if test -n "$TMUX"
    # assume first arg not starting with '-' is hostname
    set hname -- $argv
    for a in (string split -- ' ' $argv)
      if not string match -- '-*' $a
        set hname -- $a
        break
      end
    end
    tmux rename-window $hname
    echo -- $argv
    command ssh $argv
    tmux set -w automatic-rename "on"
  # if not running tmux, just use ssh
  else
    command ssh $argv
  end
end

Note that this version assumes that the first arg not starting with a dash is the hostname, so if will be wrong if you do something like ssh -L 5901:192.168.1.42:5901 hostname. Instead, you have to do ssh hostname -L 5901.192.168.1.42:5901

@willifehler
Copy link

@darren-muon awesome, thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment