每一个 programmer 总是想要一个“酷炫的终端”,大一上的时候望着复杂的 Windows Terminal 美化教程只能放弃(美化需要用到 CSS,不会前端),虽然现在也基本不会,但依然可以造一个,通过尝试开(费)箱(时)即(费)用(力)的轮子,
实现酷炫的装B特效
打造酷炫终端(一)——Shell 配置
全过程肯定需要Github
,考虑下怎么办吧(提示:见 clash 的文章)
oh my zsh
安装 zsh
安装 zsh
sudo apt update
sudo apt install zsh
(注:还是别切到 root 去了,就正常用户安装吧)
安装 oh my zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
安装完成后看到 oh my zsh
的 logo
以下就是 oh my zsh
的原生效果:
切到 root 后再换回普通用户貌似效果又没了,又重新执行了一遍安装脚本,鼓捣了一阵子
安装插件
插件分为两类,一类是需要下载的,传送门;另一类是 oh my zsh
自带的,配置文件直接启用即可
zsh-syntax-highlighting
语法高亮插件,安装方法具体见链接
开启全局用户的方法:
注意:这里的 make
前需要 sudo
zsh-autosuggestions
输入命令时提供建议(历史命令)的插件,安装方法具体见链接
autojump
快速跳转文件夹,安装方法具体见链接
这里我遇到了点坑,可能和初次用 zsh 有关,会提示 /usr/bin/env: ‘python’: no such file or directory
,尝试
没有用,解决方法见链接,即,
sudo apt install python-is-python3
效果:
其它插件
默认自带 git,这里我添加了 sudo(<Esc>
两次可以在命令前加上 sudo
) 和 tmux(建议安装,好像可以修复 tmux 下 256 色的 bug)
Powerlevel10k
这是一个炫酷的主题,传送门
安装字体
⭐UPDATE:Nerd Fonts 不止 Powerlevel10k 所推荐的 Meslo Nerd Font,还有很多可以选择!比如你可以选择 Fira Code Nerd Font(目前笔者已经把字体替换为笔者最喜欢的 Fira Code Nerd Font 了),在 ryanoasis/nerd-fonts 你可以找到所有 Nerd Fonts。
下载字体后复制到 C:\Windows\Fonts
完成安装
把 VScode 的终端字体也修改下,其它终端的修改方法仓库上都有
安装 powerlevel10k
交互式配置,很舒服
再改下终端光标样式
可选增加的 batteries
我暂时添加了 CPU
和 RAM
效果
到这里应该有如下效果了:
有运行时间显示,输入命令与显示目录不在同一行,命令输入空间终于不受目录挤压了,喔喔喔喔喔
不过有一点我尝试了好一会没有成功,就是希望在目录比较长的时候切换成相对目录,这主题的目录压缩有点诡异
fishshell
fish is a smart and user-friendly command line shell for Linux, macOS, and the rest of the family.
安装 fish 和 oh-my-fish 以及 ays 主题。
sudo apt install fish
chsh -s /usr/bin/fish
curl https://raw.githubusercontent.com/oh-my-fish/oh-my-fish/master/bin/install | fish
omf install ays
下图是 oh-my-fish 的 ays 主题的效果图,最近在 bandwagonhost 的机子上已入坑,感觉挺清爽酷炫的。
后来我又发现了一个带执行时间显示的更酷的主题 Scorphish,配置步骤如下:
omf install scorphish
omf theme scorphish
不用 Rust 和 Node 可省略接着根据喜好对主题进行基本的配置(这部分可以参加仓库文档),编辑 ~/.config/fish/conf.d/omf.fish
文件,添加如下选项:
set -g theme_display_rust yes # To display current Rust version
set -g theme_display_node yes # To display current Node version
接着该主题有些小 bug,需要进行一些魔改修复,进入到~/.local/share/omf/themes/scorphish/functions
目录,编辑 fish_prompt.fish
文件:
- 去掉多余的
|
。在fish_prompt
函数中,找到_prompt_pwd
函数调用的地方,确保在其后不立即跟随|
字符。if test "$theme_display_pwd_on_second_line" != yes _prompt_pwd # 原来可能是这样的 printf '%s|' $gray,现在去掉 | printf '%s' $gray end
- 修改 ⚡️ 图标的颜色。在
fish_prompt
函数中找到输出 ⚡️ 图标的部分,修改为如下代码:_prompt_versions $blue $gray $green $orange $red # printf '%s] ⚡️ %0.3fs' $gray (math $CMD_DURATION / 1000) printf '%s]' $gray echo -n (set_color yellow)'⚡️' printf ' %0.3fs' (math $CMD_DURATION / 1000) if set -q SCORPHISH_GIT_INFO_ON_FIRST_LINE set theme_display_git_on_first_line end
最后,我们需要让主题支持 autojump,步骤如下:
- Source the autojump.fish file in your fish configuration. 在
$OMF_CONFIG/init.fish
中添加如下代码:begin set --local AUTOJUMP_PATH /usr/share/autojump/autojump.fish if test -e $AUTOJUMP_PATH source $AUTOJUMP_PATH end end
- Add
j
shortcut command to fish. 编辑~/.config/fish/functions/j.fish
文件,添加如下代码:function j set new_path (autojump $argv) if test -d "$new_path" echo $new_path cd "$new_path" else echo "autojump: directory '$argv' not found" echo "Try \`autojump --help\` for more information." false end end
再重启 fishshell 就大功告成了。实际效果如下:
How to install autojump for Fish on Ubuntu
CSDN – oh-my-fish 主题添加执行时间和当前时间
oh-my-zsh 自带主题推荐
What’s the best theme for Oh My Zsh?
笔者现在已经有些看腻了臃肿的 Powerlevel10k 主题😡,想换个更轻量好看的主题。因此,对于 oh-my-zsh 自带主题,笔者希望也能找到一个类似 oh-my-fish 的 ays 主题的。经过挑选,答案就是 oh-my-zsh 的 ys 主题。
编辑 ~/.zshrc
修改 ZSH_THEME="ys"
设置主题为 ys 主题,其原配置如下:
# Clean, simple, compatible and meaningful.
# Tested on Linux, Unix and Windows under ANSI colors.
# It is recommended to use with a dark background.
# Colors: black, red, green, yellow, *blue, magenta, cyan, and white.
#
# Mar 2013 Yad Smood
# VCS
YS_VCS_PROMPT_PREFIX1=" %{$reset_color%}on%{$fg[blue]%} "
YS_VCS_PROMPT_PREFIX2=":%{$fg[cyan]%}"
YS_VCS_PROMPT_SUFFIX="%{$reset_color%}"
YS_VCS_PROMPT_DIRTY=" %{$fg[red]%}x"
YS_VCS_PROMPT_CLEAN=" %{$fg[green]%}o"
# Git info
local git_info='$(git_prompt_info)'
ZSH_THEME_GIT_PROMPT_PREFIX="${YS_VCS_PROMPT_PREFIX1}git${YS_VCS_PROMPT_PREFIX2}"
ZSH_THEME_GIT_PROMPT_SUFFIX="$YS_VCS_PROMPT_SUFFIX"
ZSH_THEME_GIT_PROMPT_DIRTY="$YS_VCS_PROMPT_DIRTY"
ZSH_THEME_GIT_PROMPT_CLEAN="$YS_VCS_PROMPT_CLEAN"
# SVN info
local svn_info='$(svn_prompt_info)'
ZSH_THEME_SVN_PROMPT_PREFIX="${YS_VCS_PROMPT_PREFIX1}svn${YS_VCS_PROMPT_PREFIX2}"
ZSH_THEME_SVN_PROMPT_SUFFIX="$YS_VCS_PROMPT_SUFFIX"
ZSH_THEME_SVN_PROMPT_DIRTY="$YS_VCS_PROMPT_DIRTY"
ZSH_THEME_SVN_PROMPT_CLEAN="$YS_VCS_PROMPT_CLEAN"
# HG info
local hg_info='$(ys_hg_prompt_info)'
ys_hg_prompt_info() {
# make sure this is a hg dir
if [ -d '.hg' ]; then
echo -n "${YS_VCS_PROMPT_PREFIX1}hg${YS_VCS_PROMPT_PREFIX2}"
echo -n $(hg branch 2>/dev/null)
if [[ "$(hg config oh-my-zsh.hide-dirty 2>/dev/null)" != "1" ]]; then
if [ -n "$(hg status 2>/dev/null)" ]; then
echo -n "$YS_VCS_PROMPT_DIRTY"
else
echo -n "$YS_VCS_PROMPT_CLEAN"
fi
fi
echo -n "$YS_VCS_PROMPT_SUFFIX"
fi
}
# Virtualenv
local venv_info='$(virtenv_prompt)'
YS_THEME_VIRTUALENV_PROMPT_PREFIX=" %{$fg[green]%}"
YS_THEME_VIRTUALENV_PROMPT_SUFFIX=" %{$reset_color%}%"
virtenv_prompt() {
[[ -n "${VIRTUAL_ENV:-}" ]] || return
echo "${YS_THEME_VIRTUALENV_PROMPT_PREFIX}${VIRTUAL_ENV:t}${YS_THEME_VIRTUALENV_PROMPT_SUFFIX}"
}
local exit_code="%(?,,C:%{$fg[red]%}%?%{$reset_color%})"
# Prompt format:
#
# PRIVILEGES USER @ MACHINE in DIRECTORY on git:BRANCH STATE [TIME] C:LAST_EXIT_CODE
# $ COMMAND
#
# For example:
#
# % ys @ ys-mbp in ~/.oh-my-zsh on git:master x [21:47:42] C:0
# $
PROMPT="
%{$terminfo[bold]$fg[blue]%}#%{$reset_color%} \
%(#,%{$bg[yellow]%}%{$fg[black]%}%n%{$reset_color%},%{$fg[cyan]%}%n) \
%{$reset_color%}@ \
%{$fg[green]%}%m \
%{$reset_color%}in \
%{$terminfo[bold]$fg[yellow]%}%~%{$reset_color%}\
${hg_info}\
${git_info}\
${svn_info}\
${venv_info}\
\
[%*] $exit_code
%{$terminfo[bold]$fg[red]%}$ %{$reset_color%}"
我对该主题作了如下魔改增强,更加符合我的偏好和喜爱:
- 去除 root 用户的高亮显示。
PROMPT
变量中,%(#,%{$bg[yellow]%}%{$fg[black]%}%n%{$reset_color%},%{$fg[cyan]%}%n)
这部分是用来控制用户名称的显示。其中%(#,%{$bg[yellow]%}%{$fg[black]%}%n%{$reset_color%},%{$fg[cyan]%}%n)
的意思是如果是 root 用户,则用黄色背景和黑色前景显示用户名;如果不是 root 用户,则用青色显示用户名。 - 增加执行命令的计时功能,并把“当前时间”由
PROMPT
移动到了RPROMPT
位置。 -
修改了命令提示符
$
的颜色为绿色。
编辑 ~/.oh-my-zsh/themes/ys.zsh-theme
主题文件,替换为修改后的 ys 主题代码如下:
# Clean, simple, compatible and meaningful.
# Tested on Linux, Unix and Windows under ANSI colors.
# It is recommended to use with a dark background.
# Colors: black, red, green, yellow, *blue, magenta, cyan, and white.
#
# Mar 2013 Yad Smood
# VCS
YS_VCS_PROMPT_PREFIX1=" %{$reset_color%}on%{$fg[blue]%} "
YS_VCS_PROMPT_PREFIX2=":%{$fg[cyan]%}"
YS_VCS_PROMPT_SUFFIX="%{$reset_color%}"
YS_VCS_PROMPT_DIRTY=" %{$fg[red]%}x"
YS_VCS_PROMPT_CLEAN=" %{$fg[green]%}o"
# Git info
local git_info='$(git_prompt_info)'
ZSH_THEME_GIT_PROMPT_PREFIX="${YS_VCS_PROMPT_PREFIX1}git${YS_VCS_PROMPT_PREFIX2}"
ZSH_THEME_GIT_PROMPT_SUFFIX="$YS_VCS_PROMPT_SUFFIX"
ZSH_THEME_GIT_PROMPT_DIRTY="$YS_VCS_PROMPT_DIRTY"
ZSH_THEME_GIT_PROMPT_CLEAN="$YS_VCS_PROMPT_CLEAN"
# SVN info
local svn_info='$(svn_prompt_info)'
ZSH_THEME_SVN_PROMPT_PREFIX="${YS_VCS_PROMPT_PREFIX1}svn${YS_VCS_PROMPT_PREFIX2}"
ZSH_THEME_SVN_PROMPT_SUFFIX="$YS_VCS_PROMPT_SUFFIX"
ZSH_THEME_SVN_PROMPT_DIRTY="$YS_VCS_PROMPT_DIRTY"
ZSH_THEME_SVN_PROMPT_CLEAN="$YS_VCS_PROMPT_CLEAN"
# HG info
local hg_info='$(ys_hg_prompt_info)'
ys_hg_prompt_info() {
# make sure this is a hg dir
if [ -d '.hg' ]; then
echo -n "${YS_VCS_PROMPT_PREFIX1}hg${YS_VCS_PROMPT_PREFIX2}"
echo -n $(hg branch 2>/dev/null)
if [[ "$(hg config oh-my-zsh.hide-dirty 2>/dev/null)" != "1" ]]; then
if [ -n "$(hg status 2>/dev/null)" ]; then
echo -n "$YS_VCS_PROMPT_DIRTY"
else
echo -n "$YS_VCS_PROMPT_CLEAN"
fi
fi
echo -n "$YS_VCS_PROMPT_SUFFIX"
fi
}
# Virtualenv
local venv_info='$(virtenv_prompt)'
YS_THEME_VIRTUALENV_PROMPT_PREFIX=" %{$fg[green]%}"
YS_THEME_VIRTUALENV_PROMPT_SUFFIX=" %{$reset_color%}%"
virtenv_prompt() {
[[ -n "${VIRTUAL_ENV:-}" ]] || return
echo "${YS_THEME_VIRTUALENV_PROMPT_PREFIX}${VIRTUAL_ENV:t}${YS_THEME_VIRTUALENV_PROMPT_SUFFIX}"
}
local exit_code="%(?,,C:%{$fg[red]%}%?%{$reset_color%})"
# Prompt format:
#
# PRIVILEGES USER @ MACHINE in DIRECTORY on git:BRANCH STATE [TIME] C:LAST_EXIT_CODE
# $ COMMAND
#
# For example:
#
# % ys @ ys-mbp in ~/.oh-my-zsh on git:master x [21:47:42] C:0
# $
PROMPT="
%{$terminfo[bold]$fg[blue]%}#%{$reset_color%} \
%{$fg[cyan]%}%n \
%{$reset_color%}@ \
%{$fg[green]%}%m \
%{$reset_color%}in \
%{$terminfo[bold]$fg[yellow]%}%~%{$reset_color%}\
${hg_info}\
${git_info}\
${svn_info}\
${venv_info}\
\
$exit_code
%{$terminfo[bold]$fg[green]%}$ %{$reset_color%}"
autoload -Uz add-zsh-hook
add-zsh-hook preexec preexec
add-zsh-hook precmd precmd
# 修改 preexec 和 precmd,实现毫秒级计时
unction preexec() {
timer=$(date +%s%3N) # 记录当前时间的毫秒数
}
function precmd() {
if [ $timer ]; then
end_timer=$(date +%s%3N) # 命令结束时的毫秒数
timer_show=$((end_timer - timer)) # 计算耗时,单位为毫秒
# 初始化时间单位
local days=$((timer_show/86400000))
local hours=$((timer_show%86400000/3600000))
local minutes=$((timer_show%3600000/60000))
local seconds=$((timer_show%60000/1000))
local milliseconds=$((timer_show%1000))
# 构建时间字符串
local time_string=""
[[ $days -gt 0 ]] && time_string="${time_string}${days}d "
[[ $hours -gt 0 ]] && time_string="${time_string}${hours}h "
[[ $minutes -gt 0 ]] && time_string="${time_string}${minutes}m "
[[ $seconds -gt 0 ]] && time_string="${time_string}${seconds}s "
[[ $milliseconds -gt 0 ]] && time_string="${time_string}${milliseconds}ms"
# 设置 RPROMPT,加入空格
if [[ $timer_show -ge 0 ]]; then # 如果有耗时,则显示
RPROMPT='%{$fg_bold[yellow]%}('${time_string}') %f%{$fg_bold[white]%}[%*]%f'
else
RPROMPT='%{$fg_bold[white]%} [%*]%f'
fi
unset timer
fi
}
效果如下图:
Reference
https://github.com/ohmyzsh/ohmyzsh
https://github.com/zsh-users
https://github.com/ohmyzsh/ohmyzsh/wiki/Plugins
https://github.com/romkatv/powerlevel10k
https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
https://stackoverflow.com/questions/3655306/ubuntu-usr-bin-env-python-no-such-file-or-directory