Skip to content

Instantly share code, notes, and snippets.

@dohq
Last active May 6, 2025 18:27
Show Gist options
  • Save dohq/1dc702cc0b46eb62884515ea52330d60 to your computer and use it in GitHub Desktop.
Save dohq/1dc702cc0b46eb62884515ea52330d60 to your computer and use it in GitHub Desktop.
fzf-ssh
function fzf-ssh () {
local selected_host=$(grep "Host " ~/.ssh/ssh_config | grep -v '*' | cut -b 6- | fzf --query "$LBUFFER" --prompt="SSH Remote > ")
if [ -n "$selected_host" ]; then
BUFFER="ssh ${selected_host}"
zle accept-line
fi
zle reset-prompt
}
zle -N fzf-ssh
bindkey '^s' fzf-ssh
@ciscohack
Copy link

not working in my zsh shell . throwing following error fzf-ssh:zle:7: widgets can only be called when ZLE is active

any fix for this?

@dohq
Copy link
Author

dohq commented May 14, 2021

not working in my zsh shell . throwing following error fzf-ssh:zle:7: widgets can only be called when ZLE is active

any fix for this?

This snippet is supposed to be called from a keyboard shortcut.
If you want to run the fzf-ssh command, please use execute-named-cmd.
Also, if the screen appears to freeze with Ctrl-s, you can set the
setopt no_flow_control
in your .zshrc file.

@ciscohack
Copy link

Thanks for the comment. I tried calling the function with alias it's not working and i am not getting my ssh session showup. shortcut ctrl + S keyword combination is failing saying ZLE is not active

@dohq
Copy link
Author

dohq commented May 14, 2021

I tried calling the function with alias it's not working

alias is alias foo='fzf-ssh' settings?
fzf-ssh cannot be run even via an alias.
(This is no different than running fzf-ssh directly.)

Minimum .zshrc

setopt no_flow_control
function fzf-ssh () {
  local selected_host=$(grep "Host " ~/.ssh/ssh_config | grep -v '*' | cut -b 6- | fzf --query "$LBUFFER" --prompt="SSH Remote > ")

  if [ -n "$selected_host" ]; then
    BUFFER="ssh ${selected_host}"
    zle accept-line
  fi
  zle reset-prompt
}

zle -N fzf-ssh
bindkey '^s' fzf-ssh

After placing this in /tmp/.zshrc, try executing ZDOTDIR=/tmp/ zsh.
You should be able to ssh with Ctrl-s.
(Modify ~/.ssh/ssh_config as you see fit.)

@ciscohack
Copy link

I didn't understand last statement place in /tmp/.zshrc then try executing ZDOTDIR=/tmp/ zsh. my .zshrc is in my home directory .. why do i put in tmp directory that i didn't get.

BTW thanks for the help

@RafaelMoreira1180778
Copy link

RafaelMoreira1180778 commented May 2, 2022

Working like a charm, thank you very much!

@nicovs
Copy link

nicovs commented Mar 21, 2024

i created a slightly updated version of your gist:

function fzf-ssh () {
  local selected_host=$(grep "Host " ~/.ssh/config | \grep -v '*' | cut -b 6- | awk '{print $1}' | fzf +s --query "$LBUFFER" --prompt="SSH Remote > ")

  if [ -n "$selected_host" ]; then
    BUFFER="ssh ${selected_host}"
    zle accept-line
  fi
  zle reset-prompt
}

zle -N fzf-ssh
bindkey '^s' fzf-ssh

=> where i get the 1st string after the Host because you could have something like this in you config:
Host Server1-prod Production
and this would make your ssh session fail with: Production no such command

=> also added +s in the fzf command to sort the Fuzzy Finder window

@ashebanow
Copy link

ashebanow commented May 6, 2025

I also tweaked the grep query a bit, since it was producing some odd results on my .ssh/config. Here's the before and after on the grep.

Here's the BEFORE:

github.com github
unifi unifi.lan
setup.unifi.com
storage
calamansi
reverse-proxy
t 192.168.86.68

Note that lines 1, 2, and7 are incorrect, and the '*' entry is missing (which is probably a good thing). Line 7 actually comes from a commented out section in my config:

# Host 192.168.86.68
#   Ciphers 3des-cbc
#   KexAlgorithms +diffie-hellman-group1-sha1

Now here's the AFTER:

*
github.com
unifi
setup.unifi.com
storage
calamansi
reverse-proxy

Here's my modified version of your zsh function:

setopt no_flow_control

function fzf-ssh () {
  local selected_host=$(grep "^Host " ~/.ssh/config | awk '{print $2}' | fzf --no-sort --query "$LBUFFER" --prompt="SSH Remote > ")

  if [ -n "$selected_host" ]; then
    BUFFER="ssh ${selected_host}"
    zle accept-line
  fi
  zle reset-prompt
}

zle -N fzf-ssh
bindkey '^s' fzf-ssh

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