Open4

Nushellでghqでfzfするキーバインドを設定する

Nakano as a ServiceNakano as a Service

このページに必要なことは全て書いてあった。
https://www.nushell.sh/book/line_editor.html#keybindings
このページでは以下のようにしてキーバインドを設定できるとある。

  let-env config = {
    ...
    keybindings: [
    {
      name: change_dir_with_fzf
      modifier: CONTROL
      keycode: Char_y
      mode: emacs
      event: {
        send: executehostcommand,
        cmd: "cd (ls | where type == dir | each { |it| $it.name} | str join (char nl) | fzf | decode utf-8 | str trim)"
      }
    }
  ]
    ...
  }

このサンプルのコマンドにならい、ghqのパスをfzfでfuzzy searchしてcdできるコマンドを作った。

cd (ghq list --full-path | fzf | decode utf-8 | str trim)
Nakano as a ServiceNakano as a Service

以下をconfigの下の方にあるkeybindingsの設定に追加すればいけた。

keybindings: [
    ...
    {
      name: fuzzy_ghq
      modifier: control
      keycode: char_g
      mode: emacs
      event: {
        send: executehostcommand,
        cmd: "cd (ghq list --full-path | fzf | decode utf-8 | str trim)"
      }
    }
]
Nakano as a ServiceNakano as a Service

input list --fuzzyという組み込みコマンドでfuzzy searchができるようになったので以下でも可能。
だがfzfのような曖昧検索ではないので少し不便。

  {
    name: fuzzy_ghq
    modifier: control
    keycode: char_g
    mode: emacs
    event: {
      send: executehostcommand,
      cmd: "ghq list --full-path | lines | input list --fuzzy | cd $in"
    }
  }