Skip to content

Shell使用zsh

约 1301 字大约 4 分钟

Ubuntuzsh

2025-01-07

介绍

Zsh(Z-shell)是一款用于交互式使用的shell,也可以作为脚本解释器来使用。其包含了 bashkshtcsh 等其他shell中许多优秀功能,也拥有诸多自身特色。

安装zsh

MacOS安装zsh

我现在用的是MacOS系统,之前也是自己安装的zsh。从 macOS Catalina 版开始,其默认shellbash改为zsh

命令行查看安装的shell

$ cat /etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

查看当前使用的shell

$ echo $SHELL
/bin/zsh

这里已经是zsh了,等后面换电脑的时候我再更新一下。

ubuntu安装zsh

目前只有之前ubuntu系统安装zsh的记录,就简单整理一下。

命令行查看安装的shell

$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/usr/bin/sh
/bin/dash
/usr/bin/dash

查看当前使用的shell

$ echo $SHELL
/bin/bash

更新apt,使用了sudo需要输入密码。

$ sudo apt update
[sudo] password for tdy: 
Hit:1 http://packages.microsoft.com/repos/code stable InRelease                                    
Hit:2 https://dl.google.com/linux/chrome/deb stable InRelease                                      
Hit:3 https://linux.teamviewer.com/deb stable InRelease                                            
Get:4 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]    
Hit:5 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy InRelease                          
Get:6 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-updates InRelease [119 kB]
Hit:7 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-backports InRelease
Fetched 229 kB in 3s (77.4 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
8 packages can be upgraded. Run 'apt list --upgradable' to see them.

通过apt安装zsh

$ sudo apt install zsh
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  gnome-terminal-data ibus-data ibus-gtk4 python3-ibus-1.0
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  zsh-common
Suggested packages:
  zsh-doc
The following NEW packages will be installed:
  zsh zsh-common
0 upgraded, 2 newly installed, 0 to remove and 8 not upgraded.
Need to get 4,794 kB of archives.
After this operation, 18.2 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy/main amd64 zsh-common all 5.8.1-1 [3,985 kB]
Get:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy/main amd64 zsh amd64 5.8.1-1 [809 kB]
Fetched 4,794 kB in 1s (4,346 kB/s)
Selecting previously unselected package zsh-common.
(Reading database ... 223724 files and directories currently installed.)
Preparing to unpack .../zsh-common_5.8.1-1_all.deb ...
Unpacking zsh-common (5.8.1-1) ...
Selecting previously unselected package zsh.
Preparing to unpack .../archives/zsh_5.8.1-1_amd64.deb ...
Unpacking zsh (5.8.1-1) ...
Setting up zsh-common (5.8.1-1) ...
Setting up zsh (5.8.1-1) ...
Processing triggers for man-db (2.10.2-1) ...

设置默认shell

$ chsh -s /bin/zsh

新启动一个终端将有如下提示:

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 ---

选择2即可。

配置ohmyzsh

克隆清华的ohmyzsh仓库

$ git clone https://mirrors.tuna.tsinghua.edu.cn/git/ohmyzsh.git
Cloning into 'ohmyzsh'...
remote: Enumerating objects: 31766, done.
remote: Counting objects: 100% (8793/8793), done.
remote: Compressing objects: 100% (2787/2787), done.
remote: Total 31766 (delta 8178), reused 6006 (delta 6006), pack-reused 22973
Receiving objects: 100% (31766/31766), 8.55 MiB | 9.00 MiB/s, done.
Resolving deltas: 100% (18070/18070), done.
tdy@tangdaoyong ~ % ls -a
.	       Desktop	    Music		 Public			    .viminfo
..	       Documents    .nvidia-settings-rc  .pyenv			    .vscode
.bash_history  Downloads    ohmyzsh		 snap			    .xinputrc
.bash_logout   .gitconfig   .pam_environment	 .sogouinput		    .zcompdump
.bashrc        .gnupg	    Pictures		 .ssh			    .zsh_history
.cache	       .local	    .pki		 .sudo_as_admin_successful  .zshrc
.config        .mailmaster  .presage		 Templates		    下载
.dbus	       matiastang   .profile		 Videos

执行安装脚本

$ cd ./ohmyzsh/tools
$ REMOTE=https://mirrors.tuna.tsinghua.edu.cn/git/ohmyzsh.git sh install.sh
Cloning Oh My Zsh...
remote: Enumerating objects: 1370, done.
remote: Counting objects: 100% (1370/1370), done.
remote: Compressing objects: 100% (1327/1327), done.
remote: Total 1370 (delta 34), reused 872 (delta 19), pack-reused 0
Receiving objects: 100% (1370/1370), 2.01 MiB | 2.61 MiB/s, done.
Resolving deltas: 100% (34/34), done.
From https://mirrors.tuna.tsinghua.edu.cn/git/ohmyzsh
 * [new branch]      master     -> origin/master
Branch 'master' set up to track remote branch 'master' from 'origin'.
Already on 'master'
/home/tdy/ohmyzsh/tools

Looking for an existing zsh config...
Found /home/tdy/.zshrc. Backing up to /home/tdy/.zshrc.pre-oh-my-zsh
Using the Oh My Zsh template file and adding it to /home/tdy/.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...
[sudo] password for tdy: 
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 Twitter: @ohmyzsh
 Join our Discord community: Discord server
 Get stickers, t-shirts, coffee mugs and more: Planet Argon Shop

安装Powerline字体

中文的显示可能有问题,所以我们需要下载并安装支持中文的字体,比如Powerline字体。

找一个目录下载字体

$ git clone https://github.com/powerline/fonts.git
Cloning into 'fonts'...
remote: Enumerating objects: 968, done.
remote: Total 968 (delta 0), reused 0 (delta 0), pack-reused 968
Receiving objects: 100% (968/968), 20.28 MiB | 373.00 KiB/s, done.
Resolving deltas: 100% (393/393), done.

安装字体

$ cd fonts
$ ./install.sh 
Copying fonts...
xargs: warning: options --max-args and --replace/-I/-i are mutually exclusive, ignoring previous --max-args value
Resetting font cache, this may take a moment...
Powerline fonts installed to /home/tdy/.local/share/fonts

注意:安装完字体后一定要应用。无论是原来的Terminal还是Termintor右键都会有属性配置,把字体设置成Ubuntu Mono derivative Powerline Bold[Regulor][Italic][Bold Italic]四种中的任意一种都可以。

参考

清华大学开源软件镜像站 ohmyzsh