Shell prompts and tricks

Many interactive REPL-style programs (such as a “shell") allow you to customize the string used to prompt for input. Putting certain DomTerm-specific escape sequences in the prompt enables some nice features. The following discussion focuses on the bash shell.

Detecting domterm terminal

The DOMTERM environment variable is normally set when running under DomTerm. However, using DOMTERM to determine if the current terminal is DomTerm is not reliable: If you ssh, DOMTERM will not be automatically set. On the other hand, if you start some other terminal emulator like xterm from within DomTerm, the xterm will inherit the DOMTERM variable will be misleadingly set.

So it is better to probe the terminal itself. You can use the domterm is-domterm command:

if domterm is-domterm
  execute domterm-specific actions

If you’re not sure whether the domterm program will be in the PATH, you can use the following shell code:

probe_domterm() {
  # probe if TERM unset, or contains "xterm", or DOMTERM is set
  case "$TERM/$DOMTERM" in
    /* | *xterm*/* | */?*)
      echo -en "\e[>0c" >/dev/tty
      read -s -t 1 -d c </dev/tty
      case "${REPLY}" in
        ?"[>990;"*";"*) DOMTERM_REPLY="${REPLY}" ;;
        "") DOMTERM_REPLY="-timeout)" ;;
        *) DOMTERM_REPLY="-non-match"
    *) DOMTERM_REPLY="-not-tried"

is_domterm() {
  test -z "$DOMTERM_REPLY" && probe_domterm
  case "$DOMTERM_REPLY" in
    -*) return -1;;
    *) return 0;;

if is_domterm
  execute domterm-specific actions

The is_domterm function only tests if the current terminal is domterm; it does not check if standard input/output have been re-directed. To check that, you can use the test builtin with the -t option:

if test -t 0 -a -t 1 -a -t 2 && is_domterm
  execute domterm-specific actions

Setting the Bash shell prompt

You can place the following in your ~/.bashrc to take advantage of DomTerm features:

if [ "$PS1" != "" ]
  # Optionally override the system default - for all terminals
  PS1='$ ' # or whatever

  # Check if the DOMTERM variable includes the string "tty=ttyname"
  # where ttyname is the output from the tty command
  case ";$DOMTERM;" in
    *";tty=`tty`;"*) ;;
    *";tty="*) unset DOMTERM;;

  if [ -n "$DOMTERM" ]
    # Add some DomTerm-specific escape sequences

This causes the prompt to have the prompt style (specifically to be in a <span std="prompt"> element), while the remainder of the current line gets the input style (specifically, in a <span std="input"> element). The appearance of these styles can be customized with CSS stylesheets.

Furthermore, this prompt enables text folding: a hide/show button (click on the character), which hides/shows the output from the command.

See the Wire byte protocol section for the details.

(The "\[" and "\]" are bash syntax, and are equivalent to readline’s "\001" and "\002". They are used to indicate escape sequences that don’t move the cursor, which is needed for readline to calculate the column position.)

Tracking current working directory

It is useful for DomTerm to track the working directory of the process. One reason is creating links from compiler error messages.

If you’re using the Bash shell, you can set the PROMPT_COMMAND to send a special escape sequence, like the following.

# Based on Orwellophile's answer to
# adding the LC_ALL=C trick from /etc/profile.d/ (on Fedora27)
print_path_url() {
  local LC_ALL=C
  local string="$PWD"
  local strlen=${#string}
  local encoded=""
  local pos c o

  for (( pos=0 ; pos<strlen ; pos++ )); do
     case "$c" in
        [-_.~a-zA-Z0-9/] ) o="${c}" ;;
        * )               printf -v o '%%%02x' "'$c"
  printf "\033]7;file://%s%s\007" "${HOSTNAME:-}" "${encoded}"
test "$PROMPT_COMMAND" = __vte_prompt_command || \

(On some platforms Gnome Terminal loads which sets PROMPT_COMMAND to __vte_prompt_command, which sends the same escape sequence as print_path_url.)

Directory tracking of make

Sometimes make will recurse into sub-directories. Error message in those sub-directories may be relative. The following make wrapper causes make to report to DomTerm the current directory, so it can resolve relative files names to absolsute file: links.

if test -t 1 && is_domterm
  export MAKE=`command -v $0`
  $BASE_MAKE "$@"
  exit $ex
  $BASE_MAKE "$@"

This uses the is_domterm and print_path_url functions defined above.