Fish Shell —— 更简单好用的Shell
写在前面
Fish Shell (Friendly Interactive Shell) 是一款现代的Shell工具,它的设计目标是提供更简单、更易用的命令行环境,它具有自动补全、语法高亮和更友好的用户界面等特性。
Fish Shell (Friendly Interactive Shell)
1.安装最新版本的Fish Shell
sudo apt-add-repository ppa:fish-shell/release-3
sudo apt update
sudo apt install fish
2.配置默认Shell为Fish Shell
cat /etc/shells # 查看所有Shell
chsh -s /usr/bin/fish
3.重新打开终端
Starship 轻量、迅速、可无限定制的高颜值终端!
如果是使用的Windows Terminal等终端,或者是VS Code等,需要注意安装一个 Nerd Font 的字体,并在终端启用(例如,可以尝试使用 Fira Code Nerd Font 字体)。
安装教程: https://zhuanlan.zhihu.com/p/467581369
我这里用的主题是Pastel Powerline Preset
fzf —— Fish Shell 的理想伙伴
1.首先需要安装fzf
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf # 获取最新版fzf
~/.fzf/install # 安装fzf
2.(可选)使用插件管理器fisher安装fzf.fish
curl -sL https://raw.githubusercontent.com/jorgebucaran/fisher/main/functions/fisher.fish | source && fisher install jorgebucaran/fisher # 安装fisher
fisher install jethrokuan/fzf
3.现在就可以通过以下快捷键来操作fzf了
fzf默认快捷键 | 新快捷键 | 备注 |
---|---|---|
Ctrl-t | Ctrl-o | 查找文件 |
Ctrl-r | Ctrl-r | 查找历史命令 |
Alt-c | Alt-c | 打开子目录 (递归搜索) |
Alt-Shift-c | Alt-Shift-c | 打开子目录 (隐藏文件夹) |
Ctrl-o | Alt-o | 使用默认编辑器 ($EDITOR) 打开文件/目录 |
Ctrl-g | Alt-Shift-o | 使用xdg-Open或open命令打开文件/目录 |
4.实现一个快速目录跳转
fzf有非常高的自由度,可以自己写脚本实现很多很有意思的功能,比如实现一个在命令行可以用的目录跳转功能,而不是每次cd一个目录就要ls一下
- 创建一个fzf_directory.fish
vim ~/.fzf/shell/fzf_directory.fish
然后写入以下代码
#!/usr/bin/fish
function fzf-getpath --description "Get path to current directory"
# set -U rpath_cwd_v1 (pwd)
# 主函数 前进/后退 路径
# function get_path
set -l main $argv[1]
set -l relative_index $argv[2]
set -l prefix $argv[3]
if test $main -eq 1
set -U rpath_cwd_v1 (pwd)
echo $rpath_cwd_v1
find $rpath_cwd_v1 -maxdepth 1 -type d -printf '%f\n' | tail -n +2 | sort
else if test $main -eq 0
if test $prefix != $rpath_cwd_v1
if test $relative_index -eq 1
if test $rpath_cwd_v1 = "/"
set -U rpath_cwd_v1 /$prefix
else
set -U rpath_cwd_v1 $rpath_cwd_v1/$prefix
end
echo $rpath_cwd_v1
find $rpath_cwd_v1 -maxdepth 1 -type d -printf '%f\n' | tail -n +2 | sort
else if test $relative_index -eq -1
set -U rpath_cwd_v1 (dirname $rpath_cwd_v1)
echo $rpath_cwd_v1
find $rpath_cwd_v1 -maxdepth 1 -type d -printf '%f\n' | tail -n +2 | sort
end
else
echo $rpath_cwd_v1
find $rpath_cwd_v1 -maxdepth 1 -type d -printf '%f\n' | tail -n +2 | sort
end
else
set -g rpath_temp $rpath_cwd_v1
if test -n $prefix && test $prefix != $rpath_cwd_v1
set -g rpath_temp $rpath_cwd_v1/$prefix
end
echo $rpath_temp
end
end
function fzf-routepath --description "Change directory"
set -l result (fzf-getpath 1 0 '' | fzf --height=85% --prompt 'Directories> ' --layout=reverse --info=inline --border --margin=1 --padding=1 --preview 'tree -L 2 -C (fish -C "fzf-getpath -1 0 {}")' --bind 'right:reload(fzf-getpath 0 1 {})' --bind 'left:reload(fzf-getpath 0 -1 /)' --bind 'enter:become(fzf-getpath -1 0 {})' --bind 'esc:become(pwd)' )
if [ -n "$result" ]
cd -- $result
# Remove last token from commandline.
commandline -t ""
commandline -it -- $prefix
end
commandline -f repaint
end
bind \ed fzf-routepath # 绑定fzf-rrpath到Alt-D
- 启动Fish Shell 的时候激活
在
echo "source ~/.fzf/shell/fzf_directory.fish" >> ~/.config/fish/config.fish
source ~/.config/fish/config.fish
- 按Alt D,打开fzf页面,按左右键进入和退出目录,按回车cd目录,右侧窗口可以预览目录内容
配置好了用起来还是非常爽的。