1

I added a battery function to my vim statusline here's a link. It displays my battery correctly in vim and if I run $ acpi | grep -oP "(\d+)%" it returns some number followed by the percent sign (as expected). I decided to update my zshrc since it prints better than the cut I was using:

bat () { acpi | grep -oP "(\d+)%" }
PROMPT="%t | $(bat) | %d
$ "

However, what I end up with is:

7:22PM | 89[m | /home/rain
$ 

How do I get the [m to print as a %? Or can I drop it altogether and just have the digit print?

  • You appear to have mis-transcribed into the question what you are setting PROMPT to. – JdeBP Oct 18 '18 at 5:48
  • What part is appearing as mis-transcribed? – 345422 Oct 18 '18 at 10:52
  • %(bat) does not perform command substitution and does not produce the claimed effect (it rather producing a far more drastic one), so you haven't correctly transcribed the part that does the command substitution. – JdeBP Oct 18 '18 at 11:24
  • Oh. Yes. The % should be a $. Thank you! – 345422 Oct 18 '18 at 11:54
3

The [m is probably the tail end of an ANSI escape code \e[m (= reset to normal colors & attributes, short form) with the initial ESC character (\e) stripped off.

My guess would be that your grep is coloring matching strings when run interactively, and the interactivity detection is misfiring when run as part of a shell function whose output becomes part of the prompt.

The percent sign in the output triggers special processing in prompts, and if %\e does not mean anything it is simply deleted.

Try defining your bat function like this:

bat () { acpi | grep --color=never -oP "(\d+)%" }
  • Alright. So I changed the function to that and the PROMPT to ... | $(bat)% | ... and it looks exactly how I was hoping it would. Also, that fixed the other problem I was having and now the prompt is all one color. Thanks. – 345422 Oct 18 '18 at 10:58

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.