From a4f3f73dc1ee851c5c2f8dffc8aa861514b16627 Mon Sep 17 00:00:00 2001 From: Davide Frattarolo Date: Tue, 29 Sep 2026 18:25:51 +0200 Subject: [PATCH] feat(keybindings): scorciatoie fg-select, clear-repeat e palette --- config.fish | 2 + functions/__clear_repeat.fish | 8 ++ functions/__clear_repeat_loop.fish | 25 +++++ functions/__fg_select.fish | 19 ++++ functions/fish_user_key_bindings.fish | 21 +++++ functions/palette.fish | 126 ++++++++++++++++++++++++++ 6 files changed, 201 insertions(+) create mode 100644 functions/__clear_repeat.fish create mode 100644 functions/__clear_repeat_loop.fish create mode 100644 functions/__fg_select.fish create mode 100644 functions/palette.fish diff --git a/config.fish b/config.fish index 032354a..f365855 100644 --- a/config.fish +++ b/config.fish @@ -18,6 +18,8 @@ if status is-interactive # \c Ctrl, \e Alt bind -M insert \cy 'y; commandline -f repaint' bind -M insert \cv 'nvim; commandline -f repaint' + bind -M insert \el 'clear; lls; commandline -f repaint' + bind -M default \el 'clear; lls; commandline -f repaint' set -q KREW_ROOT; and set -gx PATH $PATH $KREW_ROOT/.krew/bin; or set -gx PATH $PATH $HOME/.krew/bin set -U autovenv_enable yes diff --git a/functions/__clear_repeat.fish b/functions/__clear_repeat.fish new file mode 100644 index 0000000..385a9a3 --- /dev/null +++ b/functions/__clear_repeat.fish @@ -0,0 +1,8 @@ +function __clear_repeat --description "Clear the screen and re-run the last command" + set -l last $history[1] + or return + + clear + commandline -r -- $last + commandline -f execute +end \ No newline at end of file diff --git a/functions/__clear_repeat_loop.fish b/functions/__clear_repeat_loop.fish new file mode 100644 index 0000000..aeadaa2 --- /dev/null +++ b/functions/__clear_repeat_loop.fish @@ -0,0 +1,25 @@ +function __clear_repeat_loop --description "Re-run the last command on a timer, clearing the screen each iteration" + set -l last $history[1] + or return + + read -l -P "Ripeti quante volte? (invio = infinito) " count + read -l -P "Ogni quanti secondi? (default 2) " sec + set -l sec (string match -r '^[0-9]+$' -- $sec) + or set sec 2 + + if test -z "$count"; or test "$count" -eq 0 + while true + clear + eval $last + sleep $sec + end + else + for i in (seq $count) + clear + eval $last + sleep $sec + end + end + + commandline -f repaint +end \ No newline at end of file diff --git a/functions/__fg_select.fish b/functions/__fg_select.fish new file mode 100644 index 0000000..05e82d6 --- /dev/null +++ b/functions/__fg_select.fish @@ -0,0 +1,19 @@ +function __fg_select --description 'Bring a background/stopped job to foreground, picking via fzf when multiple' + set -l jobs_list (jobs | string match -r '^[0-9]+\t.*') + set -l n (count $jobs_list) + if test $n -eq 0 + echo 'no background jobs' >&2 + return 1 + end + + set -l picked + if test $n -eq 1 + set picked $jobs_list[1] + else + set picked (printf '%s\n' $jobs_list | fzf --no-multi --prompt='fg> ') + or return 1 + end + + set -l id (string match -r '^([0-9]+)\t' -- $picked)[2] + fg %$id +end \ No newline at end of file diff --git a/functions/fish_user_key_bindings.fish b/functions/fish_user_key_bindings.fish index 40b3988..784691b 100644 --- a/functions/fish_user_key_bindings.fish +++ b/functions/fish_user_key_bindings.fish @@ -3,4 +3,25 @@ function fish_user_key_bindings if type -q atuin bind -M default k _atuin_bind_up end + + # Ctrl+Shift+Z -> fg (fzf pick when multiple background jobs) + bind -M insert ctrl-shift-z '__fg_select; commandline -f repaint' + bind -M default ctrl-shift-z '__fg_select; commandline -f repaint' + + # Ctrl+Alt+L -> clear and re-run last command + bind -M insert ctrl-alt-l '__clear_repeat' + bind -M default ctrl-alt-l '__clear_repeat' + + # Ctrl+Alt+Shift+L -> clear and re-run last command on a timer + bind -M insert ctrl-alt-shift-l '__clear_repeat_loop' + bind -M default ctrl-alt-shift-l '__clear_repeat_loop' + + # Palette: switch between utilities / shells (fzf) + # Ctrl+Alt+P -> utilities, Ctrl+Alt+S -> shells, Ctrl+Alt+Shift+P -> both + bind -M insert ctrl-alt-p 'palette utility; commandline -f repaint' + bind -M default ctrl-alt-p 'palette utility; commandline -f repaint' + bind -M insert ctrl-alt-s 'palette shell; commandline -f repaint' + bind -M default ctrl-alt-s 'palette shell; commandline -f repaint' + bind -M insert ctrl-alt-shift-p 'palette both; commandline -f repaint' + bind -M default ctrl-alt-shift-p 'palette both; commandline -f repaint' end diff --git a/functions/palette.fish b/functions/palette.fish new file mode 100644 index 0000000..786bb46 --- /dev/null +++ b/functions/palette.fish @@ -0,0 +1,126 @@ +function palette --description 'Command palette: switch between shells and/or utilities via fzf' + set -l mode both + if set -q argv[1] + set mode $argv[1] + end + + set -l lines + switch $mode + case both + set lines (__palette_lines utility; __palette_lines shell) + case shell + set lines (__palette_lines shell) + case utility util + set lines (__palette_lines utility) + case '*' + echo "usage: palette [both|shell|utility]" >&2 + return 1 + end + + test -n "$lines"; or return 1 + + set -l picked (printf '%s\n' $lines | fzf --no-multi --no-sort \ + --delimiter=\t --nth=1 --with-nth=1 \ + --prompt="palette($mode)> " \ + --preview='echo -n {} | cut -f4') + or return 1 + + __palette_dispatch "$picked" +end + +function __palette_lines -a kind + switch $kind + case shell + set -l shells fish:fish bash:bash zsh:zsh nushell:nu dash:dash tcsh:tcsh + for s in $shells + set -l name (string split : -- $s)[1] + set -l bin (string split : -- $s)[2] + command -q $bin; or continue + test $bin = fish; and continue + printf '%s\tshell\t%s\t%s\n' $name $bin "switch to $name (nested)" + end + case utility + printf 'cd\tutil\tcd\tzoxide: jump to a directory\n' + printf 'files\tutil\tfiles\tfd/fzf: pick a file or directory\n' + printf 'history\tutil\thistory\tatuin: search command history\n' + printf 'yazi\tutil\tyazi\tyazi: file manager\n' + printf 'git\tutil\tgit\tgit: common actions\n' + printf 'jobs\tutil\tjobs\tfg: pick a background job\n' + end +end + +function __palette_dispatch + set -l f (string split \t -- $argv[1]) + set -l kind $f[2] + set -l action $f[3] + switch $kind + case util + __palette_run_util $action + case shell + command $action + commandline -f repaint + end +end + +function __palette_run_util -a action + switch $action + case cd + zi + case files + __palette_file + case history + if functions -q _atuin_search + _atuin_search + else if type -q atuin + commandline -r -- (atuin search --interactive) + end + case yazi + y + case jobs + __fg_select + case git + __palette_git + end + commandline -f repaint +end + +function __palette_file + command -q fd; or return 1 + set -l pick (fd -0 -H -t f -t d --max-depth=8 . 2>/dev/null | fzf --read0 \ + --delimiter=/ --with-nth=-1 --prompt='files> ' \ + --preview='test -d "{}"; and lls "{}" 2>/dev/null; or bat --color=always --line-range=:80 "{}" 2>/dev/null; or cat "{}" 2>/dev/null') + or return 1 + + if test -d "$pick" + cd "$pick" + else + commandline -r -- (string escape -- $pick) + end +end + +function __palette_git + set -l lines \ + (string join \t -- status 'git status') \ + (string join \t -- log 'git log --oneline -20') \ + (string join \t -- switch '__palette_git_switch') \ + (string join \t -- diff 'git diff') \ + (string join \t -- 'add -p' 'git add -p') \ + (string join \t -- commit 'git commit') \ + (string join \t -- push 'git push') + set -l pick (printf '%s\n' $lines | fzf --delimiter=\t --with-nth=1 --prompt='git> ') + or return 1 + + set -l cmd (string split \t -- $pick)[2] + switch $cmd + case __palette_git_switch + __palette_git_switch + case '*' + commandline -r -- $cmd + end +end + +function __palette_git_switch + set -l branch (git for-each-ref --format='%(refname:short)' refs/heads | fzf --prompt='branch> ') + or return 1 + git switch $branch +end \ No newline at end of file