Zsh配置

(10 mins to read)

安装

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

curl -fsSL https://raw.githubusercontent.com/zimfw/install/master/install.zsh | zsh

配置

选择zimfw作为框架,而不是oh-my-zsh,速度更快,也更简单。

.zshrc中添加以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ZIM_HOME=${ZDOTDIR:-${HOME}}/.zim
# Download zimfw plugin manager if missing.
if [[ ! -e ${ZIM_HOME}/zimfw.zsh ]]; then
if (( ${+commands[curl]} )); then
curl -fsSL --create-dirs -o ${ZIM_HOME}/zimfw.zsh \
https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh
else
mkdir -p ${ZIM_HOME} && wget -nv -O ${ZIM_HOME}/zimfw.zsh \
https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh
fi
fi
# Install missing modules, and update ${ZIM_HOME}/init.zsh if missing or outdated.
if [[ ! ${ZIM_HOME}/init.zsh -nt ${ZDOTDIR:-${HOME}}/.zimrc ]]; then
source ${ZIM_HOME}/zimfw.zsh init -q
fi
# Initialize modules.
source ${ZIM_HOME}/init.zsh

再创建.zimrc,该文件每行是一个zmodule,即一个插件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Start configuration added by Zim install {{{
#
# This is not sourced during shell startup, and it's only used to configure the
# zimfw plugin manager.
#

#
# Modules
#

# Sets sane Zsh built-in environment options.
zmodule environment
# Provides handy git aliases and functions.
zmodule git
# Applies correct bindkeys for input events.
zmodule input
# Sets a custom terminal title.
zmodule termtitle
# Utility aliases and functions. Adds colour to ls, grep and less.
zmodule utility

#
# Prompt
#

# Exposes to prompts how long the last command took to execute, used by asciiship.
zmodule duration-info
# Exposes git repository status information to prompts, used by asciiship.
zmodule git-info
# A heavily reduced, ASCII-only version of the Spaceship and Starship prompts.
zmodule asciiship

#
# Completion
#

# Additional completion definitions for Zsh.
zmodule zsh-users/zsh-completions --fpath src
# Enables and configures smart and extensive tab completion.
# completion must be sourced after all modules that add completion definitions.
zmodule completion

#
# Modules that must be initialized last
#

# Fish-like syntax highlighting for Zsh.
# zsh-users/zsh-syntax-highlighting must be sourced after completion
zmodule zsh-users/zsh-syntax-highlighting
# Fish-like history search (up arrow) for Zsh.
# zsh-users/zsh-history-substring-search must be sourced after zsh-users/zsh-syntax-highlighting
zmodule zsh-users/zsh-history-substring-search
# Fish-like autosuggestions for Zsh.
zmodule zsh-users/zsh-autosuggestions
# }}} End configuration added by Zim install

zmodule romkatv/powerlevel10k
zmodule fzf

大部分插件是默认就添加的。我只额外加了一些。

  • powerlevel10k:一个自定义的主题p10k configure

  • fzf

  • zimfw install/uninstall

  • zimfw update

  • zimfw upgrade:更新zimfw自己

最近,在WSL2上如果不启动Docker Desktop会报一个关于docker命令补全文件找不到的错,解决方法如下:需要先打开Docker Desktop

1
2
3
sudo rm -rf /usr/share/zsh/vendor-completions/_docker
sudo cp /mnt/wsl/docker-desktop/cli-tools/usr/share/zsh/vendor-completions/_docker /usr/share/zsh/vendor-completions/
sudo chattr +i /usr/share/zsh/vendor-completions/_docker

Starship

starship是一个高度自定义的prompt,可以平替p10k

安装

1
2
3
curl -sS https://starship.rs/install.sh | sh

eval "$(starship init zsh)" # 添加到~/.zshrc

配置

添加~/.config/starship.toml文件。format由若干个module组成,对需要的module进行配置即可,各个字段的含义在文档中都有解释。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Inserts a blank line between shell prompts
add_newline = false

format = """
$directory\
$git_branch\
$git_commit\
$git_state\
$git_status\
$sudo\
$character\
"""

right_format = """
$cmd_duration\
"""

[directory]
truncate_to_repo = false
truncation_symbol = ".../"

[sudo]
symbol = "with 💪 "
allow_windows = true
disabled = false

[cmd_duration]
min_time = 10_000 # Show command duration over 10,000 milliseconds (=10 sec)

# Make prompt a single line instead of two lines
[line_break]
disabled = true

# Disable the package module, hiding it from the prompt completely
[package]
disabled = true

快捷键

  • C-a:移动到行首
  • C-e:移动到行尾
  • C-r:搜索历史命令
  • C-u:剪切当前行
  • C-k:剪切光标到行尾的内容
  • C-y:粘贴剪贴的内容

卸载

1
2
rm -rf $HOME/.zim
rm $HOME/.zimrc

并移除在.zshrc中添加的行。