终端美化教程

作为一个天天用命令行终端的人, 我也确实花了不少时间在这上面.
要美化终端需要一点点 Linux 基础, 因为过程中还会涉及到其他内容.

部分文件以压缩包的形式放在结尾处.

Shell

Zsh

首先把干活的工具给弄好才方便接下来办事!
但其实 zsh 上手难度很高, 更多人是用 ohmyzsh 这个版本.

官网: https://ohmyz.sh/

项目地址: https://github.com/ohmyzsh/ohmyzsh

官方自动安装脚本: sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

不管是用什么方式安装的其实都差不多, 因为 ohmyzsh 其实就是一堆 shell 脚本, 都不涉及到可执行二进制文件.
安装后可以导入这份配置文件, 但暂时还不能用.

$ cat ~/.zshrc
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="powerlevel10k/powerlevel10k"

# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in $ZSH/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment one of the following lines to change the auto-update behavior
# zstyle ':omz:update' mode disabled  # disable automatic updates
zstyle ':omz:update' mode auto      # update automatically without asking
# zstyle ':omz:update' mode reminder  # just remind me to update when it's time

# Uncomment the following line to change how often to auto-update (in days).
# zstyle ':omz:update' frequency 13

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# You can also set it to another string to have that shown instead of the default red dots.
# e.g. COMPLETION_WAITING_DOTS="%F{yellow}waiting...%f"
# Caution: this setting can cause issues with multiline prompts in zsh < 5.7.1 (see #5765)
COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(zsh-autosuggestions zsh-syntax-highlighting z)

source $ZSH/oh-my-zsh.sh

[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

这份配置使用了 powerlevel10k 主题和基础插件并开启了自动更新, 可以说写的非常简单.
如果网络环境不好建议关掉自动更新, 否则登录的时候可能会卡一会.

虽然装的时候会问你一句要不要将它设为默认 shell, 如果有其他想法可以手动切换成其他的. (服务器环境建议保留 bash 手动切, 问就是我服务器 SSH root 密钥登不上发现是指定了一个不存在的 shell.

chsh -l
which <xsh>
chsh -h
chsh -s </path/to/xsh> <username>

powerlevel10k

接下来开始下载这个主题到本地并应用, 这里使用默认的主题目录. 如果有多用户需求可以指定配置文件中 ZSH_CUSTOM 的路径实现共享, 前提是其他配置文件也要指定目录.

cd ~/.oh-my-zsh/custom/themes
git clone https://github.com/romkatv/powerlevel10k.git
source ~/.zshrc

修改主题设置

vim ~/.p10k.zsh

这个主题很庞大, 其实不太好改.

plugins

我只开启了最基础的插件, less is more.

同样的, 把需要的插件放到 ~/.oh-my-zsh/custom/plugins
插件有很多, 具体功能需要去看 on-my-zsh 项目下的插件说明.

  • zsh-autosuggestions
  • zsh-syntax-highlighting 结合上一个实现解放双手
  • z cd 命令增强版

插件有很大一部分是通过 alias 实现的, 但是对我来说太难记了而且命令作用不是很直观.

环境变量

然后是一些常用的环境变量, 直接追加到 ~/.zshrc 结尾就行.

export KUBECONFIG=/etc/kubernetes/admin.conf
export PATH=$HOME/bin:$PATH
export LANG=en_US.UTF-8
export VISUAL=nvim
export EDITOR=nvim
export DOCKER_BUILDKIT=1
export ARCHFLAGS="-arch x86_64"

alias

设置一些常用别名可以大幅提升使用体验, 例如我常用 ip 命令有颜色区分就很容易辨别了.

同样的追加即可.

alias grep='grep --color=auto'
alias diff='diff --color=auto'
alias ip='ip --color=auto'

Logo

命令行的乐趣之一, 而且可以快速查看一些系统信息.

fastfetch

这是一个类似于 neofetch 的 C 语言版, 相比之下快了十倍甚至九倍 (

项目地址: https://github.com/fastfetch-cli/fastfetch

软件源: yay -S fastfetch

由于这是我现在使用的所以着重讲讲. 首先导出默认配置文件.

fastfetch --gen-config jsonc

用外部编辑器去修改配置文件, 可以参考项目地址中的 example.

cd ~/.config/fastfetch
vim config.jsonc

修改这份配置费时费力费脑, 这里提供一份我修改好的.

2023-12-01T16:01:56.png

图标来源: https://www.nerdfonts.com/cheat-sheet

neofetch

最知名的一个软件, 但是用 bash 编写运行效率相对来说很低.

项目地址: https://github.com/dylanaraps/neofetch

软件源: yay -S neofetch

这两个软件都支持 Windows!

screenfetch

曾经用过, 以及一些其他 fetch 就不太清楚了.

项目地址: https://github.com/KittyKatt/screenFetch

增强

如果将 zsh 设为默认 shell 每次登录都会尝试加载 zshrc 文件. 那么最简单的办法就是将 fastfetch 追加到 ~/.zshrc 文件的头部.
注意这里是头部而不是之前的尾部, 如果放在尾部登录时会看到报错之类的.

自定义输出

首先需要去了解 ANSI 转义序列 才能开始修改那些花花绿绿的输出.
长话短说介绍下吧!

终端中不支持输出半透明 / 渐变色 / UTF-16 等较为现代化的东西, 但是前景和背景, 斜体加粗删除线这些都是没问题的.
而我们要做的就是给一些要输出的字符设置这些格式, 例如偶尔能够在浏览器控制台看到的彩色输出.
但是 ANSI 写的格式第一眼看上去可能头大, 可读性极差. 例如下面这行代码为一句话设置了一个简单的前景和背景.

echo -e "\u001b[48;2;255;16;255m\u001b[38;2;0;240;0mHello world!\u001b[0m"

好消息是现在可以使用 RGB 模式来输出更丰富的颜色了, 坏消息是得手动写上面这种东西.

top

用于显示一些实时系统资源信息的, 最基础的就是 top 命令.
其他变种: atop btop htop 等等, 可以尝试在软件源内直接搜 top 关键词, 其实还有很多.
其中 btop 是我用过的最好看的一个, 同样也支持 Windows!

2023-12-05T02:00:33.png
2023-12-05T01:59:44.png

优点: 界面好看, 可以鼠标键盘交互, 不需要 sudo.
缺点: 色彩在部分终端内输出可能会显示异常, 不同终端内表现有明显区别, 终端窗口不能过小并且需要很大的屏幕才能显示更多的信息. 都没有中文
其他变种: 刷新问题, 少数需要 sudo 才能获取完整的信息, 交互问题等.

Font

MesloLGS NF

这是一个使用较为广泛的终端用字体, 将这些字体装到系统上然后在你需要用到终端的软件上配置它.
去这里 下载它们, 或者其他支持图标的字体.

Terminal

Windows 换上 新的 Windows Terminal, 然后指定字体 (字体需要先安装到本地).

设置 > 配置文件 > 默认值 > 其他设置 > 外观 > 文本 > 字体

附件

zsh_config.zip