From 442e25b4970cad44d9dad59a714640298a468611 Mon Sep 17 00:00:00 2001 From: Guilherme Rugai Freire Date: Sun, 15 May 2022 19:11:26 -0300 Subject: [PATCH] refactor fzf file to edit chooser Changes: - Refactored into a function, instead of alias - Renamed to fzf_fast_file_edit, in place of ce, which is more descriptive - Made the code easier to read and edit --- .config/zsh/.zshrc | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/.config/zsh/.zshrc b/.config/zsh/.zshrc index 991fb49..249a293 100644 --- a/.config/zsh/.zshrc +++ b/.config/zsh/.zshrc @@ -40,9 +40,23 @@ alias config="git --git-dir=\$HOME/.dotfiles/ --work-tree=\$HOME" alias pacs="pacman -Slq | fzf --multi --preview 'pacman -Si {1}' | xargs -ro sudo pacman -S" alias apacs="paru -Slq | fzf --multi --preview 'paru -Si {1}' | xargs -ro paru -S" -# Config/scripts Edit -alias ce="echo \"\$(/bin/ls \$HOME/.scripts/bin | sed \"s|^|\$(realpath \$HOME/.scripts/bin/ --relative-to=.)/|\")\n\$((cd \$HOME && config ls-tree -r master --name-only \$HOME) | sed \"s|^|\$(realpath \$HOME --relative-to=.)/|\" | sed 's|^\./||')\" | fzf --info=inline --prompt='Select a file: ' --preview='bat --paging=never --style=plain --color=always {}' | xargs -r \$(echo \$EDITOR)" -bindkey -s '^[^E' 'ce\n' +fzf_fast_file_edit() { + # Get scripts + scripts="$(/bin/ls "$HOME/.scripts/bin" | sed "s|^|$(realpath "$HOME"/.scripts/bin/ --relative-to=.)/|")" + + # Get config files + current_branch="$(config rev-parse --abbrev-ref HEAD)" + config_files="$(config ls-tree -r "$current_branch" --name-only "$HOME")" + + # Select file + selected_file=$(printf "%s\n%s" "$scripts" "$config_files" | fzf --info=inline --prompt='Select a file: ' --preview='bat --paging=never --style=plain --color=always {}') + + + if [ -n "$selected_file" ]; then + $EDITOR "$selected_file" + fi +} +bindkey -s '^[^E' 'fzf_fast_file_edit\n' # Alias for python3 alias python="python3"