Shell Integration
src/sempropos/shell.py generates snippets that bind Ctrl+S to transform the current command line in place and register tab completion for sem. The user types a natural-language task, presses Ctrl+S, and the line is replaced with a generated command — never executed.
Installing#
bash# zsh or bash
eval "$(sem init zsh)"
eval "$(sem init bash)"
# fish
sem init fish | source
sem init <shell> prints the snippet verbatim so it can be evaluated or sourced. Supported names are zsh, bash, and fish (supported_shells()). An unsupported name prints Unsupported shell '<name>'. Choose one of: ... plus the supported list to stderr and exits 2 (the CLI usage-error code).
The --porcelain contract#
All widgets call sem --porcelain -- <query>. The contract they depend on:
- the generated command is the only thing written to stdout;
- warnings and errors go to stderr;
- a non-zero exit means no replacement should happen;
- an empty stdout result also leaves the line unchanged.
The widgets also guard on a non-empty current line, so pressing Ctrl+S on an empty prompt does nothing. Failures leave the user's input intact.
zsh#
The zsh snippet runs only in an interactive shell ([[ -o interactive ]]) and:
- calls
stty -ixon(errors suppressed) so the terminal does not consume Ctrl+S as XOFF; - defines
_sem_transform, which returns on an empty$BUFFER, captures stderr in a temporary file, and callscommand sem --porcelain -- "$BUFFER"; - on success with a non-empty result, assigns it to
BUFFERand movesCURSORto the end; - on failure, reads the first stderr line and displays it with
zle -M "sem: $err_msg"; - removes the temporary file and calls
zle reset-prompt; - registers the widget with
zle -N _sem_transformand binds^Swithbindkey; - defines
_sem_completefor command names and, aftershow/doc, tool names fromsem complete-tools, registering it withcompdef semwhencompdefexists.
bash#
The bash snippet:
- defines
_sem_transform, which returns on an empty$READLINE_LINEand callscommand sem --porcelain -- "$READLINE_LINE", discarding stderr; - on success with a non-empty result, writes it back to
READLINE_LINEand setsREADLINE_POINTto the end; - defines
_sem_complete, completing command names at position 1 and indexed tool names aftershow/doc; - in interactive shells (
[[ $- == *i* ]]), runsstty -ixon, binds the function withbind -x '"\C-s": _sem_transform', and registerscomplete -F _sem_complete sem.
fish#
The fish snippet:
- defines
_sem_transform, which reads the buffer withcommandline -band returns when it is empty; - calls
command sem --porcelain -- $query, so a leading dash in natural language is not parsed as an option; - replaces the line with
commandline -r -- "$cmd"only when the command succeeded and returned a non-empty value; - repaints with
commandline -f repaint; - runs
stty -ixonand binds\csto the function; - registers
complete -c semrules for command names and forcomplete-toolsoutput aftershow/doc.
Keybinding notes#
stty -ixonis required because many terminals reserve Ctrl+S for XOFF/flow control. If the terminal still intercepts the key, rebind a different key by editing thebindkey/bind -x/bindline.- Ctrl+R (history) and Ctrl+T (files) are intentionally left to fzf;
semonly adds Ctrl+S. - The replacement is inserted into the prompt, so the user can review and edit it before running.
- Only zsh surfaces the first stderr line in the editor; bash and fish discard diagnostics and simply leave the line unchanged.