Differences between revisions 5 and 6
Revision 5 as of 2010-02-25 14:40:00
Size: 890
Editor: mail
Comment: added note about checkwinsize
Revision 6 as of 2010-03-23 14:17:39
Size: 935
Editor: cE67647C1
Comment: Making it a function, using lowercase variable names, and soft quotes instead of hardquotes.
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:
BLUE=$(tput setaf 4)
PURPLE=$(tput setaf 5)
BLACK=$(tput setaf 0)
PS1='\[$BLUE\]\h:\[$PURPLE\]\w\[$BLACK\]\$ '
fancy_prompt() {
  local blue
=$(tput setaf 4)
  local purple=$(tput setaf 5)
  local reset=$(tput sgr0)
  PS1="\[$blue\]\h:\[$purple\]\w\[$reset\]\\$ "
}

I have a fancy prompt with colors, and now bash doesn't seem to know how wide my terminal is. Lines wrap around incorrectly.

You must put \[ and \] around any non-printing escape sequences in your prompt. Thus:

fancy_prompt() {
  local blue=$(tput setaf 4)
  local purple=$(tput setaf 5)
  local reset=$(tput sgr0)
  PS1="\[$blue\]\h:\[$purple\]\w\[$reset\]\\$ "
}

Without the \[ \], bash will think the bytes which constitute the escape sequences for the color codes will actually take up space on the screen, so bash won't be able to know where the cursor actually is.

If you still have problems, e.g. when going through your command history with the Up/Down arrows, make sure you have the checkwinsize option set:

shopt -s checkwinsize;

Refer to the Wikipedia article for ANSI escape codes.

BashFAQ/053 (last edited 2022-09-14 02:03:48 by emanuele6)