You are not logged in.

#1 2004-01-17 10:11:59

jlowell
Member
Registered: 2003-08-10
Posts: 270

Changing Color Of Console Cursor

I've searched for about five hours trying to uncover the method of changing the color of the console cursor
in Linux. Specifically I'm not trying to change the color of the cursor in terminals, I know how to do that, but I don't know how change them in the console. I've looked at the How To on the bash prompt which cover cursor movement but says nothing about color. I've seen a blinking green cursor in a console in one of the source distros but have no idea of how that was done. Is this a variable set in /etc/profile or ~/.bashrc? Can anyone help?

jlowell

Offline

#2 2004-01-17 10:25:46

RdsArts
Member
Registered: 2003-10-17
Posts: 32

Re: Changing Color Of Console Cursor

You'd want to put something like [33[01;32m] at the end of your PROMPT or PS1 variable. The important numbers there are 01 and 32. Those are just regular ANSI color codes. If you need a list of them, you can find one here.

Edit: or, I could have been thinking of something completely different. tongue:) I know in Xterm you can set it by sending a -cr colorname option, if that helps any. ^^;

Offline

#3 2004-01-17 17:30:14

jlowell
Member
Registered: 2003-08-10
Posts: 270

Re: Changing Color Of Console Cursor

RdsArts,

Thanks for the reply.

Adding in [033[32m] is an interesting suggestion but, unfortunately it doesn't work because all subsequent text has it's color changed as well. What is interesting about your suggestion, however, is that the cursor is, in fact, changed in the process. Clearly, bash treats the cursor as a text item - as least to this extent it does - so we're making some progress. I've tried out a couple of variations on the theme in an attempt to change only the prompt and the cursor and then escape to normal light gray text but have not succeeded. Here's my PS1:

PS1="[033[32m][u@h W]\$ [33[0m]"

This entry produces the following in green:

[root@Arch1 root]#

If I add [033[32m]" right after $ and remove the following [033[0m]", as I say, while the cursor becomes green, so does all subsequent text. Trying to isolate the change to the cursor without changing the text color by adding back in the final [033[0m]" doesn't work either and, if I try other variations, at least the ones I suspect might work, I get EOF complaints and the prompt reverts to the standard, default bash prompt.

So close but so far. Any suggestions as to syntax?

jlowell

Offline

#4 2004-01-17 21:56:03

scottro
Member
From: NYC
Registered: 2002-10-11
Posts: 466
Website

Re: Changing Color Of Console Cursor

Well, it would depend what colors you actually wanted.
export PS1='[33[01;31m]h [33[01;34m]W $ [33[00m]'

Should give you a rather colorful prompt--it's based on Gentoo's default user prompt.

export PS1='[33[01;32m]u@[33[1;34m]h [33[01;36m]W $ [33[00m]'

Note the syntax, and you can fool around with the colors..  (The only difference between the one just above and the first one is making a couple of things bold.)

That gives username@machinename and the working directory--only the first part of the machine name and only the basename of the working directory.

For example, if I'm scottro on a machine called scottro11.homeunix.net and in the directory /var/abs/unofficial it will show

scottro@scottro11 unofficial $
scottro@ is in green scottro11 in blue abs in cyan and the prompt in cyan.  Note the spaces as well (there is also a space between the $ prompt and a command that is typed.

Hope this helps.

Scott

Offline

#5 2004-01-17 22:15:14

jlowell
Member
Registered: 2003-08-10
Posts: 270

Re: Changing Color Of Console Cursor

scottro,

Thanks for your reply.

Specifically, we're talking about changing the color of the cursor in this instance. Can you point me to the portion of your PS1= above that effects that part of things?

I look forward to hearing from you.

jlowell

Offline

#6 2004-01-17 23:11:29

scottro
Member
From: NYC
Registered: 2002-10-11
Posts: 466
Website

Re: Changing Color Of Console Cursor

export PS1='[33[01;32m]u@[33[1;34m]h [33[01;36m]W $ [33[00m]'

Ok, if you just wanted say the cursor in bold cyan but still wanted all that information

export PS1='u@h W[33[1;36m] $ [33[00m]'

That will make the prompt cyan--if you want it blue then change the 36m to 34m, if you want it green then change it to 32m.

Here's what's happening, more or less--my memory, however, is a little hazy so someone correct mistakes.

PS1='
That part's obvious.  It begins with a single quote.
Then, various things that I want indicated, user, hostname (only the first part and working directory.  The u is user, the h is hostname (only the personal name--hrrm, that ~can't be the right word, but I mean the first part of the FQDN (Fully Qualified Domain Name).
Now, the color stuff begins and this is where my memory is hazy.  I believe the [33 part indicates that you're doing something with the colors---note that you have to escape the brackets with backslashes.  So, we start indicating there will be color with [33
Then the [1;36m 
That part indicates which color--I forget all the codes, looked this stuff up a long time ago and settled upon what I like.  I just know the green blue and cyan because I use them in my prompts.   1;31m is red.  Then we end the color with another ] and the $ for the prompt.  I put spaces in, you might choose to do so or not.  I like a space between the prompt and command.  Then, we need to close the colors with another [33[00m]'

(Don't forget the single quote at the end.)

One way to fool around with it is to make a little file called, for example, ps1.txt

u@h W[33[1;31m] $ [33[00m]

Note there are no quotes in that.

Now you can just do

PS1=`cat ps1.txt`

Note that those are backticks, the little mark under the ~, not single quotes.  That particular one will give you a red $.  You can edit your ps1.txt file for instance trying 1;31m 1;32m etc to see what you like, and rather than having to type the whole thing each time, just do that
PS1=`cat ps1.txt` each time you want to try a new color.  So, you just have to change a number in your ps1.txt file. (I'm very lazy and hate typing--if I were really playing with it, I'd probably make a script to do that called ps1x or something, which would simply read
PS1=`cat ps1.txt`

However, that's being quite lazy.

Sorry for the long winded explanation, but hopefully it helped you.  If I told you many things you already know, I apologize, but maybe some newcomer, browsing archives will find it helpful.

Scott

Offline

#7 2004-01-18 00:08:52

jlowell
Member
Registered: 2003-08-10
Posts: 270

Re: Changing Color Of Console Cursor

scottro,

While I'm certainly grateful to you for all the time it must have taken you to post the above, I'm sorry to report that

PS1= "[33[32m][u@h W[033[32m]\$ [33[0m]"

does not produce [root@Arch1 root]# followed by a blinking, green cursor as I'd hope it would. I don't think we're getting anywhere on how and where to place the necessary syntax for the cursor and, believe me, I've tinkered around with what I've thought might be decent candidates for them for the better part of twelve hours now. I know this result can be achieved but I'm not sure that its done via the PS1 variable. Any other thoughts; anyone else with thoughts?

jlowell

Offline

#8 2004-01-18 00:22:20

scottro
Member
From: NYC
Registered: 2002-10-11
Posts: 466
Website

Re: Changing Color Of Console Cursor

Y'know, I'm sorry. I realize now you're talking about the cursor and I was thinking about the prompt--you're quite right.  I'm not sure how one would do the cursor, interesting question though.

[Added a few minutes later]
I believe it would be something in termcap, but have no idea what.  I did a quick search through deja and didn't come up with anything

Offline

Board footer

Powered by FluxBB