| 1 |
# ____ ____ |
| 2 |
# / __/___ / __/ |
| 3 |
# / /_/_ / / /_ |
| 4 |
# / __/ / /_/ __/ |
| 5 |
# /_/ /___/_/ key-bindings.nu |
| 6 |
# |
| 7 |
# - $FZF_TMUX (default: 0) |
| 8 |
# - $FZF_TMUX_OPTS |
| 9 |
# - $FZF_TMUX_HEIGHT (default: 40%) |
| 10 |
# - $FZF_CTRL_T_COMMAND (set to "" to disable) |
| 11 |
# - $FZF_CTRL_T_OPTS |
| 12 |
# - $FZF_CTRL_R_COMMAND (set to "" to disable) |
| 13 |
# - $FZF_CTRL_R_OPTS |
| 14 |
# - $FZF_ALT_C_COMMAND (set to "" to disable) |
| 15 |
# - $FZF_ALT_C_OPTS |
| 16 |
|
| 17 |
# Code provided by @igor-ramazanov |
| 18 |
# Source: https://github.com/junegunn/fzf/issues/4122#issuecomment-2607368316 |
| 19 |
|
| 20 |
# Merge default options in the same order as bash/zsh: |
| 21 |
# 1. --height, --min-height, --bind=ctrl-z:ignore, $prepend |
| 22 |
# 2. $FZF_DEFAULT_OPTS_FILE contents |
| 23 |
# 3. $FZF_DEFAULT_OPTS, $append |
| 24 |
def __fzf_defaults [prepend: string, append: string]: nothing -> string { |
| 25 |
let base = $"--height ($env.FZF_TMUX_HEIGHT? | default '40%') --min-height 20+ --bind=ctrl-z:ignore ($prepend)" |
| 26 |
let opts_file = if ($env.FZF_DEFAULT_OPTS_FILE? | default '' | is-not-empty) { |
| 27 |
try { open --raw ($env.FZF_DEFAULT_OPTS_FILE) | str trim } catch { '' } |
| 28 |
} else { |
| 29 |
'' |
| 30 |
} |
| 31 |
let default_opts = $env.FZF_DEFAULT_OPTS? | default '' |
| 32 |
$"($base) ($opts_file) ($default_opts) ($append)" | str trim |
| 33 |
} |
| 34 |
|
| 35 |
# Return the fzf command to use: fzf-tmux when inside tmux and |
| 36 |
# FZF_TMUX is enabled or FZF_TMUX_OPTS is set, plain fzf otherwise. |
| 37 |
def __fzfcmd []: nothing -> list<string> { |
| 38 |
let in_tmux = ($env.TMUX_PANE? | default '' | into string | is-not-empty) |
| 39 |
if $in_tmux { |
| 40 |
let fzf_tmux = ($env.FZF_TMUX? | default 0 | into string) |
| 41 |
let fzf_tmux_opts = ($env.FZF_TMUX_OPTS? | default '' | into string) |
| 42 |
if ($fzf_tmux != '0') or ($fzf_tmux_opts | is-not-empty) { |
| 43 |
let opts = if ($fzf_tmux_opts | is-not-empty) { $fzf_tmux_opts } else { $"-d($env.FZF_TMUX_HEIGHT? | default '40%')" } |
| 44 |
return ['fzf-tmux' ...(($opts | split row ' ' | where { $in != '' })) '--'] |
| 45 |
} |
| 46 |
} |
| 47 |
['fzf'] |
| 48 |
} |
| 49 |
|
| 50 |
# Keybinding modes to activate. The Helix modes only exist since Nushell |
| 51 |
# 0.115.0, so they are only included when running a version that supports |
| 52 |
# them (major > 0 covers a hypothetical 1.0+ where the minor resets). |
| 53 |
def __fzf_modes []: nothing -> list<string> { |
| 54 |
let v = version |
| 55 |
let major = ($v.major | into int) |
| 56 |
let minor = ($v.minor | into int) |
| 57 |
if ($major > 0) or ($minor >= 115) { |
| 58 |
['emacs', 'vi_normal', 'vi_insert', 'helix_normal', 'helix_select', 'helix_insert'] |
| 59 |
} else { |
| 60 |
['emacs', 'vi_normal', 'vi_insert'] |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
export-env { |
| 66 |
$env.FZF_CTRL_T_OPTS = $env.FZF_CTRL_T_OPTS? | default "" |
| 67 |
$env.FZF_CTRL_R_OPTS = $env.FZF_CTRL_R_OPTS? | default "" |
| 68 |
$env.FZF_ALT_C_OPTS = $env.FZF_ALT_C_OPTS? | default "" |
| 69 |
} |
| 70 |
|
| 71 |
# Directories |
| 72 |
let alt_c = { |
| 73 |
name: fzf_dirs |
| 74 |
modifier: alt |
| 75 |
keycode: char_c |
| 76 |
mode: (__fzf_modes) |
| 77 |
event: [ |
| 78 |
{ |
| 79 |
send: executehostcommand |
| 80 |
cmd: " |
| 81 |
let fzf_opts = (__fzf_defaults '--reverse --walker=dir,follow,hidden --scheme=path' $'($env.FZF_ALT_C_OPTS) +m'); |
| 82 |
let fzfcmd = (__fzfcmd); |
| 83 |
let fzf_args = ($fzfcmd | skip 1); |
| 84 |
let alt_c_cmd = ($env.FZF_ALT_C_COMMAND? | default null); |
| 85 |
let result = if ($alt_c_cmd == null) or ($alt_c_cmd | is-empty) { |
| 86 |
with-env { FZF_DEFAULT_OPTS: $fzf_opts, FZF_DEFAULT_OPTS_FILE: '' } { ^($fzfcmd | first) ...$fzf_args } |
| 87 |
} else { |
| 88 |
let fzf_cmd_str = ($fzfcmd | str join ' '); |
| 89 |
let sh_cmd = [$alt_c_cmd '|' $fzf_cmd_str] | str join ' '; |
| 90 |
with-env { FZF_DEFAULT_OPTS: $fzf_opts, FZF_DEFAULT_OPTS_FILE: '' } { ^sh -c $sh_cmd } |
| 91 |
}; |
| 92 |
if ($result | is-not-empty) { cd $result }; |
| 93 |
" |
| 94 |
} |
| 95 |
] |
| 96 |
} |
| 97 |
|
| 98 |
# History |
| 99 |
let ctrl_r = { |
| 100 |
name: fzf_history |
| 101 |
modifier: control |
| 102 |
keycode: char_r |
| 103 |
mode: (__fzf_modes) |
| 104 |
event: [ |
| 105 |
{ |
| 106 |
send: executehostcommand |
| 107 |
cmd: "commandline edit --replace ( |
| 108 |
let fzf_opts = (__fzf_defaults '' $'--scheme=history --bind=ctrl-r:toggle-sort --wrap-sign \"\t↳ \" --highlight-line ($env.FZF_CTRL_R_OPTS) +m --read0'); |
| 109 |
let fzfcmd = (__fzfcmd); |
| 110 |
let fzf_args = ($fzfcmd | skip 1); |
| 111 |
# reverse | uniq: show most recent first, deduplicate keeping the latest. |
| 112 |
# Nushell's `history` loads the full history as an in-memory table |
| 113 |
# (bounded by $env.config.history.max_size, default 100,000), so |
| 114 |
# reverse and uniq run on an already-materialized list. This is O(n) |
| 115 |
# but acceptable for typical history sizes; unlike bash/zsh `fc -r`, |
| 116 |
# there is no streaming primitive that would let fzf show the latest |
| 117 |
# entries before the full list is consumed. |
| 118 |
history |
| 119 |
| get command |
| 120 |
| reverse |
| 121 |
| uniq |
| 122 |
| str join (char -i 0) |
| 123 |
| with-env { FZF_DEFAULT_OPTS: $fzf_opts, FZF_DEFAULT_OPTS_FILE: '' } { ^($fzfcmd | first) ...$fzf_args --query (commandline) } |
| 124 |
| decode utf-8 |
| 125 |
| str trim |
| 126 |
)" |
| 127 |
} |
| 128 |
] |
| 129 |
} |
| 130 |
|
| 131 |
# Files |
| 132 |
let ctrl_t = { |
| 133 |
name: fzf_files |
| 134 |
modifier: control |
| 135 |
keycode: char_t |
| 136 |
mode: (__fzf_modes) |
| 137 |
event: [ |
| 138 |
{ |
| 139 |
send: executehostcommand |
| 140 |
cmd: " |
| 141 |
let fzf_opts = (__fzf_defaults '--reverse --walker=file,dir,follow,hidden --scheme=path' $'($env.FZF_CTRL_T_OPTS) -m --print0'); |
| 142 |
let fzfcmd = (__fzfcmd); |
| 143 |
let fzf_args = ($fzfcmd | skip 1); |
| 144 |
let ctrl_t_cmd = ($env.FZF_CTRL_T_COMMAND? | default null); |
| 145 |
let result = if ($ctrl_t_cmd == null) or ($ctrl_t_cmd | is-empty) { |
| 146 |
with-env { FZF_DEFAULT_OPTS: $fzf_opts, FZF_DEFAULT_OPTS_FILE: '' } { ^($fzfcmd | first) ...$fzf_args } |
| 147 |
} else { |
| 148 |
let fzf_cmd_str = ($fzfcmd | str join ' '); |
| 149 |
let sh_cmd = [$ctrl_t_cmd '|' $fzf_cmd_str] | str join ' '; |
| 150 |
with-env { FZF_DEFAULT_OPTS: $fzf_opts, FZF_DEFAULT_OPTS_FILE: '' } { ^sh -c $sh_cmd } |
| 151 |
}; |
| 152 |
# Serialize each path as a Nushell string literal, so that syntax |
| 153 |
# in a file name is not evaluated when the line is executed. |
| 154 |
let result = ( |
| 155 |
$result |
| 156 |
| split row (char nul) |
| 157 |
| where {|path| $path != ''} |
| 158 |
| each {|path| $path | to nuon} |
| 159 |
| str join ' ' |
| 160 |
); |
| 161 |
if ($result | is-not-empty) { |
| 162 |
commandline edit --append $'($result) '; |
| 163 |
commandline set-cursor --end |
| 164 |
} |
| 165 |
" |
| 166 |
} |
| 167 |
] |
| 168 |
} |
| 169 |
|
| 170 |
# Helper to check if a binding is enabled. A binding is disabled when |
| 171 |
# the corresponding *_COMMAND variable is explicitly set to "". |
| 172 |
# When not defined (null), the binding is enabled (using fzf's built-in walker). |
| 173 |
def __fzf_binding_enabled [var_name: string]: nothing -> bool { |
| 174 |
let val = ($env | get -o $var_name) |
| 175 |
# null = not defined = enabled; "" = explicitly disabled |
| 176 |
$val == null or ($val | into string | is-not-empty) |
| 177 |
} |
| 178 |
|
| 179 |
# Update the $env.config |
| 180 |
export-env { |
| 181 |
let fzf_names = ['fzf_files', 'fzf_dirs', 'fzf_history'] |
| 182 |
# Filter out any existing fzf bindings, then re-add the enabled ones. |
| 183 |
# This allows re-sourcing to update bindings (e.g. after changing |
| 184 |
# FZF_CTRL_T_COMMAND) without creating duplicates. |
| 185 |
mut bindings = ($env.config.keybindings | where { |kb| $kb.name not-in $fzf_names }) |
| 186 |
if (__fzf_binding_enabled 'FZF_ALT_C_COMMAND') { $bindings = ($bindings | append $alt_c) } |
| 187 |
if (__fzf_binding_enabled 'FZF_CTRL_R_COMMAND') { $bindings = ($bindings | append $ctrl_r) } |
| 188 |
if (__fzf_binding_enabled 'FZF_CTRL_T_COMMAND') { $bindings = ($bindings | append $ctrl_t) } |
| 189 |
$env.config.keybindings = $bindings |
| 190 |
} |