From aef761baaaaba5d459bde394d39ec226fa7770d3 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Mon, 10 Jan 2022 11:49:56 +0100 Subject: [PATCH] config/shell/browse: consolidate zoxide helpers --- .config/shell/browse | 32 +++++++++++++++++++++++++------- .local/bin/scripts/locz | 5 ----- .local/bin/scripts/zf | 4 ---- .local/bin/scripts/zfz | 7 ------- 4 files changed, 25 insertions(+), 23 deletions(-) delete mode 100755 .local/bin/scripts/locz delete mode 100755 .local/bin/scripts/zf delete mode 100755 .local/bin/scripts/zfz diff --git a/.config/shell/browse b/.config/shell/browse index f73c089..9a6eda8 100644 --- a/.config/shell/browse +++ b/.config/shell/browse @@ -1,4 +1,4 @@ -# Utilities regarding file system navigation +# Utilities for file system navigation # Useful when the current directory was recreated alias recd='cd $PWD' @@ -39,7 +39,7 @@ cd() { fi } -# LOCATE +# LOCATE {{{1 alias fselect='fzf -0 -1 --reverse --height=30% | while read f; do test -d "$f" && cd "$f" || b "$f"; done' loci() { locate --all --ignore-case --basename --existing "$@" | @@ -53,11 +53,28 @@ alias loce='noglob locei' # locate all alias loca='noglob sudo updatedb --prunenames "" --prunefs "" --prunepaths "/var/cache" -o /var/lib/mlocate/all.db && loci --database /var/lib/mlocate/all.db' -# ZOXIDE +# ZOXIDE {{{1 alias c=z +# Listing for quick directory switcher based on zoxide and fzf +alias zoxide-list='noglob zoxide query -sl' +# fzf for zoxide +zoxide-fzf() { + # returns the selected path stripped of its score + fzf -0 -1 -n2.. --tiebreak=index \ + --preview-window=20% --preview="ls -a --color --human-readable --group-directories-first --file-type {2..}" \ + --height=80% --reverse | + sed 's|.* /|/|' +} +# Locate directories and add 0 zoxide score +locate-zox() { + locate --basename --ignore-case "$@" | + while read -r file; do test -d "$file" && echo "$file"; done | + sed 's/^/ 0 /' +} +# Switch directory heuristically using zoxide and locate d() { test "$1" != "-" -a ! -d "$1" -a $# -eq 1 || { cd "$@" && return } - local query="$(zf "$@")" + local query="$(zoxide-list "$@")" # First find out whether there is an obvious match # (score at least ten times above runner-up and score above 20) # If not select with fzf, using locate to find extra options @@ -65,10 +82,11 @@ d() { expr "$(echo "$query" | head -1 | cut -d' ' -f1)" \> 20 \& \ $(echo "$query" | sed 'N;s|/.* \([0-9]\)|> 10 * \1|;q' | sed 's| */.*||') >/dev/null #2>&1 then echo "$query" | head -1 - else test -n "$query" && echo "$query"; locz "$@" - fi | zfz)" + else test -n "$query" && echo "$query"; locate-zox "$@" + fi | zoxide-fzf)" } +# Switch directory interactively using zoxide and locate di() { - test "$1" = "-" || local dir=$({ zf "$@"; locz "$@" } | zfz) + test "$1" = "-" || local dir=$({ zoxide-list "$@"; locate-zox "$@" } | zoxide-fzf) cd "${dir:-$1}" } diff --git a/.local/bin/scripts/locz b/.local/bin/scripts/locz deleted file mode 100755 index f75a108..0000000 --- a/.local/bin/scripts/locz +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh -# Locate existing directories in zoxide format -locate --basename --ignore-case "$@" | - while read -r file; do test -d "$file" && echo "$file"; done | - sed 's/^/ 0 /' diff --git a/.local/bin/scripts/zf b/.local/bin/scripts/zf deleted file mode 100755 index 392084c..0000000 --- a/.local/bin/scripts/zf +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -# Listing for quick directory switcher based on zoxide and fzf -set -o noglob -zoxide query -sl "$@" diff --git a/.local/bin/scripts/zfz b/.local/bin/scripts/zfz deleted file mode 100755 index 024291c..0000000 --- a/.local/bin/scripts/zfz +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh -# fzf for zoxide -# returns the selected path stripped of its score -fzf -0 -1 -n2.. --tiebreak=index \ - --preview-window=20% --preview="ls -a --color --human-readable --group-directories-first --file-type {2..}" \ - --height=80% --reverse | - sed 's|.* /|/|'