Lokyr
Documentation

Shell integration

On this page

macOS · zsh, bash, fish · on by default · [terminal] shell-integration

Shell integration lets your shell report to Lokyr where each prompt starts, where your command starts and ends, whether it succeeded, and the folder it works in. With it, you can jump between your commands, see at a glance which ones failed, and copy a command's output. Lokyr turns it on for zsh, bash and fish without changing your files.

Try it

Run this command:

ls /nonexistent

Then this one:

seq 1 200

A red bar appears at the left of the prompt of ls, which failed, and a gray one at the left of the prompt of seq, each on its prompt's first line of text. While you read the numbers, with the line seq 1 200 scrolled away, the command shows in a pill at the pane's top. Press ⌘↑: the view jumps up to the prompt of seq. Press ⌘↑ again to reach the prompt of ls, and ⌘↓ to come back down. With Blocks, ⌘↑ selects the block of seq too, and ↑ and ↓ move from block to block · Since 1.4.

What it gives you

Turn it off

[terminal]
shell-integration = false

The change applies to the shells that start after you save the file. The shells that are already open keep what they started with.

Which shells get it

Lokyr loads the integration into your login shell, the shell that every new pane starts.

ShellWhat Lokyr does
zshLoads it through ZDOTDIR, which it sets back to yours at once, so that your .zshenv, .zprofile, .zshrc and .zlogin load as usual
bashStarts bash in POSIX mode, which reads only ENV. The integration leaves POSIX mode, then reads your startup files itself, as a login bash does: /etc/profile, then the first of .bash_profile, .bash_login and .profile
fishAdds its folder to XDG_DATA_DIRS, whose vendor_conf.d fish reads, and takes it away again as fish starts. fish 4 and later report their prompts themselves

A shell that reports its prompts on its own, such as Nushell, works too, with nothing to set up.

Set it up by hand

macOS's own bash, /bin/bash, is version 3.2: it reads your files but not ENV in POSIX mode, so it gets no integration. A later bash, such as Homebrew's, gets it. To add it to /bin/bash, which starts as a login shell in a new pane and reads ~/.bash_profile, add this line to ~/.bash_profile:

[ -n "$LOKYR_RESOURCES_DIR" ] && source "$LOKYR_RESOURCES_DIR/shell-integration/bash/lokyr.bash"

A shell that you start from another one, such as bash typed in a zsh pane, gets no integration either. To have it there too, source your shell's script from its startup file. For zsh, in ~/.zshrc:

[ -n "$LOKYR_RESOURCES_DIR" ] && source "$LOKYR_RESOURCES_DIR/shell-integration/zsh/lokyr-integration"

For bash, the line above, in ~/.bashrc, which a bash that you start from another shell reads. For fish 3, in ~/.config/fish/config.fish; fish 4 and later need nothing:

set -q LOKYR_RESOURCES_DIR; and source "$LOKYR_RESOURCES_DIR/shell-integration/fish/vendor_conf.d/lokyr-shell-integration.fish"

Loading a script twice does no harm, so a shell that Lokyr already set up can keep the line.

How it works

At each prompt and around each command, your shell prints escape codes that Lokyr reads and never shows:

In bash 4.4 and later, the output's mark comes from PS0, so the integration sets no trap: your own DEBUG trap, $_ and the commands that your keys run through bind -x, such as fzf's, stay as they are. An older bash marks the output from a DEBUG trap, unless you have one of your own.

At each prompt, after your own prompt commands run and before the shell draws the prompt, the integration puts its marks around your prompt, so a prompt theme that sets PS1 keeps working.

Limits

Troubleshooting

Commands get no marks

Why: the shell isn't one that Lokyr loaded the integration into, such as /bin/bash or a shell started from another, or the integration is off.

Fix: check that shell-integration is true, open a new tab, or add the integration by hand as above.

Check: in zsh or bash, run:

echo "${_lokyr_integration-none}"

You should see 1. In fish, echo $version should print 4 or later, or functions -q __lokyr_prompt_start; and echo 1 should print 1.

⌘↑ jumps past your last commands to an older prompt

Why: the commands ran in a shell that you started from another one, such as bash typed in a zsh pane, which has no integration: to Lokyr, its whole session is the output of the command that started it, so ⌘↑ goes to that command's prompt.

Fix: source your shell's script in that shell, as above, or make it your login shell.

Check: in zsh or bash, echo "${_lokyr_integration-none}" prints none without the integration, and 1 once you source it. In fish 3, functions -q __lokyr_prompt_start; and echo 1 prints 1 once you source it.

A click in the command line doesn't move the cursor

Platforms: macOS · Since 1.3

Why: [cursor] click-to-move is false, the shell has no integration, the program in front is not a shell, such as ssh, the shell is fish 4.0, or a bash older than 4.4 has a DEBUG trap of its own.

Fix: set click-to-move to true, or use bash 4.4 or later, such as Homebrew's, or fish 4.1 or later.

Check: type echo hello world, click on the w, then type X: the line reads echo hello Xworld.

New tabs open in your home folder

Why: the shell doesn't report its folder, because it has no integration, or the folder belongs to another machine, such as over ssh.

Fix: as for commands that get no marks.

For shell authors

To have Lokyr understand your shell or your prompt, print these codes. Each ends with the string terminator, \033\\; the bell character, \a, works too.

At the prompt's start:

printf '\033]133;A\033\\'

Where the input starts, after the prompt:

printf '\033]133;B\033\\'

As the command starts, with the command line percent-encoded in cmdline_url if you have it:

printf '\033]133;C;cmdline_url=ls%%20-l\033\\'

As the command ends, with its exit status:

printf '\033]133;D;%s\033\\' "$?"

The shell's folder, as a file URL, its path percent-encoded but for its letters, digits and /._~-. In bash:

lokyr_report_folder() {
    local LC_ALL=C url= byte i
    for ((i = 0; i < ${#PWD}; i++)); do
        byte=${PWD:i:1}
        case $byte in
            [A-Za-z0-9/._~-]) url+=$byte ;;
            *) printf -v byte '%%%02X' "'$byte"; url+=$byte ;;
        esac
    done
    printf '\033]7;file://%s%s\033\\' "$HOSTNAME" "$url"
}

Other options of A, and the right prompt:

See also