You are not logged in.

#1 2015-09-17 18:01:58

Storm
Member
From: Hungary
Registered: 2014-09-12
Posts: 147

[SOLVED] What's wrong with my bashrc?

Hi!

So I created my own bashrc, primarily for a shiny colored prompt. I took a well known example by Thomas ( https://github.com/thomas-holmes/dotfil … er/.bashrc ) and changed it a bit. However, something went wrong, and now I have the following visual issues:

- When I type a command longer than a line, characters appear in wrong places, line break is incorrect, and cursor is not visually the same place it technically is.
- Using the UP cursor key to go through command history will often result in the first few characters if a history line getting stuck at the end of my promt, command being entered after it. *
- When using command history or long commands, lines randomly get misplaced, or ofsetted


All of these are visal only, they do not affect command input, however, they often make it hard or impossible to tell what am I exactly inputting. Also, it's extremely frustrating.

So could some of you gurus have a look at my bashrc and try to find the problem?

http://pastebin.com/wiyGqS8z


NOTE: I broke up my prompt formatting into several lines using line breaks escaped with \ because it was extremely painful for me to decode what I see, and this way I can see which part does what, as it corresponds to the lines of comments above. This did not seem to make a difference, I tried removing the line breaks and it didn't fix it. Removing all custom code from my bashrc fixes it tho, so the problem is in there somewhere.



* For ex.:
$ |
$ going back in history|
$ another command history entry|

Then my promt gets stuck like this:

$ anoth|

Typing then "sudo" will look like this:

$ anothsudo|

Last edited by Storm (2015-09-18 03:27:46)


"Sic itur ad astra per aspera."

Offline

#2 2015-09-17 18:43:58

brebs
Member
Registered: 2007-04-03
Posts: 3,742

Re: [SOLVED] What's wrong with my bashrc?

PS1 is difficult to get right. Try this nightmare in ~/.bashrc:

export PS1="\[\e[36;1m\]8-\[\e[33;1m\]\u:\[\e[34;1m\]\w\`if [ \$? = 0 ]; then echo -e '\\[\\e[01;32m\\]\\$'; else echo -e '\\[\\e[01;31m\\]\\$'; fi\`\[\e[0m\] "

Notice that it's double-escaped when *within* code.

Last edited by brebs (2015-09-17 18:45:23)

Offline

#3 2015-09-18 00:43:04

Storm
Member
From: Hungary
Registered: 2014-09-12
Posts: 147

Re: [SOLVED] What's wrong with my bashrc?

Ohhh! I like the idea of changing the color of your $ based on return value instead of adding an extra bit upon negative return like in mine. I would actually love to modify mine this way.

But first we should find out what causes my visual glitches. Replacing my PS1 with yours fixed it so now we know the error is within that part.


P.s.: Is there a term to the character that indicates if you are user or root?


"Sic itur ad astra per aspera."

Offline

#4 2015-09-18 00:47:21

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED] What's wrong with my bashrc?

Storm wrote:

Replacing my PS1 with yours fixed it so now we know the error is within that part.

This is why copy/pasting other people's configs, especially complex ones, is a bad idea.

The escaping is broken in the prompt. You can either meticulously comb through it and fix it or, preferrably, start with a simpler prompt and build in "functionality" piece by piece to avoid breakage.
The wiki page has details.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#5 2015-09-18 00:58:10

Storm
Member
From: Hungary
Registered: 2014-09-12
Posts: 147

Re: [SOLVED] What's wrong with my bashrc?

brebs wrote:
export PS1="\[\e[36;1m\]8-\[\e[33;1m\]\u:\[\e[34;1m\]\w\`if [ \$? = 0 ]; then echo -e '\\[\\e[01;32m\\]\\$'; else echo -e '\\[\\e[01;31m\\]\\$'; fi\`\[\e[0m\] "

OK I integrated thhe part changing color of the $ based on return. That part works fine, but I still have the visual glitches. Now my prompt looks like this:

        ### Setting the prompt
        # BOLD> BLUE> [
        # If root RED>
        # Else GREEN>
        # user@home BLUE> ] (dir/dir)
        # If error RED> Else BLUE> $
        # If root RED> Else RESET>
PS1="\033[01;34m\][\
$(if [[ ${EUID} == 0 ]]; then echo '\[\033[31m\]';\
else echo '\[\033[32m\]'; fi)\
\u@\h\[\033[34m\]] (\w) \
\`if [ \$? = 0 ]; then echo -e '\\[\\e[01;32m\\]\\$'; else echo -e '\\[\\e[01;31m\\]\\$'; fi\`\
$(if [[ ${EUID} == 0 ]]; then echo '\[\033[00;91m\]';
else echo '\[\033[00m\]'; fi) "

