3

I have played around with PS1 and PROMPT_COMMAND in bash to create a zsh-style right side prompt. I have a solution who almost works.

The problem is that if I write a long line of input, the second line overwrites the first one. The third line will appear nicely on a new line.

Maybe some line counter are of-by-one because my cursor movement, or is this a limitation/bug?

A simple example:

export PS1="prompt>\[\033[s\033[10C\]test\[\033[u\]"

Print prompt>, save position, move 10 characters to the left, print test, restore position.

The prompts looks nice and works perfectly, until i write more then one line of text.

Example 1, expected behaviour:

------------------------------------
prompt>          test
prompt>ls        test
files...
prompt>1 2 3 4 5 6 7 8 9 10 11 12 13
14 15 16 17 18 19 20 21 22 23 24 25
26 27 28

Example 2, current behaviour:

------------------------------------
prompt>          test
prompt>ls        test
files...
14 15 16 17 18 19 20 21 22 23 24 253
26 27 28
7

Ah, of course. Test should not be counted as a visible character and should be included between \[ and \].

Working example:

export PS1="prompt>\[\033[s\033[10Ctest\033[u\]"

The reason was because if bash count test as a visible character it will assume it's left if the cursor and the calculation of available characters left on the current line will be off by four characters (length of 'test').

  • I will not accept my own answer if someone else want to give a better and more informative answer who explains the situation in more detail. – nsg Oct 4 '12 at 13:29
  • You pretty much got it - the \[ and \] are hints to bash to tell it how much physical space your prompt takes up, and since the test part is sort of floating outside your main prompt it shouldn't be included. – jw013 Oct 4 '12 at 14:13
  • @nsg You may want to accept your answer, there is nothing wrong with that and it will prevent your question from floating around as having no accepted answer. – Adrian Frühwirth Oct 18 '13 at 9:01
  • True, I had forgotten this one. Thanks for the reminder. – nsg Oct 25 '13 at 15:22

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.