Bash prompt linewrap with colors issues

Bash prompt linewrap with colors issues

Published on Mar 19, 2014, 1:35 PM1 min read

So I've been fighting with trying to make a custom color prompt for a while now.

I've recently found out about tput to output color codes. That combined with local variables meant prompts became easier to read.

  local BG_RED=$(tput setab 1)

We started to create a custom prompt here for the team. Had all the info needed at a quick glance. Everything was good except long lines didn't wrap properly. Ctrl+r would just get nutty.

So, after a lot of research and reading various forum posts, I found out that color codes should be wrapped in "\\[...\\]". So example:

  local BG_CYAN=$(tput setab 6)
  local FG_GREEN=$(tput setaf 2)
  local RESET=$(tput sgr0)

  export PS1="\[${BG_CYAN}${FG_GREEN}\]\u@\h \[${RESET}\]$"

Note the \\[ and \\] wrapping the codes. This allows bash to properly figure out the length of the line. Note: I just grabbed random colors, I suspect this example looks bad.

I did find a slight exception

For the TITLEBAR (putty/xterm/screen) support:

case $TERM in
    xterm*|rxvt*)
      TITLEBAR="\[33]0;\u@\h:\w07\]"
      ;;
    screen*)
      TITLEBAR="\[33k\u$\h\]"
      ;;
    *)
      TITLEBAR=''
      ;;
  esac

The [ and ] should wrap the entire block as everything inside is considered part of the control code.

Gavin Mogan
Burnaby, BC, Canada

I'm Gavin.

I'm a tinker, maker, and software developer.

At home I code, game, hang out, all the cool non robot things to do.

Heavily involved with Jenkins open source, and will often submit PRs to random other projects.

I also play games, both board and video games and love to read.

You can usually find me on various services as halkeye.

#notarobot

Comments