Skip to content

Instantly share code, notes, and snippets.

@ManiaciaChao
Created March 21, 2025 05:20
Show Gist options
  • Save ManiaciaChao/028fca415254df132fff67c3073180a3 to your computer and use it in GitHub Desktop.
Save ManiaciaChao/028fca415254df132fff67c3073180a3 to your computer and use it in GitHub Desktop.
LLM command auto-completion
bind \e\\ __llm_cmd
# run `cargo install llm` to get `llm` command available
# system prompt is borrowed and modified from https://github.com/CGamesPlay/llm-cmd-comp/blob/main/llm_cmd_comp.py
function __llm_cmd -d "Fill in the command using an LLM"
set -l __system_prompt "Return only the command to be executed as a raw string, no string delimiters wrapping it, no yapping, no markdown, no fenced code blocks, what you return will be passed to the shell directly.
For example, if the user asks: undo last git commit
You return only: git reset --soft HEAD~1
The shell is $(which fish) on $(uname)"
set -l __llm_oldcmd (commandline -b)
set -l __llm_cursor_pos (commandline -C)
echo # Start the program on a blank line
set -l result (echo $__llm_oldcmd | llm --system $__system_prompt | tail -n 1)
echo
set_color brmagenta
echo "πŸ€– Suggested command:"
set_color normal
echo "β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€"
set_color brgreen
echo "β”‚ $result"
set_color normal
echo "└─────────────────────"
set_color brblue
echo -n "✏️ Anything to revise? "
set_color yellow
echo "[Enter to confirm, type to revise, R to execute]"
set_color normal
read -l revision
switch $revision
case '' # Empty (just Enter)
if test $status -eq 0
commandline -r $result
echo # Move down a line
end
case R r # Direct execution
if test $status -eq 0
commandline -r $result
commandline -f execute
end
case '*' # Any other input is treated as revision
set -l new_result (echo -e "$__llm_oldcmd\n$result\n$revision" | llm --system $__system_prompt | tail -n 1)
if test $status -eq 0
commandline -r $new_result
echo # Move down a line
end
end
commandline -f repaint
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment