简体   繁体   中英

Change Gnome terminal theme programmatically

I'd like to create a setup on my local machine (Ubuntu GNOME) whereby the terminal window has a different background color depending on whether I'm logged in to my local machine or ssh'd into a remote machine.

Is there a way to do this?

This doesn't do what you asked for, but it probably does what you want.

You can modify your .bashrc (or equivalent shell init file) to set your prompt based on whether you're using ssh or not.

ie put something like:

if [ -n $SSH_TTY ]; then
     export PS1=`echo -en '\033[42m\w\$ '`;
fi;

at the end of your .bashrc file on the remote machine. the \\033[42m is an ANSI Escape Code that changes the background colour to green.

This way, the background colour of your terminal will be green (or magenta, or cyan, or whatever) only when you're logged in to a remote machine.

I have some scripts which achieve this purpose for gnome-terminal. You can find them at https://github.com/xyrix/gnome-terminal-profile-switcher

The script works by creating a temporary profile for all terminals, and then changing the values set in the temporary profile to be copied from your normal profiles.

This allows you to change the profile of the current gnome-terminal from a script.

Included in the repository is an example "safe_ssh" script to demonstrate the usage.

You might want to checkout the options to gnome-terminal:

gnome-terminal --help

gives

 --window-with-profile=PROFILENAME

Wrap this in a shell script:

#!/bin/bash
gnome-terminal --window-with-profile=PROFILENAME

then do

ssh-term

If you want to change more, look into aterm and other terms. Also look into Devilspie which can do more dynamic changes based on things like window title (removing window decorations and so on).

http://burtonini.com/blog/computers/devilspie

You might want to take a look at GConf . It basically is for Gnome what The Registry is for Windows. Most Gnome apps use it to store their settings. You can browse it using tools like GConf-Editor , or from the command line using gconftool-2 :

$ gconftool-2 --all-entries /apps/gnome-terminal/profiles/Default
background_color = #000000000000
palette = #2E2E34343636:#CCCC00000000 [ snipped ]
... many more lines

You will find all settings here that are accessible via the Preferences dialog, plus some more. Keys can also be changed using --set , see " man gconftool-2 " for details.

There are also GConf library bindings for many programming languages.

With Ubuntu Gnome, I use .desktop files to create ssh logins with different terminal colours.

The Terminal colour can be setup on gnome-terminal in the Profiles section. I create a different profile for each background colour that I use.

I create a .desktop file shortcut for each of the server's ssh logins and put these into ~/.local/share/applications/ so that they show up in Gnome's Applications overview.

Here's an example .desktop file for ssh'ing into the localhost as root. The profile is called Red, and is configured with a red background using gnome-terminal's profile settings.

#!/usr/bin/env xdg-open

[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Icon[en_GB]=gksu-root-terminal
Name[en_GB]=LOCALHOST Root
Exec=gnome-terminal --window-with-profile Red --command "ssh -XC root@localhost"
Name=LOCALHOST Root
Icon=gksu-root-terminal

Here's an example of a multiple ssh to a server which opens a tabbed terminal:

#!/usr/bin/env xdg-open

[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Icon[en_GB]=gnome-terminal
Name[en_GB]=MYREMOTESERVER Root
Exec=gnome-terminal --command='ssh -XC root@MYREMOTESERVER'  --window-with-profile Blue --tab-with-profile Blue --tab-with-profile Blue --tab-with-profile Blue --tab-with-profile Blue  --hide-menubar 
Name=MYREMOTESERVER Root
Icon=gnome-terminal

The server is MYREMOTESERVER and the profile being used is called Blue.

This technique also works with the KDE desktop. The desktop shortcuts end up in the lost and found folder.

export PROMPT_COMMAND='echo -ne "\033]0;${HOSTNAME}: ${PWD}\007"'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM