前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux ZSH 更便捷的 shell 环境

Linux ZSH 更便捷的 shell 环境

作者头像
为为为什么
发布2024-04-26 10:15:07
1270
发布2024-04-26 10:15:07
举报
文章被收录于专栏:又见苍岚又见苍岚

zsh 是 Neo 日常开发过程中最常用的 shell,它大幅提升了开发效率,也不再为忘记命令而苦恼。本文记录 zsh 安装、使用过程。

简介

zsh 是一个为交互式使用而设计的 兼容 bash 的 shell,尽管它也是一个强大的脚本语言。Bash、 ksh 和 tcsh 的许多有用特性都被合并到 zsh 中; 还添加了许多原始特性。

相较 bash 具有以下优点:

  • Tab 补全功能强大。命令、命令参数、文件路径均可以补全。
  • 插件丰富。快速输入以前使用过的命令、快速跳转文件夹、显示系统负载这些都可以通过插件实现。
  • 主题丰富。
  • 可定制性高。

关于 zsh 的更多的信息,可以访问 https://www.zsh.org/ 查看。

zsh 的初期配置太过繁琐,流行率一直不高。

安装 zsh

  • macOS:
代码语言:txt
复制
brew install zsh
  • ubuntu:
代码语言:txt
复制
sudo apt-get install zsh
  • 安装验证

安装完成后,使用 cat /etc/shells 查看系统可以用的 shell:

代码语言:txt
复制
$ 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, 说明安装成功

修改默认 shell
  • 当前 shell

执行命令 : echo $SHELL 查看当前默认 shell

代码语言:txt
复制
$ echo $SHELL
/bin/bash
  • chsh 命令

使用 chsh -s /bin/zsh 命令将 zsh 设置为系统默认 shell。

为 root 设置默认 shell

代码语言:txt
复制
sudo chsh -s /bin/zsh

为特定用户设置默认 shell

代码语言:txt
复制
sudo chsh -s /bin/zsh <username>
# <username> 替换为实际用户名

返回结果如下,表示切换完成(下载安装 oh-my-zsh 成功后也会提示切换)

代码语言:txt
复制
# sudo chsh -s /bin/zsh
Changing shell for root.
Shell changed.

我遇到的情况是没有返回上述信息,新建terminal仍然是 bash 为默认 shell,查看 /etc/passwd 发现确实成功修改了用户的 shell 记录,重启系统才完成了默认 shell 的修改

初次启动 zsh 会进入配置界面,输入 0 可以跳过

代码语言:txt
复制
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

安装 Oh My ZSH!

github主页: https://github.com/ohmyzsh/ohmyzsh/

zsh 由于配置过于复杂门槛过高,大神开发了 oh my zsh 极大降低了使用门槛。

安装 oh my zsh 前确认本地安装了 git

  • 安装命令:
代码语言:txt
复制
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 -)"
  • 安装成功提示:
代码语言:txt
复制
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

?  ~ 

?

修改主题

代码语言:txt
复制
vim ~/.zshrc

其中 ZSH_THEME 是主题字段,主题信息可以去 这里 查看

主题文件在 ~/.oh-my-zsh/themes 目录

比如我修改主题字段为 :jonathan

修改配置后,通过 source ~/.zshrc 或者退出重新登录使配置生效。

主题就会变成:

随机主题
  • 我们还可以随机设置主题:
  • 步骤同上
代码语言:txt
复制
ZSH_THEME="random"
  • 每次打开终端主题是随机的。
开源主题

除了内置主题外,还可以选择其他开源的主题,强烈推荐尝试一下 powerlevel10k 主题,一个顶十个,项目地址为:https://github.com/romkatv/powerlevel10k

oh-my-zsh 安装这个款主题的方法:使用 git 将文件 clone 只指定文件夹 ~/.oh-my-zsh/custom/themes/powerlevel10k ,命令如下:

代码语言:txt
复制
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

