Skip to content

Instantly share code, notes, and snippets.

@DovieW
Last active December 4, 2024 18:24
Show Gist options
  • Save DovieW/492402c93a6020958039da80d8c81f9b to your computer and use it in GitHub Desktop.
Save DovieW/492402c93a6020958039da80d8c81f9b to your computer and use it in GitHub Desktop.
My FZF Stuff

git-switcher

#!/bin/bash

branches=$(git branch --all | grep -v HEAD) &&
branch=$(echo "$branches" |
    fzf -d $(( 2 + $(wc -l <<< "$branches") )) +m) &&
git checkout $(echo "$branch" | sed "s/.* //" | sed "s#remotes/[^/]*/##")

list ssh hosts from ssh config

function ssh() {
  if [[ $# -eq 0 ]]; then
    local host
    host=$(\grep '^Host ' ~/.ssh/config | awk '{print $2}' | fzf --prompt='Select SSH host: ')
    if [[ -n "$host" ]]; then
      command ssh "$host"
    fi
  else
    command ssh "$@"
  fi
}

switch dir or vim

function search() {
  local selection=$(fd --hidden --type f --type d | fzf --preview '([[ -d {} ]] && echo "📁" || bat -n --color=always {} 2> /dev/null)' --preview-window=right:40%)

  if [ -z "$selection" ]; then
    return
  elif [ -f "$selection" ]; then
    vim selection"
  elif [ -d "$selection" ]; then
    cd "$selection"
  else
    echo "Invalid selection"
  fi
}

um why'd i make this one, it seems similar as previous?

function vfd() {
    local files="$(fd --fixed-strings "${1}")"
    local amount=$(echo "${files}" | wc -l)

    if [[ -z "${files}" ]]; then
        echo "No matches"
        return 1
    elif [[ "${amount}" -gt 1 ]]; then
        local select
        select="$(echo "${files}" | grep --invert-match '\.sw[op]' | fzf)"
        if [[ -n "${select}" ]]; then
            [[ -f ~/.zsh_history ]] && echo "vim \"${select}\"" >> ~/.zsh_history
            [[ -f ~/.histfile ]] && echo "vim \"${select}\"" >> ~/.histfile
            [[ -f ~/.bash_history ]] && echo "vim \"${select}\"" >> ~/.bash_history
            vim "${select}"
	    cd "$(dirname ${select})"
            return 0
        else
            return 1
        fi
    fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment