From 5ca22a2e982011d1d33ac40f81b5ee6f78836dad Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Sat, 12 Dec 2020 13:08:49 -0500 Subject: fzf aliases, functions, bindings --- lib/common.nix | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/lib/common.nix b/lib/common.nix index 0800e52..0975cfd 100644 --- a/lib/common.nix +++ b/lib/common.nix @@ -19,6 +19,12 @@ in LEDGER_FILE = "${homedir}/.hledger.journal"; XTERM_LOCALE = locale; PYTHONSTARTUP = "${homedir}/.pythonrc"; + + FD_OPTIONS = "--follow --exclude .git"; + BAT_PAGER = "less -R"; + FZF_DEFAULT_COMMAND = "git ls-files --cached --others --exclude-standard | fd --type f --type l $FD_OPTIONS"; + FZF_CTRL_T_COMMAND = "fd $FD_OPTIONS"; + FZF_ALT_C_COMMAND = "fd --type d $FD_OPTIONS"; }; # From the command line: @@ -193,6 +199,63 @@ in } # my prompt, after much chagrin export PS1="\n\[\e[34m\][\u@\h:\w]\[\e[m\] \$(date +%Y.%m.%d..%H.%M) \$(if [ \$? == 0 ]; then true; else echo \(\$?\); fi) \n$ " + + + # https://gist.github.com/junegunn/8b572b8d4b5eddd8b85e5f4d40f17236 + is_in_git_repo() { + git rev-parse HEAD > /dev/null 2>&1 + } + + fzf-down() { + fzf --height 50% "$@" --border + } + + _gf() { + is_in_git_repo || return + git -c color.status=always status --short | + fzf-down -m --ansi --nth 2..,.. \ + --preview '(git diff --color=always -- {-1} | sed 1,4d; cat {-1}) | head -500' | + cut -c4- | sed 's/.* -> //' + } + + _gb() { + is_in_git_repo || return + git branch -a --color=always | grep -v '/HEAD\s' | sort | + fzf-down --ansi --multi --tac --preview-window right:70% \ + --preview 'git log --oneline --graph --date=short --color=always --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1) | head -'$LINES | + sed 's/^..//' | cut -d' ' -f1 | + sed 's#^remotes/##' + } + + _gt() { + is_in_git_repo || return + git tag --sort -version:refname | + fzf-down --multi --preview-window right:70% \ + --preview 'git show --color=always {} | head -'$LINES + } + + _gh() { + is_in_git_repo || return + git log --date=short --format="%C(green)%C(bold)%cd %C(auto)%h%d %s (%an)" --graph --color=always | + fzf-down --ansi --no-sort --reverse --multi --bind 'ctrl-s:toggle-sort' \ + --header 'Press CTRL-S to toggle sort' \ + --preview 'grep -o "[a-f0-9]\{7,\}" <<< {} | xargs git show --color=always | head -'$LINES | + grep -o "[a-f0-9]\{7,\}" + } + + _gr() { + is_in_git_repo || return + git remote -v | awk '{print $1 "\t" $2}' | uniq | + fzf-down --tac \ + --preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" {1} | head -200' | + cut -d$'\t' -f1 + } + bind '"\er": redraw-current-line' + bind '"\C-g\C-f": "$(_gf)\e\C-e\er"' + bind '"\C-g\C-b": "$(_gb)\e\C-e\er"' + bind '"\C-g\C-t": "$(_gt)\e\C-e\er"' + bind '"\C-g\C-h": "$(_gh)\e\C-e\er"' + bind '"\C-g\C-r": "$(_gr)\e\C-e\er"' ''; shellAliases = { add = "git add --ignore-removal"; @@ -216,13 +279,20 @@ in hs = "home-manager switch"; ll = "ls -lah"; rm = "rm -i"; + s = '' + INITIAL_QUERY="" && \ + RG_PREFIX="rg --column --line-number --no-heading --color=always --smart-case " && \ + FZF_DEFAULT_COMMAND="$RG_PREFIX '$INITIAL_QUERY'" && \ + fzf --bind "change:reload:$RG_PREFIX {q} || true" \ + --ansi --phony --query "$INITIAL_QUERY" \ + --height=50% --layout=reverse + ''; showpath = "echo $PATH | sed 's/:/\n/g'"; tdate = "date +%Y.%m.%d..%H.%M"; ttime = "date +%H.%M"; typeless = "history | tail -n 20000 | sed 's/.* //' | sort | uniq -c | sort -g | tail -n 100"; + v = "vim $(fzf)"; "v." = "vim ."; - vk = "vim $(sk -m)"; - vs = "vim $(sk -m)"; }; }; -- cgit v1.2.3