zsh 是 Neo 日常开发过程中最常用的 shell,它大幅提升了开发效率,也不再为忘记命令而苦恼。本文记录 zsh 安装、使用过程。
zsh 是一个为交互式使用而设计的 兼容 bash 的 shell,尽管它也是一个强大的脚本语言。Bash、 ksh 和 tcsh 的许多有用特性都被合并到 zsh 中; 还添加了许多原始特性。
相较 bash 具有以下优点:
关于 zsh 的更多的信息,可以访问 https://www.zsh.org/ 查看。
zsh 的初期配置太过繁琐,流行率一直不高。
brew install zsh
sudo apt-get install zsh
安装完成后,使用 cat /etc/shells
查看系统可以用的 shell:
$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash
/usr/bin/tmux
/bin/zsh
/usr/bin/zsh
发现多了 zsh, 说明安装成功
执行命令 : echo $SHELL
查看当前默认 shell
$ echo $SHELL
/bin/bash
使用 chsh -s /bin/zsh
命令将 zsh 设置为系统默认 shell。
为 root 设置默认 shell
sudo chsh -s /bin/zsh
为特定用户设置默认 shell
sudo chsh -s /bin/zsh <username>
# <username> 替换为实际用户名
返回结果如下,表示切换完成(下载安装 oh-my-zsh 成功后也会提示切换)
# sudo chsh -s /bin/zsh
Changing shell for root.
Shell changed.
我遇到的情况是没有返回上述信息,新建terminal仍然是 bash 为默认 shell,查看 /etc/passwd 发现确实成功修改了用户的 shell 记录,重启系统才完成了默认 shell 的修改
初次启动 zsh 会进入配置界面,输入 0 可以跳过
This is the Z Shell configuration function for new users,
zsh-newuser-install.
You are seeing this message because you have no zsh startup files
(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory
~). This function can help you with a few settings that should
make your use of the shell easier.
You can:
(q) Quit and do nothing. The function will be run again next time.
(0) Exit, creating the file ~/.zshrc containing just a comment.
That will prevent this function being run again.
(1) Continue to the main menu.
(2) Populate your ~/.zshrc with the configuration recommended
by the system administrator and exit (you will need to edit
the file by hand, if so desired).
--- Type one of the keys in parentheses --- 0
github主页: https://github.com/ohmyzsh/ohmyzsh/
zsh 由于配置过于复杂门槛过高,大神开发了 oh my zsh 极大降低了使用门槛。
安装 oh my zsh 前确认本地安装了 git
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# 或者
sh -c "$(wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"
Looking for an existing zsh config...
Found /home/vvd/.zshrc. Backing up to /home/vvd/.zshrc.pre-oh-my-zsh
Using the Oh My Zsh template file and adding it to /home/vvd/.zshrc.
__ __
____ / /_ ____ ___ __ __ ____ _____/ /_
/ __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \
/ /_/ / / / / / / / / / / /_/ / / /_(__ ) / / /
\____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/
/____/ ....is now installed!
Before you scream Oh My Zsh! look over the `.zshrc` file to select plugins, themes, and options.
• Follow us on Twitter: @ohmyzsh
• Join our Discord community: Discord server
• Get stickers, t-shirts, coffee mugs and more: Planet Argon Shop
➜ ~
vim ~/.zshrc
其中 ZSH_THEME
是主题字段,主题信息可以去 这里 查看
主题文件在
~/.oh-my-zsh/themes
目录
比如我修改主题字段为 :jonathan
修改配置后,通过
source ~/.zshrc
或者退出重新登录使配置生效。
主题就会变成:
ZSH_THEME="random"
除了内置主题外,还可以选择其他开源的主题,强烈推荐尝试一下 powerlevel10k
主题,一个顶十个,项目地址为:https://github.com/romkatv/powerlevel10k
oh-my-zsh 安装这个款主题的方法:使用 git 将文件 clone 只指定文件夹 ~/.oh-my-zsh/custom/themes/powerlevel10k
,命令如下:
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
使用 vim 编辑 .zshrc
,键入以下内容并保存:
ZSH_THEME="powerlevel10k/powerlevel10k"
最后,执行 source ~/.zshrc
配置生效,这时会提示对主题进行配置,按照提示进行即可。
我们看下 git 的别名:
cat ~/.oh-my-zsh/plugins/git/git.plugin.zsh
......
alias g='git'
alias ga='git add'
alias gaa='git add --all'
alias gapa='git add --patch'
alias gau='git add --update'
alias gav='git add --verbose'
alias gap='git apply'
alias gapt='git apply --3way'
......
自定义别名,在 ~/.zshrc
中,最下面直接写即可。
# Example aliases
alias ll='ls -lahF --color --time-style=long-iso'
或者直接使用 echo 写入。
echo 'alias ll="ls -lahF --color --time-style=long-iso"' >> ~/.zshrc
默认 oh-my-zsh 命令自动补全功能如下:
/usr/local/bin
这个长路径,只需要 cd /u/l/b
按 tab 键自动补全cd de
按 tab 键自动补全,或者查看 README.md
,只需要 cat rea
自动更正补全kubectl
按 tab 键即可看到可用命令kill
按 tab 键会自动显示出进程的 process id小技巧: 可以忽略
cd
命令,输入..
或者...
和当前目录名都可以跳转。
上述功能不需要额外的插件。
额外的自动补全功能,该项目将在完善时合并到 zsh,补充 zsh 中尚不支持的命令补全 (sysin)。
Clone the repository inside your oh-my-zsh repo:
git clone --depth=1 https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM:-${ZSH:-~/.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"
:
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).
另外还有一些插件来增强命令补全,可根据需要启用:
作用是根据历史输入命令的记录即时的提示(建议补全),然后按 → 键即可补全。
git clone --depth=1 https://github.com/zsh-users/zsh-autosuggestions.git ${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-autosuggestions
编辑 ~/.zshrc
,找到 plugins=(git) 这一行,修改为:
plugins=(
git
# other plugins...
zsh-autosuggestions
)
增强的实时自动命令补全插件:Incremental completion on zsh
该插件对性能似乎有一点点影响,请根据需要启用。
作用:命令错误会显示红色,直到你输入正确才会变绿色,另外路径正确会显示下划线。
安装:
git clone --depth=1 https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
配置启用插件:
编辑 ~/.zshrc
,以下部分添加插件的名字
plugins=([plugins...] zsh-syntax-highlighting)
开启新的 Shell 或执行 source ~/.zshrc
,就可以开始体验插件了。
z 是一个文件夹快捷跳转插件,对于曾经跳转过的目录,只需要输入最终目标文件夹名称,就可以快速跳转,避免再输入长串路径,提高切换文件夹的效率。
安装步骤:
.zshrc
中,把 z 加入插件列表:plugins=(
# other plugins...
zsh-autosuggestions
zsh-syntax-highlighting
z
)
source ~/.zshrc
,就可以开始体验插件了。upgrade_oh_my_zsh
uninstall_oh_my_zsh
Are you sure you want to remove Oh My Zsh? [y/N] Y
Removing ~/.oh-my-zsh
Looking for original zsh config...
Found ~/.zshrc.pre-oh-my-zsh -- Restoring to ~/.zshrc
Found ~/.zshrc -- Renaming to ~/.zshrc.omz-uninstalled-20170820200007
Your original zsh config was restored. Please restart your session.
Thanks for trying out Oh My Zsh. It's been uninstalled.
文章链接: