function __kitty_ssh_host -d "Extract the target host token from ssh/mosh-style argv"
    # Skip short options that consume a following argument (ssh + mosh-ish).
    set -l argtaking b c D E e F I i J L l m O o p Q R S W w P
    set -l host ''
    set -l skip 0
    for tok in $argv
        if test $skip -eq 1
            set skip 0
            continue
        end
        if string match -q -- '-*' $tok
            set -l opt (string sub -s 2 -l 1 -- $tok)
            if test (string length -- $tok) -eq 2; and contains -- $opt $argtaking
                set skip 1
            end
            continue
        end
        set host $tok
        break
    end
    set host (string replace -r '.*@' '' -- $host)   # strip user@
    set host (string replace -r ':.*' '' -- $host)   # strip :port (scp-style)
    echo $host
end
