Automatic window title update - cygwin

Is there a mechanism that could set the title of the cgywin window based on the current value of an environment variable?
I found this small script sometime ago (on SO I think) that allows the window title to be set to a specific string:
echo -ne "\e]2;$#\a\e]1;$#\a";
Background: I have just switched from using Subversion to Perforce (company policy) and find myself repeatedly checking the value of the P4CLIENT environment variable to know what my current workspace is. It would be quite useful to have the title of the cygwin window to automatically contain the current value of that environment variable.

At least if you're using Bash and Mintty, you could check out this post on SuperUser.
So in your case, you could do something like (single quotes changed to double quotes from the original answer since single quotes don't interpolate):
echo -ne "\e]0;$P4CLIENT\a"
You could then add that line into your .bashrc file to make it permanent.

Related

Is there any way I can change only a specific directory color in linux?

I only need to set a color that will only effect a particular directory not globally.
If you are using Bash in a terminal that supports colors and would like to change the default color for text output to the terminal (not specifically the output of ls) while inside this directory, there is one thing you could do.
The PROMPT_COMMAND variable can be set to execute a command just before printing the prompt. You could use that to check for the current directory and change the terminal color (for instance by assigning the PS1 variable with a prompt that contains non-displayable special codes for selecting a color or echoing said special codes).
PROMPT_COMMAND is documented in the Bash manual: https://www.gnu.org/software/bash/manual/bashref.html
As for how to assign colors, you should look that up if interested.
This solution may not be what you are looking for, will probably have side effects, and generally speaking, I am not sure it will be that useful changing terminal colors depending on the current directory. The usual solution is to have a prompt that shows the current directory path (or at least the last portion of the path).
I use something similar to the above to change text color depending on which machine I am executing Bash on, as I use ssh a lot from one machine to another over several terminal windows, and color (on a black background) is a good way to remember what machine any given window is connected to.

Where is PATH variable set in Ubuntu? [duplicate]

This question already has answers here:
How to permanently set $PATH on Linux/Unix [closed]
(24 answers)
Closed 6 years ago.
This is a very interesting question that I stumbled upon when I was creating a command-line application tool for Linux. Unfortunately, the answer on SO is so hidden among the myriad answers to other questions that I decided to ask another question on SO for those who want to modify PATH programmatically.
Grzegorz Żur's answer to another question captures it brilliantly. Unfortunately it was hidden away among many other answers.
There are multiple ways to do it. The actual solution depends on the
purpose.
The variable values are usually stored in either a list of assignments
or a shell script that is run at the start of the system or user
session. In case of the shell script you must use a specific shell
syntax.
System wide
/etc/environment List of unique assignments. Perfect for adding system-wide directories like /usr/local/something/bin to PATH
variable or defining JAVA_HOME.
/etc/xprofile Shell script executed while starting X Window System session. This is run for every user that logs into X Window
System. It is a good choice for PATH entries that are valid for
every user like /usr/local/something/bin. The file is included by
other script so use POSIX shell syntax not the syntax of your user
shell.
/etc/profile and /etc/profile.d/* Shell script. This is a good choice for shell-only systems. Those files are read only by shells.
/etc/<shell>.<shell>rc. Shell script. This is a poor choice because it is single shell specific.
Also, /etc/environment is not a script file, but rather consists of assignment expressions, one per line. Since this file stores the system-wide locale and path settings, it is most oft quoted choice.
Using /etc/profile is not preferred. It exists only to point to /etc/bash.bashrc and to collect entries from /etc/profile.d
User session
~/.pam_environment. List of unique assignments. Loaded by PAM at the start of every user session irrelevant if it is an X
Window System session or shell. You cannot reference other variable
including HOME or PATH so it has limited use.
~/.xprofile Shell script. This is executed when the user logs into X Window System system. The variables defined here are visible to
every X application. Perfect choice for extending PATH with values
such as ~/bin or ~/go/bin or defining user specific GOPATH or
NPM_HOME. The file is included by other script so use POSIX shell
syntax not the syntax of your user shell. Your graphical text editor
or IDE started by shortcut will see those values.
~/.profile Shell script. It will be visible only for programs started from terminal or terminal emulator. It is a good choice for
shell-only systems.
~/.<shell>rc. Shell script. This is a poor choice because it is single shell specific.
For temporary change:
~$ export PATH=$PATH:~/root/scripts
For permanent change, you can add this line to the /etc/environment file like this:
PATH=$PATH:~/root/scripts

How to make GNU screen auto start when login?

I have several linux servers to work on every day, and I have a GNU screen session on each of them to preserve work progress.
The question is I am so tired of issue screen -Rd work every time I login, is there anyways to get rid of that? Or any workarounds?
Reading #Sami's answer, I did some search on the $STY shell variable and found this:
STY: Alternate socket name. If screen is invoked, and the environment variable STY is set, then it creates only a window in the running screen session rather than starting a new session.
So I think the key is the $STY variable, we may append it to either .bashrc or .profile, as long as it gets executed upon login. Thanks #Sami
This depends on your shell. In case you use any of Bourne Shell derivatives (namely Bash) or Bourne Shell itself, put appropriate commands in ~/.profile:
[ -z "$STY" ] && screen -Rd "work"
This will only start a screen session in case you're not already running inside a screen session (screen sets environment variable STY).
In case you're not using Bash or compatible, use the proper shell initialization file with a similar test.
You could put an alias in your login script:
alias s="screen -Rd work"
then you'd only have to type
s

How do I get GNU screen to read .bash_profile/.bash_rc changes?

After I make changes in .bash_rc or .bash_profile, when I start GNU screen, it doesn't recognize those changes.
I can
source ~/.bash_profile
and it works for the current screen window I have open, but I have to do that for every screen window I have open.
How do I get screen to read my latest changes in my bash configuration?
If you want screen to always treat your shell as a login shell, and source the same files that would be read if just started a new shell normally, add the following to ~/.screenrc (or maybe ~/.byobu/.screenrc, as pointed out in the comment):
shell -$SHELL
This way, you don't need to manually tell it to source your files each time you start a new screen. Though you would have to if you just made changes and wanted those changes to be reflected in your current screen.
The documentation for this (and lots of other screen details) can be found here. Basically, shell is a command to screen telling it to run the following when it needs to create a new shell. $SHELL is the usual variable holding the path to your preferred shell. And the dash - in front of $SHELL indicates that it should be run as a login shell (which will typically mean it sources your ~/.bash_profile, etc.).
It's worth pointing out, however, that screen defaults to just inheriting most environment variables from the shell where you start screen; and a login sub-shell may alter some environment variables in unexpected ways. I ran into a situation where elements of my $PATH were basically permuted. I solved the problem thanks to this particularly excellent answer on superuser.
You may notice the source command available. It's important to note that this sources a file of screen commands, rather than shell commands. Other relevant (screen) commands include eval and exec.
You have to do it in each screen that you have open since they are all different shells. If you need the change every time a new shell is opened, I suggest you put the changes in ~/.bashrc instead.
Apparently, you can send a command to all windows at once using this syntax:
C-a :
at "#" stuff "source ~/.bash_profile^M"

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
Go to Settings | Edit Current Profile... | Appearance
Define new Color Scheme for each of the directories you want to have special background for, e.g. myprofile1, myprofile2, mystandard
Make sure you can manually call konsoleprofile ColorScheme=myprofile1, konsoleprofile ColorScheme=standard, etc.
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"

Resources