22.9 配置 Shell

Zsh

安装 Zsh

  • 使用 pkg 安装:

# pkg install zsh zsh-completions zsh-autosuggestions zsh-syntax-highlighting
程序
说明

zsh

zsh shell

zsh-completions

自动补全

zsh-autosuggestions

类 Fish shell 的 Zsh 自动补全

zsh-syntax-highlighting

类 Fish shell 的 Zsh 语法高亮

  • 使用 ports 安装:

# cd /usr/ports/shells/zsh/ && make install clean
# cd /usr/ports/shells/zsh-completions && make install clean
# cd /usr/ports/shells/zsh-autosuggestions/ && make install clean
# cd /usr/ports/shells/zsh-syntax-highlighting/ && make install clean

查看安装后说明

# pkg info -D zsh
zsh-5.9_5:
On install:
==========================================================

By default, zsh looks for system-wide defaults in
/usr/local/etc.

If you previously set up /etc/zprofile, /etc/zshenv, etc.,
either move them to /usr/local/etc or rebuild zsh with the
ETCDIR option enabled. 

==========================================================
root@ykla:/home/ykla # pkg info -D zsh-autosuggestions
默认情况下,zsh 会在 /usr/local/etc 中查找系统范围的默认设置。

如果你之前设置了 /etc/zprofile、/etc/zshenv 等,
要么将它们移到 /usr/local/etc,要么在启用 ETCDIR 选项的情况下重新构建 zsh。

==========================================================
# pkg info -D zsh-autosuggestions
zsh-autosuggestions-0.7.1:
On install:
Add the line below to your .zshrc to enable auto suggestions.

source /usr/local/share/zsh-autosuggestions/zsh-autosuggestions.zsh 
安装时:
将以下行添加到你的 .zshrc 文件中以启用自动建议。

source /usr/local/share/zsh-autosuggestions/zsh-autosuggestions.zsh 

zsh-syntax-highlighting-0.8.0,1:
On install:
Add the line below to *the end of* your .zshrc to enable highlighting.

source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
安装时:
将以下行添加到你的 .zshrc 文件 *末尾* 以启用语法高亮。

source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

配置 Zsh

# chsh -s /usr/local/bin/zsh # 切换 shell 至 zsh
chsh: user information updated

编辑 ~/.zshrc,添加下面几行:

source /usr/local/share/zsh-autosuggestions/zsh-autosuggestions.zsh
source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
fpath+=/usr/local/share/zsh/site-functions/

立即使用:

# zsh # 将当前 shell 切换到 zsh
# source ~/.zshrc # 刷新环境变量
# rm -f ~/.zcompdump; # 删除补全缓存
# autoload -Uz compinit # 加载 compinit
# compinit # 强制重建缓存

使用主题美化

# git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ~/powerlevel10k
# echo 'source ~/powerlevel10k/powerlevel10k.zsh-theme' >>~/.zshrc

刷新环境变量

# source ~/.zshrc

回答几个问题即可完成配置。重启后完成配置。

参考文献

Bash

安装 Bash

  • 使用 pkg 安装:

# pkg install bash bash-completion-freebsd bash-completion-zfs 

解释:

  • bash shell 主程序

  • bash-completion-freebsd 针对 FreeBSD 的 Bash 补全库扩展,作为依赖安装会 shells/bash-completion

  • bash-completion-zfs 针对 OpenZFS 的 Bash 补全库扩展

  • 使用 ports 安装:

# cd /usr/ports/shells/bash/ && make install clean
# cd /usr/ports/shells/bash-completion-freebsd/ && make install clean
# cd /usr/ports/shells/bash-completion-zfs/ && make install clean

查看安装后配置

# pkg info -D bash-completion # 作为依赖安装的
bash-completion-2.14.0,2:
On install:
To enable the bash completion library, add the following to
your .bashrc file:

[[ $PS1 && -f /usr/local/share/bash-completion/bash_completion.sh ]] && \
	source /usr/local/share/bash-completion/bash_completion.sh

See /usr/local/share/doc/bash-completion/README.md for more information.
安装时:
要启用 bash 补全库,请将以下内容添加到你的 .bashrc 文件中:

[[ $PS1 && -f /usr/local/share/bash-completion/bash_completion.sh ]] && \
	source /usr/local/share/bash-completion/bash_completion.sh

有关更多信息,请参见 /usr/local/share/doc/bash-completion/README.md。

配置 Bash

# chsh -s /usr/local/bin/bash # 切换到 bash
# touch ~/.bash_profile #  创建所需变量
  • 写入所需变量:

编辑 ~/.bash_profile,写入

[[ $PS1 && -f /usr/local/share/bash-completion/bash_completion.sh ]] && source /usr/local/share/bash-completion/bash_completion.sh

立即使用:

# bash # 将当前 shell 切换到 bash
# source ~/.bash_profile # 刷新环境变量

配置 csh/tcsh

csh(C shell,灵感来自 C 语言,语言亦类似,作者是 Bill Joy)是 FreeBSD 基本系统内置的 shell,过去是 root 用户的默认 shell。

技巧

csh 与 tsch 的关系

有必要注意的是,FreeBSD 中 csh 就是 tcsh。二者没有区别。你从源代码 https://github.com/freebsd/freebsd-src/blame/main/bin/csh/Makefile 就能看出来,同时 man csh 也会被重定向至 tcsh。

注意

虽然 csh tsch 本质上都是一款程序,但是使用时二者有明显的不同,如果以 csh 之参数来调用,则会关闭部分 tcsh 扩展。

注意

FreeBSD 14 中的 shell 被统一为了 sh


  • 如何使得文件文件夹显示不同颜色:

~/.csh.cshrc 里面加入:

alias ls ls –G

并重新登录即可。


  • 如何让 FreeBSD 的 csh 像 bash 那样按 Tab 列出列出无法补齐的候选文件?在 ~/.csh.cshrc 中加入:

set filec
set autolist

刷新环境变量:

# source ~/.cshrc 
  • 如何让 csh 像 zsh 那样具有命令错误修正呢

比如你用 emacs 写 c 语言程序,但当你输完 emacs maTab 键 回车时:他会匹配所有 ma 开头的文件。而此配置是对部分匹配的文件进行忽略,也即按 Tab 时不会再有被你忽略的东西。编程友好,不会再匹配到二进制 .o 之类的文件:

set correct = cmd lz/usr/bin tcsh>ls /usr/bin (y|n|e|a)?
set fignore = (.o ~) emacs ma[^D] main.c main.c~ main.o emacs ma[tab] emacs main.c

最后更新于