bash + fzf で sshの接続設定 ( ~/.ssh/config ) をパパーっとフィルタリングする

これ (手前味噌、宣伝) のbash版です。

# ~/.bashrc とかに追記

__fzf_ssh() {
  grep --with-filename "" ~/.ssh/conf.d/*.conf | \
    fzf --exact --no-sort --reverse --delimiter=":" --preview="cat {1}" --preview-window=down:50%
}

fzf_ssh_new() {
  local found
  found="$(__fzf_ssh)"
  if [[ $? -ne 0 ]] || [[ "$found" =~ "^[:blank:]*$"  ]];  then
    return 1
  fi

  # Show found conf file contents
  cat "$(echo "$found" | cut -d ':' -f 1 )"

  # And open new terminal window
  # NOTE: [mklement0/ttab](https://github.com/mklement0/ttab) is required
  nohup ttab -w "ssh '$(echo "$found" | cut -d ':' -f 1 | xargs perl -ne 'print for /Host\s+(\S+).*/;')'" > /dev/null 2>&1 &
}

fzf_ssh_inline() {
  local found
  found="$(__fzf_ssh)"
  if [[ $? -ne 0 ]] || [[ "$found" =~ "^[:blank:]*$"  ]];  then
    return 1
  fi

  local CMD="ssh $(echo "$found" | cut -d ':' -f 1 | xargs perl -ne 'print for /Host\s+(\S+).*/;')"

  if [ -n "$CMD" ] ; then
    # Replace the last entry with $CMD
    history -s $CMD

    if type osascript > /dev/null 2>&1 ; then
      # Send UP keystroke to console
      (osascript -e 'tell application "System Events" to keystroke (ASCII character 30)' &)
    fi

    # Uncomment below to execute it here directly
    # echo $CMD >&2
    # eval $CMD
  else
    # Remove the last entry
    history -d $((HISTCMD-1))
  fi
}

bind '"\C-t":"fzf_ssh_inline\n"'