From 02fc2ef1b6a424c64d65a038e056696eb4648f96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Av=C3=A9?= Date: Tue, 23 Apr 2024 22:58:26 +0200 Subject: [PATCH] Rename zsh functions --- .config/zsh/.zshrc | 191 ++++++++++++++++++++++----------------------- 1 file changed, 94 insertions(+), 97 deletions(-) diff --git a/.config/zsh/.zshrc b/.config/zsh/.zshrc index afb13ef..3c560d0 100644 --- a/.config/zsh/.zshrc +++ b/.config/zsh/.zshrc @@ -33,6 +33,90 @@ if (( ${+terminfo[smkx]} && ${+terminfo[rmkx]} )); then add-zle-hook-widget -Uz zle-line-finish zle_application_mode_stop 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() { key[Home]="${terminfo[khome]}" key[End]="${terminfo[kend]}" @@ -85,107 +169,20 @@ function zvm_after_init() { source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh 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_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 find_global_directories + zle -N find_local_directories zle -N edit_global_files zle -N edit_local_files - bindkey "^T" find_global_files - bindkey -M emacs "^T" find_global_files - bindkey -M vicmd "^T" find_global_files - bindkey -M viins "^T" find_global_files + bindkey "^T" find_global_directories + bindkey -M emacs "^T" find_global_directories + bindkey -M vicmd "^T" find_global_directories + bindkey -M viins "^T" find_global_directories - bindkey "^Y" find_local_files - bindkey -M emacs "^Y" find_local_files - bindkey -M vicmd "^Y" find_local_files - bindkey -M viins "^Y" find_local_files + bindkey "^Y" find_local_directories + bindkey -M emacs "^Y" find_local_directories + bindkey -M vicmd "^Y" find_local_directories + bindkey -M viins "^Y" find_local_directories bindkey "^E" edit_global_files bindkey -M emacs "^E" edit_global_files