bash 以编程方式更改 Gnome 终端主题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/974599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Gnome terminal theme programmatically
提问by pufferfish
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.
我想在我的本地机器(Ubuntu GNOME)上创建一个设置,根据我是登录到本地机器还是通过 ssh 连接到远程机器,终端窗口具有不同的背景颜色。
Is there a way to do this?
有没有办法做到这一点?
采纳答案by Michiel Buddingh
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.
您可以修改您的.bashrc(或等效的 shell init 文件)以根据您是否使用 ssh 来设置提示。
i.e. put something like:
即把类似的东西:
if [ -n $SSH_TTY ]; then
export PS1=`echo -en '3[42m\w$ '`;
fi;
at the end of your .bashrcfile on the remote machine. the \033[42mis an ANSI Escape Codethat changes the background colour to green.
在.bashrc远程机器上的文件末尾。这\033[42m是一个ANSI 转义码,它将背景颜色更改为绿色。
This way, the background colour of your terminal will be green (or magenta, or cyan, or whatever) onlywhen you're logged in to a remote machine.
这样,只有当您登录到远程计算机时,终端的背景颜色才会是绿色(或洋红色、青色或其他颜色)。
回答by Aiden Bell
You might want to checkout the options to gnome-terminal:
您可能想查看 gnome-terminal 的选项:
gnome-terminal --help
gives
给
--window-with-profile=PROFILENAME
Wrap this in a shell script:
将其包装在 shell 脚本中:
#!/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).
如果您想更改更多,请查看 aterm 和其他术语。还可以查看Devilspie,它可以根据窗口标题(删除窗口装饰等)等内容进行更多动态更改。
回答by trendels
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:
您可能想看看GConf。对于 Gnome 来说,注册表对于 Windows 来说基本上是一样的。大多数 Gnome 应用程序使用它来存储他们的设置。您可以使用GConf-Editor等工具浏览它,或使用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.
您将在此处找到可通过“首选项”对话框访问的所有设置,以及更多设置。也可以使用 更改键--set,man gconftool-2有关详细信息,请参阅“ ”。
There are also GConf library bindings for many programming languages.
还有许多编程语言的 GConf 库绑定。
回答by xyrix
I have some scripts which achieve this purpose for gnome-terminal. You can find them at https://github.com/xyrix/gnome-terminal-profile-switcher
我有一些脚本可以为 gnome-terminal 实现这个目的。您可以在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.
这允许您从脚本更改当前 gnome 终端的配置文件。
Included in the repository is an example "safe_ssh" script to demonstrate the usage.
存储库中包含一个示例“safe_ssh”脚本来演示用法。
回答by al.
export PROMPT_COMMAND='echo -ne "3]0;${HOSTNAME}: ${PWD}##代码##7"'

