linux 设置终端 zsh
linux
本文字数:1.2k 字 | 阅读时长 ≈ 5 min

linux 设置终端 zsh

linux
本文字数:1.2k 字 | 阅读时长 ≈ 5 min

1. 安装 zsh

  1. 查看当前使用的 shellecho $SHELL
  2. 查看当前环境下所有的shellcat /etc/shells
  3. 安装zsh
  1. 切换默认shell为zshsudo chsh -s /bin/zsh
  2. 切换回bashsudo chsh -s /bin/bash

值得注意的是:zsh 在MacOSX上使用 brew 命令的安装的 zsh 路径在/usr/local/bin/zsh,而系统自带的 zsh 默认是在 /bin/zsh,如果需要使用brew安装的zsh,则在/etc/shells中添加一条记录,/usr/local/bin/zsh,也可以直接使用系统自带的zsh也行。

安装完后重开一个terminal执行echo $SHELL 发现当前 shell 已经变为 zsh

2. 安装 oh-my-zsh

有两种安装方式

这里以 curl 的安装方式为例,当出现 oh-my-zsh 就代表安装成功了

iZuf67mlx4ftb5vohxeobnZ# sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

Cloning Oh My Zsh...
remote: Enumerating objects: 1403, done.
remote: Counting objects: 100% (1403/1403), done.
remote: Compressing objects: 100% (1348/1348), done.
remote: Total 1403 (delta 35), reused 1059 (delta 27), pack-reused 0
Receiving objects: 100% (1403/1403), 3.21 MiB | 15.01 MiB/s, done.
Resolving deltas: 100% (35/35), done.
From https://github.com/ohmyzsh/ohmyzsh
 * [new branch]      master     -> origin/master
Branch 'master' set up to track remote branch 'master' from 'origin'.
Already on 'master'
/blog/hexo-blog

Looking for an existing zsh config...
Using the Oh My Zsh template file and adding it to /root/.zshrc.

Time to change your default shell to zsh:
Do you want to change your default shell to zsh? [Y/n] y
Changing your shell to /usr/bin/zsh...
Shell successfully changed to '/usr/bin/zsh'.

         __                                     __   
  ____  / /_     ____ ___  __  __   ____  _____/ /_  
 / __ \/ __ \   / __ `__ \/ / / /  /_  / / ___/ __ \ 
/ /_/ / / / /  / / / / / / /_/ /    / /_(__  ) / / / 
\____/_/ /_/  /_/ /_/ /_/\__, /    /___/____/_/ /_/  
                        /____/                       ....is now installed!


Before you scream Oh My Zsh! look over the `.zshrc` file to select plugins, themes, and options.

• Follow us on X: @ohmyzsh
• Join our Discord community: Discord server
• Get stickers, t-shirts, coffee mugs and more: Planet Argon Shop
➜  hexo-blog git:(main) ✗ 

3. 配置oh-my-zsh

Oh-My-Zsh的默认配置在~/.zshrc文件中配置可以配置,修改默认的基本配置:

ZSH_THEME="robbyrussell"  # zsh主题,可以配置为后面的Dracula主题
export UPDATE_ZSH_DAYS=30 # 30天检查更新zsh
HIST_STAMPS="yyyy-mm-dd"  # 历史记录时间格式
export LANG=en_US.UTF-8   # 语言
plugins=(git autojump zsh-autosuggestions) # 开启常用插件(这些插件都包含在oh-my-zsh中)

autojump命令能够记住我们在命令终端输入的命令,在我们下次使用的时候配合zsh-autosuggestions可以在我们输入命令时给出一些提示,加快我们的命令敲入速度,Oh-My-Zsh插件目录路径在~/.oh-my-zsh/plugins目录下

3.1 优化主题

  1. 安装Dracula主题
    Dracula在很多编辑器上都可以使用,更多信息可以参考官网
wget -O dracula.zip -c --no-check-certificate https://github.com/dracula/zsh/archive/master.zip
unzip dracula.zip # 解压缩
mv zsh-master/dracula.zsh-theme ~/.oh-my-zsh/themes/ # 将dracula.zsh-theme移动到oh-my-zsh主题目录

wget参数解释-O:自定义存储的文件名 -c:支持断点续传 --no-check-certificate:忽略 https 验证

安装好以后打开~/.zshrc文件修改文件中ZSH_THEME的配置为:dracula,重新打开一个终端或者使用命令source ~/.zshrc重载配置。

报错

如果出现以下错误,执行mv ~/zsh-master/lib ~/.oh-my-zsh/themes/lib即可

/root/.oh-my-zsh/themes/dracula.zsh-theme:source:16: no such file or directory: /root/.oh-my-zsh/themes/lib/async.zsh
bash: async_init: command not found...
bash: async_start_worker: command not found...                                                                                                                                      
bash: async_register_callback: command not found...
bash: async_job: command not found...

3.2 安装插件

Oh-My-Zsh的强大之处是安装各种插件加速我们在命令行终端的体验

  1. zsh-autosuggestions自动补全
  1. autojump
  1. zsh-syntax-highlighting

4. 一些问题

  1. 使用 screen:在 ~/.screenrc 文件中添加 shell "/bin/zsh" 来指定 zsh 作为 screen 的默认 shell,不然使用 screen 的时候会出现 zsh: command not found: git_prompt_info
  2. 有时候加入了插件无法用,检查一下最后有没有加入 source,或者有没有加入多个 plugins,保留一个列表即可
plugins=(
    autojump 
    zsh-autosuggestions
    zsh-syntax-highlighting
    ) # 开启常用插件(这些插件都包含在oh-my-zsh中)
[[ -s ~/.autojump/etc/profile.d/autojump.sh ]] && . ~/.autojump/etc/profile.d/autojump.sh

source $ZSH/oh-my-zsh.sh
4月 06, 2025
3月 10, 2025
12月 31, 2024