Skip to content
Myr

key-bindings.nu

This file

Path

shell/key-bindings.nu

190 lines · 7.3 KiB

Last changed by

[nushell] Quote paths inserted by CTRL-T (#4919)Junegunn Choibefore adoptionfe04475

History of this file

Take it

1#     ____      ____2#    / __/___  / __/3#   / /_/_  / / /_4#  / __/ / /_/ __/5# /_/   /___/_/ key-bindings.nu6#7# - $FZF_TMUX               (default: 0)8# - $FZF_TMUX_OPTS9# - $FZF_TMUX_HEIGHT        (default: 40%)10# - $FZF_CTRL_T_COMMAND     (set to "" to disable)11# - $FZF_CTRL_T_OPTS12# - $FZF_CTRL_R_COMMAND     (set to "" to disable)13# - $FZF_CTRL_R_OPTS14# - $FZF_ALT_C_COMMAND      (set to "" to disable)15# - $FZF_ALT_C_OPTS1617# Code provided by @igor-ramazanov18# Source: https://github.com/junegunn/fzf/issues/4122#issuecomment-26073683161920# Merge default options in the same order as bash/zsh:21#   1. --height, --min-height, --bind=ctrl-z:ignore, $prepend22#   2. $FZF_DEFAULT_OPTS_FILE contents23#   3. $FZF_DEFAULT_OPTS, $append24def __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 trim33}3435# Return the fzf command to use: fzf-tmux when inside tmux and36# FZF_TMUX is enabled or FZF_TMUX_OPTS is set, plain fzf otherwise.37def __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}4950# Keybinding modes to activate. The Helix modes only exist since Nushell51# 0.115.0, so they are only included when running a version that supports52# them (major > 0 covers a hypothetical 1.0+ where the minor resets).53def __fzf_modes []: nothing -> list<string> {54  let v = version55  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}636465export-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}7071# Directories72let alt_c = {73    name: fzf_dirs74    modifier: alt75    keycode: char_c76    mode: (__fzf_modes)77    event: [78      {79        send: executehostcommand80        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}9798# History99let ctrl_r = {100  name: fzf_history101  modifier: control102  keycode: char_r103  mode: (__fzf_modes)104  event: [105    {106      send: executehostcommand107      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 table113        # (bounded by $env.config.history.max_size, default 100,000), so114        # 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 latest117        # entries before the full list is consumed.118        history119          | get command120          | reverse121          | uniq122          | 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-8125          | str trim126      )"127    }128  ]129}130131# Files132let ctrl_t =  {133    name: fzf_files134    modifier: control135    keycode: char_t136    mode: (__fzf_modes)137    event: [138      {139        send: executehostcommand140        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 syntax153          # in a file name is not evaluated when the line is executed.154          let result = (155            $result156            | 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 --end164          }165        "166      }167    ]168}169170# Helper to check if a binding is enabled. A binding is disabled when171# the corresponding *_COMMAND variable is explicitly set to "".172# When not defined (null), the binding is enabled (using fzf's built-in walker).173def __fzf_binding_enabled [var_name: string]: nothing -> bool {174  let val = ($env | get -o $var_name)175  # null = not defined = enabled; "" = explicitly disabled176  $val == null or ($val | into string | is-not-empty)177}178179# Update the $env.config180export-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 changing184  # 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 = $bindings190}