优化终端体验

Contents
  1. 1. 使用 Zsh
  2. 2. 快捷用法
  3. 3. Mise
  4. 4. fzf
  5. 5. bat
  6. 6. fd
  7. 7. uv
  8. 8. AI 工具

本文作为笔记,以便部署新环境时使用。

使用 Zsh

1
2
sudo apt install zsh
chsh -s $(which zsh)

重新登录后,安装 oh-my-zsh:

1
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

安装主题和插件:

1
2
3
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

修改配置:

1
vim ~/.zshrc

取消如下注释:

1
2
# If you come from bash you might have to change your $PATH.
export PATH=$HOME/bin:$HOME/.local/bin:/usr/local/bin:$PATH

修改主题:

1
ZSH_THEME="powerlevel10k/powerlevel10k"

添加插件:

1
2
3
4
5
6
7
8
plugins=(
git
zsh-autosuggestions
zsh-syntax-highlighting
z
sudo
extract
)

使之生效:

1
2
source ~/.zshrc
p10k configure

快捷用法

  • z 目录名的一部分:自动切换至匹配的高频目录
  • d:列出最近访问过的目录列表,然后输入数字即可跳转
  • ..:等同于 cd ..
  • ...:等同于 cd ../..
  • -:返回上一次所在的目录
  • Ctrl + F:标向右移动,接受自动建议
  • Alt + F:一次只接受一个单词,而不是整句
  • 按两下 ESC:自动加 sudo
  • 输入 cd /u/l/b 然后按 Tab:自动补全为 cd /usr/local/bin/
  • x filename:自动判断格式并用正确的命令解压

Mise

安装:

1
2
3
4
5
6
7
8
9
curl https://mise.run | sh

# 再按程序提示运行 eval 命令

source ~/.zshrc
mise doctor
mise use --global [email protected]
mise use --global node@lts
mise use --global pnpm@latest

fzf

安装:

1
2
mise use -g fzf@latest
source <(fzf --zsh)

用法:

  • Ctrl + R:搜索 history
  • Ctrl + T:快速把文件路径填入命令行
  • Alt + C:快速切换目录,搜索并确认
  • **
    • kill -9 ** 然后按 Tab:会列出所有进程,搜索名字,回车,PID 自动填入
    • export ** 然后按 Tab:fzf 会列出当前所有环境变量
    • cd ** 然后按 Tab:搜索并选择目录

bat

安装:

1
2
3
sudo apt install bat
mkdir -p ~/.local/bin
ln -s /usr/bin/batcat ~/.local/bin/bat

设置别名:

1
vim ~/.zshrc

添加:

1
alias bat="bat --style=numbers --color=always"

使之生效:

1
source ~/.zshrc

fd

find 替换为 fd 可提高查找速度:

1
2
3
4
sudo apt install fd-find
mkdir -p ~/.local/bin
ln -s $(which fdfind) ~/.local/bin/fd
vim ~/.zshrc

末尾添加:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# ====================================================
# FZF Performance Fix (Switch to fdfind)
# ====================================================

# 1. Use fdfind for Ctrl+T and default search
export FZF_DEFAULT_COMMAND='fdfind --type f --hidden --follow --exclude .git --exclude node_modules'
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"

# 2. Use fdfind for Alt+C (directory navigation)
export FZF_ALT_C_COMMAND='fdfind --type d --hidden --follow --exclude .git --exclude node_modules'

# 3. Use fdfind for '**' completion
_fzf_compgen_path() {
fdfind --hidden --follow --exclude ".git" --exclude "node_modules" . "$1"
}
_fzf_compgen_dir() {
fdfind --type d --hidden --follow --exclude ".git" --exclude "node_modules" . "$1"
}

使之生效:

1
source ~/.zshrc

uv

安装:

1
curl -LsSf https://astral.sh/uv/install.sh | sh

AI 工具

安装:

1
2
mise use --global npm:@openai/codex@latest
curl -fsSL https://claude.ai/install.sh | bash
Mastodon