English 中文(简体)
How to change colors programmatically in Konsole based on current directory?
原标题:

I currently use a color scheme based on which directory that I m working in. I manually open up a Konsole shell and then cd into a directory and got to Settings and change the color scheme.

What I would like to do is have Konsole automatically set its foreground and background colors based on which directory I m in. Basically if I m in any subdirectory below /home/me/src/java then I would like to use text white, background blue, for example. If I m below /home/me/src/documentation I want text black, background white, for example. I would like the color change to occur automatically, programmatically, when I call the "cd" command.

Is this possible? If so, can you provide me some direction as to how?

The way I see it I will need to be able to do a couple of things:

  • Be able to detect which Konsole the shell process (bash) is running in.
  • Be able to tell Konsole, probably via API call, to change the color scheme on the fly.
最佳回答

To get you started, here s a little information about using D-Bus to script Konsole.

You don t say which shell you re using, but if it s Bash you may want to use the $PROMPT_COMMAND variable which holds a command to be executed each time the $PS1 prompt is issued. The Z shell has a similar facility that s probably a bit more powerful (see man zshmisc for chpwd and precmd).

Otherwise, you might be able to use xterm escape sequences.

问题回答

Context

Actually, Konsole has support for what they profiles. A profile is a group of settings (not only background), which you can manually define under Settings | Manage Profiles and around.

Also, there exists a command line utility called konsoleprofile which allows for programmatic changing of the profiles.

Actual answer

  1. Go to Settings | Edit Current Profile... | Appearance
  2. Define new Color Scheme for each of the directories you want to have special background for, e.g. myprofile1, myprofile2, mystandard
  3. Make sure you can manually call konsoleprofile ColorScheme=myprofile1, konsoleprofile ColorScheme=standard, etc.
  4. Plug in the calls to konsoleprofile into your $PROMPT_COMMAND, e.g. add this to your .bashrc:
PROMPT_COMMAND= [[ "$PWD" = /home/me/src/java* ]] && konsoleprofile ColorScheme=myprofile1 || konsoleprofile ColorScheme=mystandard ";$PROMPT_COMMAND"





相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签