Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

The meat

In (some) terminals you can use a sequence like this;

printf "\e]4;3;rgb:cc/78/33\e\\"

to set the third color in the ANSI color palette to the RGB values represented by #CC7833. Can you do the opposite? Can I get the current RGB value (in any form) of the third (well, any really) color?

Why?

The reason you'd want to do this, in my case, is to be able to save the entire color table before messing it up (on purpose). I want to be able to list code in the terminal using an RGB exact color theme, call me anal. To do that I have to override the color definitions in the terminal color table, as per above.

For me the problem is now solved. But since I want to add this functionality to an existing open source project (pry) used by a lot of people I need to be able to save the current state of the color palette before setting the theme colors for the programs session and then switch them back after.

I know some terminals have this in settings and some systems have it in config files. But I need something that is general and would work across systems. Ideally all the systems that support setting them in the way above :)

Bonus

Right now the changing of colors affect the entire system, all terminal sessions, even after closing and restarting the terminal (this is using iTerm2 on OS X Mountain Lion).

If the effect could be made local, say to a sub shell, the problem would go away since the changes would die with the sub shell when the process ended. Even better since it would protect against cases where the session crashed...

share|improve this question

2 Answers 2

The closest to a standard method for this that I can think of would be to rely on xrdb. You could use xrdb -query and get the color values used by the current X session. For example, mine provides the following:

*background:    #151515
*color0:    #101010
*color1:    #E84F4F
*color10:   #A0CF5D
*color11:   #F39D21
*color12:   #4E9FB1
*color13:   #8542FF
*color14:   #42717B
*color15:   #dddddd
*color2:    #B8D68C
*color3:    #E1AA5D
*color4:    #7DC1CF
*color5:    #9B64FB
*color6:    #6D878D
*color7:    #dddddd
*color8:    #404040
*color9:    #D23D3D
*foreground:    #D7D0C7
...

Unfortunately, due to the way xrdb works, I'm not aware of any way to only set settings for a sub-shell, as the entire point of xrdb is to coordinate settings for the current x-session. However, you could "hack" around it by exporting the settings via xrdb -query and then reloading them when you are done.

share|improve this answer
    
This is a viable option only on systems that use X11 or the same set of tools. It won't work on OS X or other Linux display servers such as Wayland or Mir ... –  d-Pixie Oct 15 '13 at 14:00
    
Ah, yes. I'm sorry -- I assumed OS X used X for some reason. –  tgood Oct 15 '13 at 18:19

The classic way I used was to define a set of xrdb properties for a new class --- like that: (in linux you have to put it in $HOME/.Xdefault-hostname, YMMV)

MYXTerm*foreground: red;
MYXTerm*color11: navyblue;

and if necessary (not sure, depends on the system) loading the file using xrdb -merge. After that, starting a terminal with:

xterm -class MYXTerm

Now you should have a terminal with a set of private colors that do not affect the normal ones . Now, if what you want is to run your program into the same terminal, I really do not know how to do it...

share|improve this answer
    
This is a viable option only on systems that use X11 or the same set of tools. It won't work on OS X or other Linux display servers such as Wayland or Mir ... –  d-Pixie Oct 15 '13 at 14:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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