FreeBSD 全局安装配置 zsh + oh-my-zsh

The Power To Serve

Posted by sysin on 2022-06-01
Estimated Reading Time 11 Minutes
Words 2.4k In Total
更新日期:Wed Jun 01 2022 18:12:28 GMT+0800,阅读量:

请访问原文链接:FreeBSD 全局安装配置 zsh + oh-my-zsh,查看最新版。原创作品,转载请保留出处。

作者主页:sysin.org


无耻抄袭者 Yu Tao,请立遁!!!

OMZLogo

FreeBSD 使用 tcsh(1) 作为自己的默认 Shell。zsh 作为终极 Shell,在 FreeBSD 下也是适用的。使用 oh-my-zsh 配置 zsh 默认仅对当前用户有效,新建或者切换用户后,仍然恢复到了默认的 shell,重新配置新用户略嫌繁琐。本文描述全局配置的方法,新用户将默认使用 zsh 并加载统一的配置文件。

本文内容基于 FreeBSD 13,其他版本理论上也适用。

准备 zsh

  • 查看当前 shell

    1
    echo $SHELL

    返回结果:

    1
    /bin/csh

注意:以下全局配置相关命令需要 root 权限,请切换到 root 账号,或者使用 sudo(需手动安装配置)。

  • 安装 zsh

    1
    pkg install zsh

    提示消息:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    Message from zsh-5.8:

    --
    ==========================================================

    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.

    ==========================================================

    注意:在 FreeBSD 中非系统自带 shell 安装路径如下:

    1
    2
    % type zsh
    zsh is /usr/local/bin/zsh
  • 设置默认 shell

    根据 FreeBSD 安全建议,不要为 root 用户切换为非系统内置 shell,因为 FreeBSD 中 非内置 shell 安装路径为 /usr/local/bin/(不同于 Linux 安装在 /bin/),意外情况下可能导致文件系统无法挂载,root 用户无法访问自己的 shell。但是可以将 toor 账号配置使用 zsh 作为默认 shell。

    为非 root 用户切换默认 shell:

    1
    2
    3
    chsh -s /usr/local/bin/zsh <username>
    # <username> 替换为实际用户名
    chsh -s /usr/local/bin/zsh toor

    退出重新登录生效。

  • 安装 git

    执行:

    1
    pkg install git

全局配置 zsh

