Is there a way to change settings (e.g. color scheme) in a running tmux session triggered by a certain shell command (e.g. ssh)? - hook

I just found out about the possibility of hooks in tmux. Since I'm still very new to the command line (and therefore not used to (say need time to) read through all relevant parts of a manpage) I just quickly wanted to ask here, if it is possible to use bash commands to trigger (e.g. when I use ssh, I want to change the color of my status bar). (Although I fear it is not possible).
If not, what could be a way to achieve what I want? Can I make an alias for the command in question and 'reload' a different .tmux.conf file (in a current session)?
Thanks for your help!
Edit:
For example:
I have two versions of a program where I source different environmental variables via aliases (say progVersionA and progVersionB).
When I have a tmux session running, I would like the current 'sourced' environment shown in the status bar (say "progVersionA"). And as soon as I switch to the other program the status bar should read "progVersionB". So, what I want is, that the status bar changes as soon as I run the alias progVersionB.

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.

Clean any output of a linux console

I have a linux installation without X. When I launch a third-part application (i.e. gstreamer) it draws on a portion of the screen, let the users see through the external areas.
I want to "clear" the console so it appears black. Of course the clear command won't work because you still see the prompt.
Might you recommend any way to achieve this?
The environment variable which contains the output displayed by the prompt is named PS1. You can empty this variable when needed.
Don't forget to keep a 'backup' of the value in order to be able to set it back to its old value

Check from within bash script that autocompletion is initialized

I'm looking for a method to check from within my shell script that script specific completion have been initialized by user using complete -F ...
I want this check to print out an advice on how to initialize the completion like:
Warning: Auto completion is not initialized. Please run : source ....; complete -F ...
The problem is that the script,being run in a sub-shell has no information about "complete" environment of the parent shell where user is working.
So complete -p| grep my-script-name never return any result.
User is expected to run "source" and "complete" commands or add them into his .bashrc manually, because we're working on a server where we have no access to the bash completion system directory.
Alternatively if you know a method of initializing(and not only checking) the auto-complete from within the script, I would happily accept it.
The only way your script can have access to such information for the parent shell is if it is included instead of executed as a sub-shell. Rather than instructing your users they can include some configuration, you can design your script so it works whether it is run or included.
Then you can simply inform your users that if they want completion enabled they need to include your script rather than run it as an executable script (with instuctions to use source or . as you wish).
However, in this case, I would be inclined to either add this information in documentation or add it into a banner which is always displayed (but can be disabled with an option switch like -q), rather than support two modes of running the script (since the gain is so small).
Either you want every user to use bash completion (which I don't think is good, for example I prefer to have it turned off), or let users decide themselves, but having them this message printed out on each TABTAB is a threat, don't you think?
I'd put into /etc/bash.bashrc what you think every user should run.

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