使用 vim 编辑 .zshrc,键入以下内容并保存:

代码语言:txt
复制
ZSH_THEME="powerlevel10k/powerlevel10k"

最后,执行 source ~/.zshrc 配置生效,这时会提示对主题进行配置,按照提示进行即可。

别名配置

我们看下 git 的别名:

代码语言:txt
复制
cat ~/.oh-my-zsh/plugins/git/git.plugin.zsh
代码语言:txt
复制
......

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 中,最下面直接写即可。

代码语言:txt
复制
# Example aliases
alias ll='ls -lahF --color --time-style=long-iso'

或者直接使用 echo 写入。

代码语言:txt
复制
echo 'alias ll="ls -lahF --color --time-style=long-iso"' >> ~/.zshrc

命令自动补全

内置自动补全功能

默认 oh-my-zsh 命令自动补全功能如下:

  • 自动列出目录 输入 cd 按 tab 键,目录将自动列出,在按 tab 可以切换
  • 自动目录名简写补全 要访问 /usr/local/bin 这个长路径,只需要 cd /u/l/b 按 tab 键自动补全
  • 自动大小写更正 要访问 Desktop 文件夹,只需要 cd de 按 tab 键自动补全,或者查看 README.md,只需要 cat rea 自动更正补全
  • 自动命令补全 输入 kubectl 按 tab 键即可看到可用命令
  • 自动补全命令参数 输入 kill 按 tab 键会自动显示出进程的 process id

小技巧: 可以忽略 cd 命令,输入 .. 或者 ... 和当前目录名都可以跳转。

上述功能不需要额外的插件。

zsh-completions

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

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

代码语言:txt
复制
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":

代码语言:txt
复制
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

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

代码语言:txt
复制
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) 这一行,修改为:

代码语言:txt
复制
plugins=(
    git
    # other plugins...
    zsh-autosuggestions
)
Incremental completion on zsh

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

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

其他插件

语法高亮插件

插件名称:zsh-syntax-highlighting

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

安装:

代码语言:txt
复制
git clone --depth=1 https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

配置启用插件:

编辑 ~/.zshrc,以下部分添加插件的名字

代码语言:txt
复制
plugins=([plugins...] zsh-syntax-highlighting)

开启新的 Shell 或执行 source ~/.zshrc,就可以开始体验插件了。

文件夹快捷跳转插件

z 是一个文件夹快捷跳转插件,对于曾经跳转过的目录,只需要输入最终目标文件夹名称,就可以快速跳转,避免再输入长串路径,提高切换文件夹的效率。

安装步骤:

  1. 由于 oh-my-zsh 内置了 z 插件,所以只需要在 .zshrc 中,把 z 加入插件列表:
代码语言:txt
复制
plugins=(
     # other plugins...
     zsh-autosuggestions
     zsh-syntax-highlighting
     z
)
  1. 开启新的 Shell 或执行 source ~/.zshrc,就可以开始体验插件了。

升级 Oh My Zsh

  • 打开终端输入:
代码语言:txt
复制
upgrade_oh_my_zsh

卸载 Oh My Zsh

  • 终端输入 :
代码语言:txt
复制
uninstall_oh_my_zsh
Are you sure you want to remove Oh My Zsh? [y/N]  Y
  • 终端提示信息:
代码语言:txt
复制
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.

参考资料

文章链接:

/developer/article/2412910

本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-4-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
  • 安装 zsh
    • 修改默认 shell
    • 安装 Oh My ZSH!
    • 修改主题
      • 随机主题
        • 开源主题
        • 别名配置
        • 命令自动补全
          • 内置自动补全功能
            • zsh-completions
              • zsh-autosuggestions
                • Incremental completion on zsh
                • 其他插件
                  • 语法高亮插件
                    • 文件夹快捷跳转插件
                    • 升级 Oh My Zsh
                    • 卸载 Oh My Zsh
                    • 参考资料
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
                    http://www.vxiaotou.com