Skip to content

Instantly share code, notes, and snippets.

@dohq
Last active March 21, 2024 14:44
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
@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

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