更新时间:2023-02-08 16:05:39浏览次数:927+次
本文介绍如何在Linux/macOS系统上安装、配置和使用Theme.sh,并用它轻松更改Linux/macOS终端主题的方法。Theme.sh是一个交互式脚本,可以设置终端主题,这个脚本在一个文件中包含大约400个主题,您可以直接设置终端主题,也可以使用fzf(命令行模糊查找器)设置,它提供了一个用于主题选择的终端菜单。
在系统上安装Theme.sh
安装theme.sh是一项非常简单的任务。它需要下载脚本并将其放到路径中的某个位置。
这可以通过以下cURL命令实现:
1、在Linux上
sudo curl -Lo /usr/bin/theme.sh 'https://git.io/JM70M' && sudo chmod +x /usr/bin/theme.sh
2、在OSX上
sudo curl -Lo /usr/local/bin/theme.sh 'https://git.io/JM70M' && sudo chmod +x /usr/local/bin/theme.sh
您可能还需要为交互式shell安装fzf(通用命令行模糊查找器),如下所示:
1、在Linux上
在继续执行以下操作之前,请确保您的系统上安装了git:
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
fzf在Linux软件包管理器中可用,可按如下方式安装:
##在Debian/Ubuntu上
sudo apt-get install fzf
##在Fedora上
sudo dnf install fzf
##在Arch Linux上
sudo pacman -S fzf
##在openSUSE上
sudo zypper install fzf
2、在macOS上
您可以使用Homebrew安装此软件包:
$ brew install fzf
# To install useful key bindings and fuzzy completion:
$(brew --prefix)/opt/fzf/install
使用Theme.sh编辑Linux/macOS终端主题
下面,您就可以轻松更改Linux/macOS终端了。
列出可用主题:
theme.sh -l
样本输出:
列出深色主题:
theme.sh --dark --list
样本输出:
现在切换到所需的主题:
theme.sh gruvbox-dark-medium
样本输出:
您还可以使用交互式shell设置主题:
theme.sh -i
样本输出:
选择后,按Enter键应用主题:
配置theme.sh
在没有进一步配置的情况下使用theme.sh是不持久的,当系统重新启动或打开新的终端窗口时,设置的主题会自动丢失。
为了让theme.sh更改在系统重新启动后仍然存在,我们需要编辑shell并添加如下行:
1]、对于bash/Zsh
打开文件进行编辑:
$ vim ~/.bashrc
##或##
$ vim ~/.zshrc
现在在文件末尾添加以下行:
if command -v theme.sh > /dev/null; then
[ -e ~/.theme_history ] && theme.sh "$(theme.sh -l|tail -n1)"
# Optional
#Binds C-o to the previously active theme.
bind -x '"\C-o":"theme.sh $(theme.sh -l|tail -n2|head -n1)"'
alias th='theme.sh -i'
# Interactively load a light theme
alias thl='theme.sh --light -i'
# Interactively load a dark theme
alias thd='theme.sh --dark -i'
fi
您可能需要获取配置文件:
$ source ~/.bashrc
##或##
$ source ~/.zshrc
2]、对于Fish
vim ~/.config/fish/config.fish
在文件末尾,添加行:
if type -q theme.sh
if test -e ~/.theme_history
theme.sh (theme.sh -l|tail -n1)
end
# Optional
# Bind C-o to the last theme.
function last_theme
theme.sh (theme.sh -l|tail -n2|head -n1)
end
bind \co last_theme
alias th='theme.sh -i'
# Interactively load a light theme
alias thl='theme.sh --light -i'
# Interactively load a dark theme
alias thd='theme.sh --dark -i'
end
您还可以将Vim配置为使用~/.vimrc中的新主题:
colorscheme default
set notermguicolors
highlight Search ctermfg=0
1、sudo/su包装器
当前,您还可以编辑sudo/su包装器。例如,要在运行su/sudo时设置红色警报主题,请在~/.bashrc中添加以下行:
su() {
(
INHIBIT_THEME_HIST=1 theme.sh red-alert
trap 'theme.sh "$(theme.sh -l|tail -n1)"' INT
env su "$@"
theme.sh "$(theme.sh -l|tail -n1)"
)
}
sudo() {
(
pid=$(exec sh -c 'echo "$PPID"')
# If the command takes less than .2s, don't change the theme.
# We could also just match on 'su' and ignore everything else,
# but this also accomodates other long running commands
# like 'sudo sleep 5s'. Modify to taste.
(
sleep .2s
ps -p "$pid" > /dev/null && INHIBIT_THEME_HIST=1 theme.sh red-alert
) &
trap 'theme.sh "$(theme.sh -l|tail -n1)"' INT
env sudo "$@"
theme.sh "$(theme.sh -l|tail -n1)"
)
}
获取配置文件的源代码并尝试运行su/sudo命令。您的终端将显示如下:
当运行sudo命令时,终端将更改为红色警报主题,并在完成后返回默认主题。
2、Theme.sh SSH集成
用于SSH集成:
$ vim ~/.ssh_themes
host1: zenburn
host2: red-alert
在文件中,将host1和host2替换为所需的主机名/IP_addresss,您还可以为主机设置更多的主题,以便在zenburn和red-alert使用。
现在将行添加到~/.bashrc:
ssh() {
# A tiny ssh wrapper which extracts a theme from ~/.ssh_themes
# and applies it for the duration of the current ssh command.
# Each line in ~/.ssh_themes has the format:
# <hostname>: <theme>.
# Restoration relies on the fact that you are using theme.sh to manage
# the current theme. (that is, you set the theme in your bashrc.)
# This can probably be made more robust. It is just a small demo
# of what is possible.
touch ~/.ssh_themes
host="$(echo "$@"|awk '{gsub(".*@","",$NF);print $NF}')"
theme="$(awk -vhost="$host" -F': *' 'index($0, host":") == 1 {print $2}' < ~/.ssh_themes)"
if [ -z "$theme" ]; then
env ssh "$@"
return
fi
INHIBIT_THEME_HIST=1 theme.sh "$theme"
trap 'theme.sh "$(theme.sh -l|tail -n1)"' INT
env ssh "$@"
theme.sh "$(theme.sh -l|tail -n1)"
}
例如,尝试使用红色警报主题集将SSH连接到host2:
3、为主题添加theme.sh
如果theme.sh脚本是可写的,则可以使用–add标志轻松地向其中添加主题。例如,要添加主题,请执行以下操作:
curl -O 'https://raw.githubusercontent.com/dexpota/kitty-themes/master/themes/Solarized_Darcula.conf'
sudo theme.sh --add Solarized_Darcula.conf
设置新主题:
theme.sh Solarized_Darcula
执行输出:
在这里,任何具有类似名称的主题都将被覆盖。