Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Prompt Customization

Hash shell supports powerful prompt customization through the PS1 variable.

Presets

Pick a prompt by name and it applies at once:

set prompt=two-line

set prompt=list shows every preset with a live sample, the current one marked with *:

PresetLook
classic
plain
minimal
two-line
powerline
  • classic is the default: path, git branch and status, and #> in the colour of the last exit status.
  • plain is the look of a classic bash prompt, user@host:path$, without git.
  • minimal shows the directory name and a chevron coloured by the last status.
  • two-line puts user, host, path and git on one line and the chevron on the next, with a failed status in red before the line break.
  • powerline draws coloured segments with triangle separators. It uses characters every monospace font has, so no patched font is needed.

Put the line in ~/.hashrc to keep it. The presets use the colour elements below (path, git_*, prompt, prompt_error), so set color.path=... changes them too, except the powerline segments, whose backgrounds are part of the look. A name that is not a preset fails with the list of names and status 1.

Default Prompt

/home/user/projects git:(main) #>
  • Blue current path
  • Green git: (yellow if dirty)
  • Cyan branch name
  • Blue #> (red if last command failed)

Quick Start

In ~/.hashrc:

# Simple prompt
set PS1='\W #> '

# Full path with git
set PS1='\w\g \e#>\e '

# User@host:path
set PS1='\u@\h:\w\$ '

Or as environment variable:

export PS1='\w\g \e#>\e '

Continuation Prompt (PS2)

When a line does not finish a command, for example for f in *.txt on its own, hash shows the continuation prompt and keeps reading until the compound command is complete:

#> for f in *.txt
> do
>   echo "$f"
> done

The default is > . Set PS2 to change it:

export PS2='... '

Press Ctrl+C at the continuation prompt to abandon the unfinished command.

Escape Sequences

Path Information

SequenceDescriptionExample
\wFull current path/home/user/projects
\WCurrent directory onlyprojects

User Information

SequenceDescription
\uUsername
\hHostname
\$$ for user, # for root

Git Integration

SequenceDescription
\gGit branch with status

The \g sequence:

  • Shows nothing outside git repos
  • Shows git:(branch) in repos
  • git: is green if clean, yellow if dirty
  • Branch name is cyan

Exit Code Indicator

SequenceDescription
\eSets color based on exit code

Use \e before and after text:

set PS1='\w \e#>\e '
#           ^  ^
#           |  Reset color
#           Set color (blue=success, red=fail)

Special Characters

SequenceOutput
\$# for root, $ otherwise
\nNewline
\\A backslash
\[ \]Ignored; bash wraps colour codes in them and hash measures the codes itself

Status, Time, Jobs

SequenceOutput
\?The last exit status when it was not 0, otherwise nothing
\bThe git branch alone, nothing outside a repository
\t \T \@The time: 24-hour, 12-hour, 12-hour with am/pm
\dThe date, Thu Nov 06
\jThe number of background jobs
\!The history number of the command about to be entered
\s \vThe shell name and version
\HThe full host name (\h stops at the first dot)

Example Prompts

Minimal

set PS1='\W #> '
# Output: projects #>

Classic

set PS1='\u@\h:\w \$ '
# Output: user@hostname:/home/user $

Full Path with Git

set PS1='\w\g \e#>\e '
# Output: /home/user/projects git:(main) #>

Two-Line

set PS1='\w\g\n\e#>\e '
# Output:
# /home/user/projects git:(main)
# #>

Powerline Style

set prompt=powerline     # the preset; see Presets above

Color Behavior

Path Colors

\w and \W are bold blue

Git Colors

  • git: is green when clean
  • git: is yellow when dirty
  • Branch name is cyan

Exit Code Colors

  • \e sets blue on success (exit 0)
  • \e sets red on failure (exit non-zero)

Tips

Test Before Setting

#> export PS1='\W\g \e#>\e '
projects git:(main) #>

Keep It Simple

Complex prompts can slow down the shell:

# Fast
set PS1='\W #> '

# Slower (git checks)
set PS1='\w\g \e#>\e '

Two-Line for Long Paths

set PS1='\w\g\n\e#>\e '

Disable Git in Large Repos

set PS1='\w \e#>\e '  # No \g

Troubleshooting

Colors Not Showing

Enable colors:

set colors=on

Check terminal:

echo $TERM
# Should be xterm-256color or similar

Git Branch Not Showing

Verify you’re in a git repo:

git status

Ensure \g is in your PS1.

Exit Colors Not Working

Wrap characters between two \e:

# Wrong
set PS1='\w \e#> '

# Correct
set PS1='\w \e#>\e '

Comparison with Bash

Similar

  • \w, \W, \u, \h, \H, \$, \t, \T, \@, \d, \j, \!, \s, \v, \[, \] work the same
  • Color support
  • Environment variable support

Hash-Specific

  • set prompt=NAME - Presets
  • \? - Last exit status when non-zero, \b - bare git branch
  • \g - Git integration
  • \e - Exit code color
  • Automatic ~ substitution

Not Supported (Yet)

  • \D{format} - Date with a custom format
  • PROMPT_COMMAND - Use $(...) in PS1 instead