config/shell: improve completion & some functions

This commit is contained in:
xeruf 2022-12-13 13:07:02 +01:00
parent d29e0dd14f
commit 3b01d60544
4 changed files with 40 additions and 21 deletions

View File

@ -36,7 +36,7 @@ cl() {
cd() {
if test -d "$DATA" && test "${1:--}" != "-" -a ! -d "$1" -a $# -eq 1
then
dir=$(fd --no-ignore --glob "$1*" "$DATA" $MNT --maxdepth 2 --type d --max-results 1 2>/dev/null)
dir=$(fd --no-ignore --glob "$1*" "$DATA" "$MNT" --maxdepth 2 --type d --max-results 1 2>/dev/null)
test -n "$dir" && cd "$dir"
else
builtin cd "$@" &&
@ -45,9 +45,10 @@ cd() {
# - column malaligns output with ANSI escape sequences (view with LESS="" less)
# column --fillrows --output-width $(expr $COLUMNS + 20)
# pr -4 -l1 -W $COLUMNS -S" "
# TODO test whether still needed with exa
command ls --sort=none --quote-name |
head -12 |
xargs ls --sort=time --format=vertical --directory --literal --classify --color=always --width $COLUMNS |
COLUMNS=$(tput cols) xargs $(which exa >/dev/null 2>&1 && echo "exa --list-dirs --icons --reverse" || echo 'ls --format=vertical --literal --classify --directory') --sort=time --color=always |
head -3
code=$? && test $code -eq 141 || return $code
fi

View File

@ -50,6 +50,7 @@ unalias rd 2>/dev/null
# System help {{{1
compdef help=man
compdef h=help
alias info='info --vi-keys'
sudos() {
@ -76,9 +77,9 @@ xtrace() {
set +x
}
# Shows source code for command, resolving nested aliases
# Shows source for given command, resolving nested aliases
wh() {
local res=$(which "$@") || return $?
local res=$(which "$@" 2>/dev/null || type "$@") || return $?
# only works in zsh, not bash
if expr "$res" : "${@:$#}: aliased to" >/dev/null && ! expr "$res" : ".*builtin" >/dev/null
then echo "$res" | bat --style=plain --language=sh &&
@ -90,6 +91,12 @@ wh() {
}
compdef wh=which
# Find where a function was defined
def() {
autoload +X "$@"
type "$@"
}
pathadd() {
local IFS=":"
local result="$@"
@ -134,6 +141,7 @@ edit-shell() {
# Edit an executable in the PATH
edit-bin() {
local toedit="$(resolvealias "$1")"
case "$toedit" in (*\ text*);; (*) toedit="$1";; esac
if f="$(which "$toedit" 2>/dev/null)" && test -f "$f"
then edit "$f"
else edit-shell -f "$toedit"
@ -358,7 +366,14 @@ alias grp='grpc --ignore-case'
# TODO remove some prefixes \([^ ]*/\)\?
grpr() { grp --color=always --recursive $(echo $DIRS_IGNORE | sed 's|-x |--exclude-dir |g') "$@" | less -FX; }
# Grep in shell config files
grsh() { grpr --no-ignore-case "$@" $HOME/.{ba,z}sh* $HOME/.local/bin $CONFIG_SHELLS $CONFIG_ZSH; }
grsh() {
local search=$1
shift
case $1 in (-*|"");; (*) local filter=$1; shift;; esac
grpr --no-ignore-case "$search" "$@" $(test -z "$filter" &&
echo $HOME/.local/bin $CONFIG_SHELLS $CONFIG_ZSH $HOME/.{ba,z}sh* /{etc,usr/share}/{ba,z}sh* /etc/profile* ||
compgen -G "$XDG_CONFIG_HOME/$filter" -G "/usr/share/$filter" -G "/etc/$filter*" -G "$HOME/.$filter*")
}
# Recover stray swap files from neovim
vrec() {
@ -455,7 +470,9 @@ killJava() {
# Files {{{1
alias l="ls -l --almost-all --human-readable --group-directories-first --file-type"
which exa >/dev/null &&
alias l='exa --icons' &&
alias ll='l --long --extended --git --all'
if which fd >/dev/null
then fn() { $(command -v fd || echo fdfind) --hidden --no-ignore-vcs --one-file-system "$@" | less -F; } # [F]ind [n]o ignore
@ -463,7 +480,7 @@ then fn() { $(command -v fd || echo fdfind) --hidden --no-ignore-vcs --one-file-
compdef ff=fd
compdef fn=fd
else
alias ff='find -not -path "*.sync*" -and -not \( -name daten -prune \)'
alias ff='find -not -path "*.sync*" -and -not \( -name daten -prune \) -iname'
alias f1='find -mindepth 1 -maxdepth 1'
fi
@ -539,29 +556,31 @@ umoul() {
# get the last arg
for arg; do true; done
if test "$arg"
then mountpoint="$(test -d "$arg" && realpath "$arg" || echo "${MNT:-${XDG_RUNTIME_DIR}/mnt}/$arg")"
mountpoint "$mountpoint" 2>/dev/null || test -b "$mountpoint" ||
mountpoint="$(mount --show-labels | grep "$arg" | cut -d' ' -f3)"
test "$mountpoint" || return 1
else mountpoint="$PWD"
while test "$mountpoint" != "/"; do
mountpoint "$mountpoint" >/dev/null && break
mountpoint="$(dirname "$mountpoint")"
then mountpoints="$(test -d "$arg" && realpath "$arg" || echo "${MNT:-${XDG_RUNTIME_DIR}/mnt}/$arg")"
mountpoint "$mountpoints" 2>/dev/null || test -b "$mountpoints" ||
mountpoints="$(mount --show-labels | grep "$arg" | cut -d' ' -f3)"
test "$mountpoints" || return 1
else mountpoints="$PWD"
while test "$mountpoints" != "/"; do
mountpoint "$mountpoints" >/dev/null && break
mountpoints="$(dirname "$mountpoints")"
done
test "$(dirname "$mountpoint")" != "/" ||
mountpoint="$(mount |
test "$(dirname "$mountpoints")" != "/" ||
mountpoints="$(mount |
grep --invert-match -e " /[^m][^/]*\(/[^/]*\)\? " -e "/sys" -e "/run/user" -e "/run/docker" |
fzf --select-1 --exit-0 | awk '{print $3}')" ||
return $?
fi
while true; do
case "$PWD" in
("$mountpoint"*) popd || builtin cd "$(dirname $mountpoint)";;
("$mountpoints"*) popd || builtin cd "$(dirname $mountpoints)";;
(*) break;;
esac
done
# pass on all args except last
moul -u "${@:1:$(((# > 1) ? #-1 : 0))}" "$mountpoint"
for mountpoint in $mountpoints
do moul -u "${@:1:$(((# > 1) ? #-1 : 0))}" "$mountpoint"
done
}
resetdocker() {

View File

@ -5,7 +5,6 @@ command -v notify-send >/dev/null &&
if which jrnl >/dev/null; then
alias j='jrnl'
jn() { jrnl -to today "$@" | less +G; }
compdef jn=jrnl
alias jnc='jn -contains'
alias jne='jrnl --edit'
# TODO jump to end

View File

@ -96,7 +96,7 @@ bindkey '^q' push-line-or-edit
export KEYTIMEOUT=1
# Obsolete: zsh-vim-mode plugin - Custom VIM bindings {{{
# Obsolete because of zsh-vim-mode plugin - Custom VIM bindings {{{
#bindkey -v
#autoload -Uz history-search-end
#