0

This is my prompt, as specified in ~/.bashrc:

export PS1="${debian_chroot:+($debian_chroot)}\[\033[01;35m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "

...that yields something like:

Prompt usual color

I would like to video-invert the colors of the luis@PortatilAsu part.
I have read on the net that the ANSI Code prefix for video inversion on any text string should be \033[7m, so I tested:

export PS1="${debian_chroot:+($debian_chroot)}\[\033[01;35m\]\[\033[7m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "

(note the extra \033[7m and its position).
And the results seem to be fine:

Inverted prompt

But the prompt config seems rather awkward, and some docs say you can screw things if you don't put codes in the right place and have strange behaviors like cursor not steping, backspace going over the prompt itself, new lines not happening... etc.

Is the position I have choosen for the PS1 prompt variable all right? My prompt seems to be working OK right now (no problem with backspaces, line endings... etc); should it manifest such strange behaviors? Any basic rule (if it exists) to prevent that "strange behavior" of the prompt would be great, too.

2

Your code looks fine, but you can just put your 7 inside previous colour definition, and additionally I would remove all unnecessary zeros, so:

export PS1="${debian_chroot:+($debian_chroot)}\[\033[1;7;35m\]\u@\h\[\033[0m\]:\[\033[1;34m\]\w\[\033[0m\]\$ "
  • OK, upon your optimization, I think I understand the syntax now. Thanks you but, why not removing the zeros in '033' too? – Sopalajo de Arrierez Dec 6 '14 at 23:26
  • 1
    Because \033[ is not colour code, it is escape code which is interpreted by bash as "from now on till m all numbers will be interpreted as colour codes separated by ;". – jimmij Dec 6 '14 at 23:30
  • 1
    @SopalajodeArrierez A leading zero is part of the conventional syntax for denoting octal notation, and octal is what is required here. 033 is 27 in decimal (3 * 8 + 3), not 33. – jw013 Dec 6 '14 at 23:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.