整理一些优化 shell 使用体验的小工具。

starship

Github: starship

starship 是跨平台支持不同 shell 的命令行主题配置工具。

安装配置:

在 Windows 可以使用 winget 安装

1
winget install --id Starship.Starship

在 Linux 可以使用下面的命令进行全局安装

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

也可以直接在 Github Release 手动下载预编译版本(使用 rust 编写,解压出来就是单文件),存放在 ~/.local/bin 即可。

需要在每一个 shell 的启动配置文件中都加入对应的配置

~/.bashrc
1
eval "$(starship init bash)"
~/.config/fish/config.fish
1
starship init fish | source
$profile
1
Invoke-Expression (&starship init powershell)

starship 的配置文件统一在 ~/.config/starship.toml 中,默认方案会使用很多 Nerd 字体的符号,而且有的默认符号很丑。

下面是我初步使用的配置方案,重点在调整默认符号和 user@hostname 的显示

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
"$schema" = 'https://starship.rs/config-schema.json'


[directory]
fish_style_pwd_dir_length = 2

[python]
symbol = " "
detect_extensions = []

[julia]
symbol = " "

[git_branch]
symbol = " "

[shell]
disabled = false
fish_indicator = '󰈺 '
powershell_indicator = ''
bash_indicator = ''
style = 'cyan bold'
format = '[$indicator]($style)'

[username]
style_user = 'green bold'
style_root = 'black bold'
format = '[$user@]($style)'
disabled = false

[hostname]
ssh_only = true
format = '[$hostname]($style) in '
ssh_symbol = ''
style = 'bold green'

[git_status]
style = 'bold yellow'

[cmake]
format = 'via [cmake]($style) '

[cpp]
disabled = false
symbol = ''

[c]
symbol = ''

具体修改可以查看官方文档中的 configpresets

可以使用 starship explain 解释正在展示的各个图标的含义。

zoxide

在各种 shell 上都有 z 跳转小工具,用法大同小异,但是相比给每个 shell 单独配置,单独存储路径数据,一个通用的工具显然更合适,尤其是在需要同时使用 bash 和 fish 的情况下。

Github: zoxide

在 Linux 上使用官方文档推荐的安装方式即可(用户级安装,安装到~/.local/)

1
curl -sSfL https://raw.githubusercontent.com/ajeetdsouza/zoxide/main/install.sh | sh

在 Windows 上使用 winget 最简单方便

1
winget install ajeetdsouza.zoxide

需要在每一个shell的启动配置文件中都加入对应的配置

~/.bashrc
1
eval "$(zoxide init bash)"
~/.config/fish/config.fish
1
zoxide init fish | source
$profile
1
Invoke-Expression (& { (zoxide init powershell | Out-String) })

使用 z 命令即可模糊跳转,与此同时还有一个 zi 命令,这个命令是交互式的。

给 init 加上 --cmd cd 选项可以直接用 z 命令接管原本的 cd 命令。

zoxide 使用的数据库是用户级的,对不同的shell通用

  • Linux: ~/.local/share/zoxide/db.zo
  • Windows: %LOCALAPPDATA%\zoxide\db.zo

可以用下面的命令查询数据库记录的路径信息

1
zoxide query --list --score

不需要使用 z 来记录和更新路径数据,直接使用 cd 就可以记录和更新数据(由 zoxide 通过 hook 实现)。

fzf

Github: fzf

在 Linux 上的安装过程如下

1
2
3
4
5
wget https://github.com/junegunn/fzf/releases/download/v0.67.0/fzf-0.67.0-linux_amd64.tar.gz

tar -xf fzf-0.67.0-linux_amd64.tar.gz

mv fzf ~/.local/bin

实际上这里的压缩包只有一个名为 fzf 的可执行文件,随便放哪都行。

对shell需要进行配置:

在 ~/.bashrc 添加如下内容

1
eval "$(fzf --bash)"

在 ~/.config/fish/config.fish 添加如下内容

1
fzf --fish | source

Windows 可以通过 winget 安装

1
winget install fzf

对 pwsh 暂时没有对应的启动配置。

pwsh 目前可行的做法是使用一个 wrapper:PSFzf

下载并在 PROFILE 中手动开启

1
2
3
Import-Module PSFzf # fzf wrapper on pwsh

Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+t' -PSReadlineChordReverseHistory 'Ctrl+r'

fzf 提供的最常用的三个快捷键是:

  • Ctrl+t: 模糊查找当前目录下的文件(用 fd 作为后端)
  • Ctrl+r: 模糊搜索命令历史
  • Alt+c: 模糊进入当前目录下的子目录

注意:对于 fish shell,Ctrl+t 的行为比较奇怪,会利用当前输入的命令的最后一部分作为起始目录,例如按下快捷键时正在输入 ls /opt/,那么操作会基于 /opt/ 目录进行。

各个 Shell(包括 pwsh)其实都配置了 Ctrl+r 搜索历史命令,但是都没有 fzf 的这个好用。

eza

Github: eza

eza 是 ls 的现代替代品 exa 的 fork,因为 exz 不再维护。

由于 eza 的选项与默认的 ls 选项非常不同,因此不建议用 alias 直接替代。