Rename zsh functions

This commit is contained in:
Thomas Avé 2024-04-23 22:58:26 +02:00
parent fe1730ac4f
commit 02fc2ef1b6
1 changed files with 94 additions and 97 deletions

View File

@ -33,6 +33,90 @@ if (( ${+terminfo[smkx]} && ${+terminfo[rmkx]} )); then
add-zle-hook-widget -Uz zle-line-finish zle_application_mode_stop add-zle-hook-widget -Uz zle-line-finish zle_application_mode_stop
fi fi
function cd_to() {
setopt localoptions pipefail no_aliases 2> /dev/null
local dir=$1
if [[ -z "$dir" ]]; then
zle redisplay
return 0
fi
zle push-line # Clear buffer. Auto-restored on next prompt.
cd $dir
zle accept-line
local ret=$?
unset dir # ensure this doesn't end up appearing in prompt expansion
zle reset-prompt
}
# Use FZY instead of FZF for ctrl-t
function find_global_directories() {
zle -I
FZY_CACHE=~/.cache/fzy_paths
if [ -f $FZY_CACHE ]; then
# Check if cache is older than 1 day
if [ $(($(date +%s) - $(date -r $FZY_CACHE +%s))) -gt 86400 ]; then
rm $FZY_CACHE
fi
fi
if [ ! -f $FZY_CACHE ]; then
PATHS="$(/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME ls-files | xargs -n 1 dirname | uniq | grep -v '^\.$' | parallel echo ~/{})"
search_dirs=( \
~/Workspace/ \
/home/server/Storage/Thomas/ \
)
for i in $search_dirs ; do
if [ -d $i ]; then
PATHS="$PATHS\n$i"
PATHS="$PATHS\n$(fdfind . $i -t d -d 8)"
fi
done
echo -e $PATHS > $FZY_CACHE
fi
cd_to "$(cat $FZY_CACHE | fzy -l 20)"
}
function find_local_directories() {
zle -I
cd_to "$(fd . -t d | fzy -l 20)"
}
function edit_local_files() {
zle -I
file=$(fd . -t f | fzy -l 20)
if [ -z "$file" ]; then
return 0
fi
nvim "$file"
}
function edit_global_files() {
zle -I
FZY_CACHE=~/.cache/fzy_file_paths
if [ -f $FZY_CACHE ]; then
# Check if cache is older than 1 day
if [ $(($(date +%s) - $(date -r $FZY_CACHE +%s))) -gt 86400 ]; then
rm $FZY_CACHE
fi
fi
if [ ! -f $FZY_CACHE ]; then
PATHS="$(/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME ls-files | parallel echo ~/{})"
search_dirs=(~/Workspace/ )
for i in $search_dirs ; do
if [ -d $i ]; then
PATHS="$PATHS\n$(fdfind . $i -t f)"
fi
done
echo -e $PATHS > $FZY_CACHE
fi
file=$(cat $FZY_CACHE | fzy -l 20)
if [ -z "$file" ]; then
return 0
fi
cd_to "$(dirname $file)"
nvim "$file"
}
function zvm_after_init() { function zvm_after_init() {
key[Home]="${terminfo[khome]}" key[Home]="${terminfo[khome]}"
key[End]="${terminfo[kend]}" key[End]="${terminfo[kend]}"
@ -85,107 +169,20 @@ function zvm_after_init() {
source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
fi fi
function cd_to() { zle -N find_global_directories
setopt localoptions pipefail no_aliases 2> /dev/null zle -N find_local_directories
local dir=$1
if [[ -z "$dir" ]]; then
zle redisplay
return 0
fi
zle push-line # Clear buffer. Auto-restored on next prompt.
cd $dir
zle accept-line
local ret=$?
unset dir # ensure this doesn't end up appearing in prompt expansion
zle reset-prompt
}
# Use FZY instead of FZF for ctrl-t
function find_global_files() {
zle -I
FZY_CACHE=~/.cache/fzy_paths
if [ -f $FZY_CACHE ]; then
# Check if cache is older than 1 day
if [ $(($(date +%s) - $(date -r $FZY_CACHE +%s))) -gt 86400 ]; then
rm $FZY_CACHE
fi
fi
if [ ! -f $FZY_CACHE ]; then
PATHS="$(/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME ls-files | xargs -n 1 dirname | uniq | grep -v '^\.$' | parallel echo ~/{})"
search_dirs=( \
~/Workspace/ \
~/Containers/ \
/home/server/Storage/Thomas/ \
)
for i in $search_dirs ; do
if [ -d $i ]; then
PATHS="$PATHS\n$i"
PATHS="$PATHS\n$(fd . $i -t d -d 5)"
fi
done
echo -e $PATHS > $FZY_CACHE
fi
cd_to "$(cat $FZY_CACHE | fzy -l 20)"
}
function find_local_files() {
zle -I
cd_to "$(fd . -t d | fzy -l 20)"
}
function edit_local_files() {
zle -I
file=$(fd . -t f | fzy -l 20)
if [ -z "$file" ]; then
return 0
fi
nvim "$file"
}
function edit_global_files() {
zle -I
FZY_CACHE=~/.cache/fzy_file_paths
if [ -f $FZY_CACHE ]; then
# Check if cache is older than 1 day
if [ $(($(date +%s) - $(date -r $FZY_CACHE +%s))) -gt 86400 ]; then
rm $FZY_CACHE
fi
fi
if [ ! -f $FZY_CACHE ]; then
PATHS="$(/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME ls-files | parallel echo ~/{})"
search_dirs=(~/Workspace/ \
~/Containers/ \
)
for i in $search_dirs ; do
if [ -d $i ]; then
PATHS="$PATHS\n$(fd . $i -t f)"
fi
done
echo -e $PATHS > $FZY_CACHE
fi
file=$(cat $FZY_CACHE | fzy -l 20)
if [ -z "$file" ]; then
return 0
fi
cd_to "$(dirname $file)"
nvim "$file"
}
zle -N find_global_files
zle -N find_local_files
zle -N edit_global_files zle -N edit_global_files
zle -N edit_local_files zle -N edit_local_files
bindkey "^T" find_global_files bindkey "^T" find_global_directories
bindkey -M emacs "^T" find_global_files bindkey -M emacs "^T" find_global_directories
bindkey -M vicmd "^T" find_global_files bindkey -M vicmd "^T" find_global_directories
bindkey -M viins "^T" find_global_files bindkey -M viins "^T" find_global_directories
bindkey "^Y" find_local_files bindkey "^Y" find_local_directories
bindkey -M emacs "^Y" find_local_files bindkey -M emacs "^Y" find_local_directories
bindkey -M vicmd "^Y" find_local_files bindkey -M vicmd "^Y" find_local_directories
bindkey -M viins "^Y" find_local_files bindkey -M viins "^Y" find_local_directories
bindkey "^E" edit_global_files bindkey "^E" edit_global_files
bindkey -M emacs "^E" edit_global_files bindkey -M emacs "^E" edit_global_files