Skip to content

Shell integration

asksh init <shell> prints a shell script that sets up ? and Alt+? keybindings and background command telemetry. Evaluating this script in your startup file enables inline prompt replacement and asksh fix.

Setup

Add the hook for your shell if the installer did not already add it:

# ~/.zshrc
eval "$(asksh init zsh)"
# ~/.bashrc
eval "$(asksh init bash)"
# ~/.config/fish/config.fish
asksh init fish | source

How keybindings behave

The shell hook binds two keys:

  • ?: opens asksh only when the line buffer is empty, is literally ?, ends with a space (*), or ends with ? (* ?). Otherwise it inserts a literal ? so URLs and globs keep working. On fish, literal ? insertion is skipped when fish_key_bindings is fish_vi_key_bindings or fish_hybrid_key_bindings.
  • Alt+? (\e?): always opens asksh regardless of cursor or buffer contents, passing the current buffer (with any trailing ? stripped) via --buffer.

On bash, keybindings are registered only when BASH_VERSINFO[0] >= 4 because the hook uses bind -x with READLINE_LINE and READLINE_POINT.

Under the hood, the hook emits an OSC 133;C sequence to /dev/tty, sets ASKSH_SHELL=<shell>, swaps file descriptors (3>&1 1>&2 2>&3), and runs asksh inline --hook. On exit, asksh prints one directive on stderr so the shell knows what to do:

Directive Shell action
__asksh_execute__:<command> Replaces the buffer with <command> and executes it
__asksh_insert__:<command> Replaces the buffer with <command> without executing
__asksh_cancel__ Restores the original buffer and cursor position

Command telemetry hooks

Each shell hook also captures finished commands asynchronously in the background by running:

asksh record --shell <shell> --exit <code> --duration <ms> --cmd <command>

Empty lines and commands starting with asksh are skipped. How each shell collects timing and exit codes:

  • zsh: uses add-zsh-hook for preexec and precmd, with EPOCHREALTIME from zsh/datetime for millisecond duration.
  • bash: uses a DEBUG trap (_asksh_debug_trap, guarded against subshells and completion) paired with PROMPT_COMMAND (supporting both array and string PROMPT_COMMAND). Duration uses EPOCHREALTIME when available and falls back to SECONDS.
  • fish: uses --on-event fish_preexec and --on-event fish_postexec with $CMD_DURATION.

asksh record sends telemetry over the askshd Unix socket when the daemon is running (500ms timeout) and falls back to writing directly to ~/.local/share/asksh/sessions.db.

Prompt status badge

If you use the background daemon, you can embed asksh prompt in your shell prompt to show active jobs and alerts:

[asksh: 1 bg, 1 watch, 1 alert]

asksh prompt uses a 50ms timeout and prints nothing when askshd is stopped or no jobs are active or alerting.

setopt PROMPT_SUBST
PROMPT='$(asksh prompt) '"$PROMPT"
PS1='$(asksh prompt) '"$PS1"
function fish_prompt
    set -l badge (asksh prompt)
    if test -n "$badge"
        echo -n "$badge "
    end
    # ...your existing prompt...
end