"Sic itur ad astra per aspera."

Offline

#6 2015-09-18 01:02:43

Storm
Member
From: Hungary
Registered: 2014-09-12
Posts: 147

Re: [SOLVED] What's wrong with my bashrc?

jasonwryan wrote:

This is why copy/pasting other people's configs, especially complex ones, is a bad idea.

The original I linked in my opening post works fine, the glitches appeared when I modified it.

And I can't build it from scratch because the whole thing is cryptic to me. I understand some of it, but not much so I couldn't do anything but to rely on examples I found.


"Sic itur ad astra per aspera."

Offline

#7 2015-09-18 01:20:29

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,571
Website

Re: [SOLVED] What's wrong with my bashrc?

You're not escaping the very first color code - worse yet you have the closing bracket, but not the open bracket.  And for some reason you use [[ ]] conditionals in some places and [ ] in others and $() in one place and `` in another.  If you don't know what you are doing, don't do it.  Learn.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#8 2015-09-18 03:20:21

Storm
Member
From: Hungary
Registered: 2014-09-12
Posts: 147

Re: [SOLVED] What's wrong with my bashrc?

Trilby wrote:

You're not escaping the very first color code - worse yet you have the closing bracket, but not the open bracket.

That's it! Actually, I just figured that out by trial and error a few minutes ago, so I came to tell I fixed it. tongue But thank you very much anyway!

Trilby wrote:

And for some reason you use [[ ]] conditionals in some places and [ ] in others

That's how it is in the original I started with, which is recommended by many people, and even the Arch wiki.

Trilby wrote:

and $() in one place and `` in another.

The `` line was taken from berbs' example above. I tried replacing `` with $() and it DOES NOT work. So there clearly is a difference.

Trilby wrote:

If you don't know what you are doing, don't do it.  Learn.

That's an oxymoron. How am I supposed to learn something without doing? big_smile With that approach I would still be sitting on Winshit 7 and reading articles about Linux instead of actually using it for a few years now. Actually, if I didn't take a leap of faith several times in my life, none of the things I'm truly proud of in my life would have been achieved. Sure, it's a hard road and has its risks, but life is boring without some adventure. wink


"Sic itur ad astra per aspera."

Offline

#9 2015-09-18 07:46:14

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [SOLVED] What's wrong with my bashrc?

Storm wrote:
Trilby wrote:

And for some reason you use [[ ]] conditionals in some places and [ ] in others

That's how it is in the original I started with, which is recommended by many people, and even the Arch wiki.

Can you back that up with at least one link to such a recommendation? Preferably the ArchWiki one, because I know there's nonsense out there like "use one for strings and the other for numbers".

Trilby wrote:

and $() in one place and `` in another.

The `` line was taken from berbs' example above. I tried replacing `` with $() and it DOES NOT work. So there clearly is a difference.

Currently you have two unescaped uses of $() and one escaped use of ``. If you changed it not only from `` to $() but also from escaped to unescaped, that's the clear difference.

Last edited by Raynman (2015-09-18 07:48:54)

Offline

#10 2015-09-18 09:41:52

txtsd
Member
Registered: 2014-10-02
Posts: 97
Website

Re: [SOLVED] What's wrong with my bashrc?

Storm wrote:
Trilby wrote:

If you don't know what you are doing, don't do it.  Learn.

That's an oxymoron. How am I supposed to learn something without doing? big_smile With that approach I would still be sitting on Winshit 7 and reading articles about Linux instead of actually using it for a few years now. Actually, if I didn't take a leap of faith several times in my life, none of the things I'm truly proud of in my life would have been achieved. Sure, it's a hard road and has its risks, but life is boring without some adventure. wink

My man.

I made the move to linux last September because Windows would use over 1.5GB RAM at boot just for the OS, and I only had 3GB RAM. My setup on Arch gives me 250MB on boot along with all my basic applications, excluding Chrome. The amount I've learned and the mount there is to learn is amazing, and everyday is an adventure [8]

Everyday I read /r/linux, /r/android, /r/programming and Hacker News, and I feel like a noob amongst pros.


[CPU] AMD Ryzen 5 2400G
[GPU] AMD Radeon RX 7600 XT
[Kernel] linux-zen
[sway] • [zsh] • Arch user since [2014-09-01 02:09]

Offline

#11 2015-09-18 11:10:07

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,571
Website

Re: [SOLVED] What's wrong with my bashrc?

Storm wrote:

That's an oxymoron. How am I supposed to learn something without doing?

First, it is unequivocally not an oxymoron.  I think you mean to suggest that it is a paradox, dillema, or catch 22.  These would be sensible claims, but I would still disagree: you learn by doing not by copying and pasting what someone else did.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

Board footer

Powered by FluxBB