在 FreeBSD 中,用户安装的程序都是放在 /usr/local 下,配置文件在 /usr/local/etc 目录下。不同于 Linux 各发行版有自己的安装程序路径规则,BSD 系统都使用统一的规范来管理。我们将 oh-my-zsh 主目录放置在 /usr/local/etc 目录下。

  • 使用 GNU sed 替代 BSD sed

    以下 sed 命令部分请用 gsed 替代,或者使用 alias。

    1
    2
    3
    pkg install gsed #FreeBSD
    gsed --v #安装后命令都是 gsed
    alias sed=gsed #可用别名替代 BSD sed
  • 切换成 root

    1
    2
    3
    4
    su #切换到 root 账号,pwd 不变
    su - #切换到 root 账号,pwd 同时变为 root 主目录
    sudo -i #同上
    alias sed=gsed #用 GNU sed 替代 BSD sed
  • 全局安装 zsh 到 /usr/local/etc/ 目录

    1
    git clone --depth=1 https://github.com/ohmyzsh/ohmyzsh.git /usr/local/etc/oh-my-zsh
  • 从模板文件复制 .zshrc 创建默认配置文件(新用户将使用该配置文件)

    1
    cp /usr/local/etc/oh-my-zsh/templates/zshrc.zsh-template /usr/share/skel/dot.zshrc

    FreeBSD 用户默认配置文件目录是 /usr/share/skel/,Linux 默认为 /etc/skel/

    注意:/usr/share/skel/ 中点开头的文件和文件夹(隐藏属性)都加上 dot,非隐藏文件和文件夹命名不变。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # ls -ahlFTo
    total 49
    drwxr-xr-x 2 root wheel uarch 10B Sep 7 04:38:19 2022 ./
    drwxr-xr-x 32 root wheel uarch 32B May 12 16:15:22 2022 ../
    -rw-r--r-- 1 root wheel uarch 962B May 12 16:29:31 2022 dot.cshrc
    -rw-r--r-- 1 root wheel uarch 323B May 12 16:29:31 2022 dot.login
    -rw-r--r-- 1 root wheel uarch 91B May 12 16:29:31 2022 dot.login_conf
    -rw------- 1 root wheel uarch 301B May 12 16:29:31 2022 dot.mail_aliases
    -rw-r--r-- 1 root wheel uarch 267B May 12 16:29:31 2022 dot.mailrc
    -rw-r--r-- 1 root wheel uarch 978B May 12 16:29:31 2022 dot.profile
    -rw-r--r-- 1 root wheel uarch 695B May 12 16:29:31 2022 dot.shrc
    -rw-r--r-- 1 root wheel uarch 3.8K Sep 7 04:38:19 2022 dot.zshrc
  • 修改 on-my-zsh 的安装目录 export ZSH=$HOME/.oh-my-zshexport ZSH=/usr/local/etc/oh-my-zsh

    1
    sed -i 's|$HOME/.oh-my-zsh|/usr/local/etc/oh-my-zsh|g' /usr/share/skel/dot.zshrc
  • 为每个用户配置独立的 cache 目录

    编辑 /usr/share/skel/dot.zshrcexport ZSH=/usr/local/etc/oh-my-zsh 下添加一句:

    1
    export ZSH_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/oh-my-zsh"

    注意:必须在 export ZSH=/usr/local/etc/oh-my-zsh 之后 source $ZSH/oh-my-zsh.sh 之前添加,否则会混乱。

    oh-my-zsh.sh 中定义了 ZSH_CACHE_DIR 路径:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # Set ZSH_CACHE_DIR to the path where cache files should be created
    # or else we will use the default cache/
    if [[ -z "$ZSH_CACHE_DIR" ]]; then
    ZSH_CACHE_DIR="$ZSH/cache"
    fi

    # Make sure $ZSH_CACHE_DIR is writable, otherwise use a directory in $HOME
    if [[ ! -w "$ZSH_CACHE_DIR" ]]; then
    ZSH_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/oh-my-zsh"
    fi

    初始安装 “$ZSH/cache” 存在为空目录。
    如果不添加上述语句,默认 root 用户对 “$ZSH/cache” 有写权限,会直接将 “$ZSH/cache” 作为 cache 目录。
    如果在 source $ZSH/oh-my-zsh.sh 之后添加,root 在登录后先将 “$ZSH/cache” 作为 cache 目录,然后再更改为自定义路径。
    对于普通用户,默认将使用 “${XDG_CACHE_HOME:-$HOME/.cache}/oh-my-zsh” 作为 cache 目录,经测试自定义其他路径无效。

  • 更改默认主题(默认 robbyrussell,推荐 ys)

    编辑 /usr/share/skel/dot.zshrc 文件修改:

    1
    sed -i '/^ZSH_THEME=*/c ZSH_THEME="ys"' /usr/share/skel/dot.zshrc
  • 取消每周自动检查更新

    取消注释 /usr/share/skel/dot.zshrc 中的 “# DISABLE_AUTO_UPDATE=“true””

    1
    2
    3
    4
    5
    6
    7
    sed -i 's/^#[ ]\(DISABLE_AUTO_UPDATE="true"\)/\1/' /usr/share/skel/dot.zshrc
    # -i 写入文件
    # "" Unix sed 备份机制,直接引号表示不备份
    # s 替换指定字符
    # ^ 匹配行开始
    # [ ] 中括号中间有空格表示要替换的内容是一个空格,注意被替换的内容是空格用 " "(双引号中间有空格)表示。
    # \1 的意思就类似于前面的 (DISABLE_AUTO_UPDATE="true"\),\1 就是复制这个位置的内容,

    上述配置在 2022年6月仍有效,现在已经更新为:

    1
    2
    3
    4
    # Uncomment one of the following lines to change the auto-update behavior
    zstyle ':omz:update' mode disabled # disable automatic updates
    # zstyle ':omz:update' mode auto # update automatically without asking
    # zstyle ':omz:update' mode reminder # just remind me to update when it's time

    手动更新:omz update(旧版命令:upgrade_oh_my_zsh)

  • 配置 ll 别名(可选)

    1
    2
    3
    echo 'alias ll="ls -lahFTo"' >> /usr/share/skel/dot.zshrc
    echo 'alias sed=gsed' >> /usr/share/skel/dot.zshrc
    echo 'alias vi=vim' >> /usr/share/skel/dot.zshrc

内容如下:

1
2
3
4
5
6
7
# alias
alias ll="ls -lahFTo"
alias sed=gsed
alias vi=vim

# ture off NOMATCH shell option set, error like: "zsh: no matches found"
unsetopt NOMATCH

全局配置插件

