Created
November 2, 2025 05:23
-
-
Save wilbeibi/45662697ffe555719e6dac54e51e445d to your computer and use it in GitHub Desktop.
~/.config/fish/conf.d/kimi-cli.fish
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Kimi CLI integration for fish: toggle a prefix to run commands via `kimi -c` | |
| # Inspired by MoonshotAI/zsh-kimi-cli | |
| set -q __KIMI_CLI_PREFIX_CHAR; or set -g __KIMI_CLI_PREFIX_CHAR '✨' | |
| set -q __KIMI_CLI_PREFIX; or set -g __KIMI_CLI_PREFIX "$__KIMI_CLI_PREFIX_CHAR " | |
| set -g __KIMI_CLI_PREFIX_ACTIVE 0 | |
| function __kimi_cli_run --description 'Run the given argv via Kimi CLI' | |
| if not type -q kimi | |
| echo "kimi: command not found" >&2 | |
| return 127 | |
| end | |
| if test (count $argv) -eq 0 | |
| echo "kimi-cli: nothing to run after '$__KIMI_CLI_PREFIX_CHAR'." >&2 | |
| return 127 | |
| end | |
| set -l escaped | |
| for t in $argv | |
| set escaped $escaped (string escape -- $t) | |
| end | |
| set -l cmd (string join ' ' -- $escaped) | |
| kimi -c "$cmd" | |
| end | |
| # Create a function named as the prefix glyph for `✨ ls` syntax | |
| functions -q $__KIMI_CLI_PREFIX_CHAR; and functions -e $__KIMI_CLI_PREFIX_CHAR | |
| eval "function $__KIMI_CLI_PREFIX_CHAR --description 'Run next args via Kimi CLI'; __kimi_cli_run \$argv; end" | |
| # Wrap command-not-found to support `✨ls` (no space) | |
| if functions -q fish_command_not_found | |
| functions -q __kimi_cli_prev_command_not_found; and functions -e __kimi_cli_prev_command_not_found | |
| functions -c fish_command_not_found __kimi_cli_prev_command_not_found | |
| end | |
| function fish_command_not_found --description 'kimi fallback when line starts with prefix' | |
| set -l missing $argv[1] | |
| set -l rest $argv[2..-1] | |
| if test "$missing" = "$__KIMI_CLI_PREFIX_CHAR" | |
| __kimi_cli_run $rest | |
| return $status | |
| else if string match -q -- "$__KIMI_CLI_PREFIX_CHAR"* -- $missing | |
| set -l stripped (string replace -r "^"(string escape --style=regex -- "$__KIMI_CLI_PREFIX_CHAR") "" -- "$missing") | |
| __kimi_cli_run $stripped $rest | |
| return $status | |
| end | |
| if functions -q __kimi_cli_prev_command_not_found | |
| __kimi_cli_prev_command_not_found $argv | |
| return $status | |
| end | |
| echo "fish: Unknown command: $missing" >&2 | |
| return 127 | |
| end | |
| function __kimi_cli_toggle_prefix --description 'Toggle Kimi prefix at start of buffer' | |
| set -l buf (commandline) | |
| set -l pos (commandline --cursor) | |
| set -l prefix_re (string escape --style=regex -- $__KIMI_CLI_PREFIX) | |
| if string match -rq "^$prefix_re" -- $buf | |
| commandline --replace (string replace -r "^$prefix_re" "" -- $buf) | |
| set -g __KIMI_CLI_PREFIX_ACTIVE 0 | |
| set -l prefix_len (string length -- $__KIMI_CLI_PREFIX) | |
| if test $pos -gt $prefix_len | |
| commandline --cursor (math $pos - $prefix_len) | |
| else | |
| commandline --cursor 0 | |
| end | |
| else | |
| commandline --replace -- "$__KIMI_CLI_PREFIX$buf" | |
| set -g __KIMI_CLI_PREFIX_ACTIVE 1 | |
| commandline --cursor (string length -- $__KIMI_CLI_PREFIX) | |
| end | |
| commandline -f repaint | |
| end | |
| function __kimi_cli_prompt_prefix --on-event fish_prompt | |
| if test $__KIMI_CLI_PREFIX_ACTIVE -eq 1 | |
| set -l buf (commandline) | |
| set -l prefix_re (string escape --style=regex -- $__KIMI_CLI_PREFIX) | |
| if not string match -rq "^$prefix_re" -- $buf | |
| commandline --replace -- "$__KIMI_CLI_PREFIX$buf" | |
| commandline --cursor (string length -- $__KIMI_CLI_PREFIX) | |
| end | |
| end | |
| end | |
| # Guard backspace/kill-word to prevent deleting across prefix boundary | |
| for f in backward-delete-char backward-kill-word | |
| if functions -q $f | |
| functions -q __kimi_cli_prev_$f; and functions -e __kimi_cli_prev_$f | |
| functions -c $f __kimi_cli_prev_$f | |
| eval " | |
| function $f | |
| if test \$__KIMI_CLI_PREFIX_ACTIVE -eq 1 | |
| set -l buf (commandline) | |
| if string match -q -- \"\$__KIMI_CLI_PREFIX\"* -- \$buf | |
| set -l min (string length -- \"\$__KIMI_CLI_PREFIX\") | |
| set -l pos (commandline --cursor) | |
| if test \$pos -le \$min | |
| commandline -f beep 2>/dev/null | |
| return | |
| end | |
| end | |
| end | |
| __kimi_cli_prev_$f | |
| end | |
| " | |
| end | |
| end | |
| # Keybindings | |
| set -q KIMI_CLI_KEYBIND; or set -g KIMI_CLI_KEYBIND ctrl-x | |
| function __kimi_cli_bindings_setup --on-variable fish_key_bindings | |
| for mode in insert default vi-insert vi-normal | |
| bind -M $mode $KIMI_CLI_KEYBIND __kimi_cli_toggle_prefix 2>/dev/null | |
| end | |
| end | |
| if status is-interactive | |
| __kimi_cli_bindings_setup | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment