Skip to content

Customizations, hacks and tweaks

Oliver Xu edited this page Apr 8, 2023 · 4 revisions

Customizations, hacks and tweaks

This page contains customizations, hacks and tweaks for changing how Pure looks and operates.

Use different prompt symbol for non-zero exit codes

By default, pure only changes the color of the prompt symbol. You can change this behavior by modifying PROMPT after pure has been initialized in your .zshrc.

.zshrc:

prompt pure
PROMPT='%(?.%F{magenta}△.%F{red}▲)%f '

The above change would show a magenta character by default, and a red when there is a non-zero exit status. This works because pure initialised PROMPT only once, so you are free to modify it afterwards.

Show exit code of last command as a separate prompt character

The prompt character still turns red if the last command didn't exit with 0, but adds another magenta to show that it was the previous command that failed.

Replace: PROMPT='%(?.%F{magenta}.%F{red})❯%f '

With: PROMPT='%(?.%F{magenta}.%F{red}❯%F{magenta})❯%f '

Safer symbols

Pure's default symbols are cool, but if you use a limited font such as Inconsolata (because hey, it does look good), you'll need to change them. These worked for me on Windows:

PURE_PROMPT_SYMBOL='»'
PURE_GIT_DOWN_ARROW=''
PURE_GIT_UP_ARROW=''

Show system time in prompt

To show the current system time before the prompt symbol like

Screenshot

Initialize pure with:

prompt pure
PROMPT='%F{white}%* '$PROMPT

Alternatively you can set RPROMPT='%F{white}%*' beforehand.

Create aliases for really long directory paths

Let's face it, a really long directory path hurts readability and can easily be aliased to something much more meaningful. Pure provides no such functionality, however, Zsh does!

By enabling the Zsh option for named directories (setopt AUTO_NAME_DIRS) we can alias directories by assigning them to a variable.

An example .zshrc with one aliased directory (SUPERSECRET):

setopt AUTO_NAME_DIRS
SUPERSECRET=$HOME/a/really/long/path/to/my/SecretProject

prompt pure

And here's the result:

~cd $SUPERSECRET

~SUPERSECRETpwd
/Users/myuser/a/really/long/path/to/my/SecretProject

As a bonus, you can enable cdable variables (setopt CDABLE_VARS). This allows us to omit the $ when cding into the directory. We can now simply cd SUPERSECRET.

Single line prompt

To display Pure as a single line prompt, add this to your .zshrc:

print() {
  [ 0 -eq $# -a "prompt_pure_precmd" = "${funcstack[-1]}" ] || builtin print "$@";
}
prompt pure

Single line preview

See #509.

Prefix prompt when logged in as root

Add the following to the root users .zshrc:

# For completeness, start by closing the original %F{magenta} with %f.
# The final %f will be added by the PROMPT definition.
PURE_PROMPT_SYMBOL='%f%F{red}#%f %F{magenta}❯'

Root prompt prefix

Originally suggested in #378.

Show number of jobs in prompt

PROMPT='%(1j.[%j] .)%(?.%F{magenta}.%F{red})${PURE_PROMPT_SYMBOL:-❯}%f '

We're simply adding %(1j.[%j] .)% to the regular pure prompt.

This works because %(x.true.false) is a test (just like we're doing with the prompt color %(?.true.false). %j is something that expands to the number of jobs and and %(j.t.f) tests the number of jobs. With 1j we are saying that if the number of jobs >= 1, the test is truthy which will produce the truthy value ([%j] ), otherwise an empty string.

Number of jobs in prompt

Disable Pure terminal title updates

Pure sets the title in an attempt to provide useful information to the user. However, this might not work for all users. Fortunately, the shell is dynamic, and you can override any function.

# First, load Pure.
prompt pure
# Then, replace the set title function with an empty one.
prompt_pure_set_title() {}

See: https://github.com/sindresorhus/pure/pull/383.

Wide cursor for VI command mode

See this example.

Include initial newline when clearing screen (Ctrl+L)

# This clear screen widget allows Pure to re-render with its initial
# newline by manually clearing the screen and placing the cursor on
# line 4 so that the prompt is redisplayed on lines 2 and 3.
custom_prompt_pure_clear_screen() {
	zle -I                   # Enable output to terminal.
	print -n '\e[2J\e[4;0H'  # Clear screen and move cursor to (4, 0).
	zle .redisplay           # Redraw prompt.
}
zle -N clear-screen custom_prompt_pure_clear_screen

Show exit code of all (piped) commands in RPROMPT

It's possible to use the RPROMPT to display some additional information, for example the output of $pipestatus.

precmd_pipestatus() {
	RPROMPT="${(j.|.)pipestatus}"
}
add-zsh-hook precmd precmd_pipestatus

screen shot 2019-02-05 at 16 32 36

Hide when value is 0

precmd_pipestatus() {
	RPROMPT="${(j.|.)pipestatus}"
       if [[ ${(j.|.)pipestatus} = 0 ]]; then
              RPROMPT=""
       fi
}

Use the pure error color for the pipestatus

# show red exit code on right
precmd_pipestatus() {
  local exitcodes="${(j.|.)pipestatus}"
  if [[ "$exitcodes" != "0" ]]; then
    RPROMPT="%F{$prompt_pure_colors[prompt:error]}[$exitcodes]%f"
  else
    RPROMPT=
  fi
}
add-zsh-hook precmd precmd_pipestatus