全局安装插件(安装到 /usr/local/etc/oh-my-zsh/custom/plugins/

  • zsh-syntax-highlighting:语法高亮插件。

    作用:命令错误会显示红色,直到你输入正确才会变绿色,另外路径正确会显示下划线。

    安装:

    1
    git clone --depth=1 https://github.com/zsh-users/zsh-syntax-highlighting.git /usr/local/etc/oh-my-zsh/custom/plugins/zsh-syntax-highlighting

    配置启用插件:

    编辑 /usr/share/skel/dot.zshrc,以下部分添加插件的名字

    1
    plugins=([plugins...] zsh-syntax-highlighting)

    另外一种写法,可以使用注释:

    1
    2
    3
    4
    5
    plugins=(
    git
    #zsh-autosuggestions
    zsh-syntax-highlighting
    )

    快速修改:

    1
    sed -i '/^plugins=*/c plugins=(git zsh-syntax-highlighting)' /usr/share/skel/dot.zshrc
  • zsh-completions,额外的自动补全。

    额外的自动补全功能,用于补充 zsh 中尚不支持的命令补全,该项目将在完善时合并到 zsh。

    • Clone the repository inside your oh-my-zsh repo:

      1
      git clone --depth=1 https://github.com/zsh-users/zsh-completions /usr/local/etc/oh-my-zsh/custom/plugins/zsh-completions
    • Add it to FPATH in your .zshrc by adding the following line before source "$ZSH/oh-my-zsh.sh":

      1
      fpath+=${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-completions/src

    Note: adding it as a regular Oh My ZSH! plugin will not work properly (see #603).

  • zsh-autosuggestions:作用是根据历史输入命令的记录即时的提示(建议补全),然后按 → 键即可补全。

    下载:

    1
    git clone --depth=1 https://github.com/zsh-users/zsh-autosuggestions.git /usr/local/etc/oh-my-zsh/custom/plugins/zsh-autosuggestions

    编辑 /usr/share/skel/dot.zshrc,找到 plugins=(git) 这一行,修改为:

    plugins=(git zsh-autosuggestions)

  • 增强的实时自动命令补全插件:Incremental completion on zsh

该插件对性能似乎有一点点影响,请根据需要启用。

作用如图:

incr

1
2
3
4
mkdir /usr/local/etc/oh-my-zsh/custom/plugins/incr
curl -fsSL https://mimosa-pudica.net/src/incr-0.2.zsh -o /usr/local/etc/oh-my-zsh/custom/plugins/incr/incr.zsh
#启用该功能
echo "source $ZSH_CUSTOM/plugins/incr/incr.zsh" >> /usr/share/skel/dot.zshrc

使用用户配置文件

  • 改变新建用户的默认 shell 行为(可选)

    FreeBSD 中可以修改 adduser 命令默认 shell

    编辑 /usr/sbin/adduser

    将 “DEFAULTSHELL=/bin/sh” 修改为 “DEFAULTSHELL=/usr/local/bin/zsh”

    以下是 Linux 中的方法,备忘。

    vi /etc/default/useradd

    将 SHELL= * (比如 SHELL=/bin/sh) 改成 SHELL=/bin/zsh

    修改后,使用 useradd 命令无需 -s /bin/zsh,用户默认使用 zsh,当然也可以不修改此项,useradd 命令继续追加 -s /bin/zsh 参数。

    新用户登录后,将自动复制 .zshrc 和上述 cache 目录到用户主目录下,并自动加载 zsh 配置。

  • 针对现有用户

    直接复制 /usr/share/skel/dot.zshrc~/

    1
    2
    cp /usr/share/skel/dot.zshrc ~/.zshrc
    source ~/.zshrc
  • 新建一个用户并指定 zsh 为默认 Shell

    FreeBSD 安装 sudo 后,并且配置 wheel 组获得 sudo 权限,新建一个管理员账号并使用 zsh 作为默认 shell:

    1
    2
    pw useradd -n <username> -G wheel -s /usr/local/bin/zsh -m && passwd <username>
    # <username> 替换为实际用户名

    以下是 Linux 中的配置,备忘:

    在 visudo (/etc/sudoers)默认配置下,创建管理员账号的命令如下:

    CentOS:

    1
    2
    useradd -m -s /bin/zsh -G wheel sysin  #sysin 为用户名
    passwd sysin #为 sysin 设置密码

    Ubuntu:

    1
    2
    useradd -m -s /bin/zsh -G sudo sysin  #sysin 为用户名
    passwd sysin #为 sysin 设置密码

    备注:

    -m 创建同名 home 目录

    -s 指定 shell

    -G 修改附加属组


捐助本站 ❤️ Donate

点击访问官方网站


文章用于推荐和分享优秀的软件产品及其相关技术,所有软件默认提供官方原版(免费版或试用版),免费分享。对于部分产品笔者加入了自己的理解和分析,方便学习和测试使用。任何内容若侵犯了您的版权,请联系作者删除。如果您喜欢这篇文章或者觉得它对您有所帮助,或者发现有不当之处,欢迎您发表评论,也欢迎您分享这个网站,或者赞赏一下作者,谢谢!

支付宝赞赏 微信赞赏

赞赏一下


☑️ 评论恢复,欢迎留言❗️
敬请注册!点击 “登录” - “用户注册”(已知不支持 21.cn/189.cn 邮箱)。请勿使用联合登录(已关闭)