零、 效果展示
目录
[TOC]
注意:此脚本已和优化vim的脚本合并到终端美化:zsh + vim
本次使用oh my zsh
来美化终端,如果你原来用的是bash
,则需要安装zsh
,具体方法如下:
一、脚本一键美化
直接把所有步骤改成bash脚本了
此脚本仅在下列平台测试并成功运行:
Debian
- debian-11.6[x86_64]
- debian-11.6[aarch64]
CentOS
- centos-7.9.2009[x86_64]
- centos-8.5.2111[x86_64]
Ubuntu
- ubuntu-22.04 [aarch64]
- ubuntu-20.04 [x86_64]
- ubuntu-20.04 [aarch64]
致mac用户:主要是为了各位macOS用户着想,下面的一键脚本中特意删除了适配macOS的部分,跟着下面的教程手动练练也好。
1.1 脚本使用方法
1.首先执行
cd ~&&touch zsh-enhancer.sh&&chmod +x zsh-enhancer.sh
2.然后通过
vim zsh-enhancer.sh
把下面的脚本复制进
zsh-enhancer.sh
vim进去后,需要先
:set paste
,然后回车,然后点击i
,然后control+v或者command+v
粘贴,然后esc
然后:wq
然后回车,然后看下一步。3.最后直接运行就可
bash zsh-enhancer.sh
1.2脚本内容
#!/bin/bash
set -e
function check_and_install_package_manager() {
local package_managers=()
if [[ "$1" == "linux-gnu"* ]]; then
if [[ -x "$(command -v apt-get)" ]]; then
package_managers+=("sudo apt-get update && sudo apt-get install -y apt")
fi
if [[ -x "$(command -v yum)" ]]; then
package_managers+=("sudo yum install -y yum-utils")
fi
if [[ -x "$(command -v dnf)" ]]; then
package_managers+=("sudo dnf install -y dnf-plugins-core")
fi
if [[ -x "$(command -v zypper)" ]]; then
package_managers+=("sudo zypper -n install -y zypper")
fi
else
echo "错误:不支持的操作系统类型。" >&2
exit 1
fi
for cmd in "${package_managers[@]}"; do
eval "$cmd"
done
}
function no_macOS(){
if [[ "$OSTYPE" == "darwin"* ]]; then
echo "坦白了,适配macOS的脚本太多报错了,我搞不定,还请跟着下面的教程手动操作"
exit 1
fi
}
function install_package() {
local package=$1
if [ ! -x "$(command -v $package)" ]; then
if [[ -x "$(command -v apt-get)" ]]; then
sudo apt-get update && sudo apt-get install -y $package
elif [[ -x "$(command -v yum)" ]]; then
sudo yum install -y $package
elif [[ -x "$(command -v dnf)" ]]; then
sudo dnf install -y $package
elif [[ -x "$(command -v zypper)" ]]; then
sudo zypper -n install -y $package
else
echo "错误:未安装 $package 并且找不到软件包管理器。" >&2
exit 1
fi
fi
}
no_macOS
# 检查和安装软件包管理器
check_and_install_package_manager $OSTYPE
# 检查和安装软件包
packages=("curl" "wget" "git" "sed")
for pkg in "${packages[@]}"; do
install_package "$pkg"
done
# 安装 zsh
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
if command -v apt > /dev/null; then
sudo apt update
sudo apt -y install zsh
elif command -v yum > /dev/null; then
sudo yum -y install zsh
else
echo "不支持的 Linux 发行版"
exit 1
fi
else
echo "不支持的操作系统类型"
exit 1
fi
# 安装 oh-my-zsh
## 不管以前有没有安装过,反正先把oh-my-zsh的目录删了
rm -rf ~/.oh-my-zsh
# 下载安装脚本
wget -O install.sh https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
# 删除与重写询问语句
sed -i '/read -r/d' install.sh
echo "ans='n'" >> install.sh
sed -i 's/exec zsh -l/\# &/' install.sh
# 添加 "-y" 选项以自动接受所有其他默认设置
sed -i '/export ZSH=/a RUNZSH="no"\nKEEP_ZSHRC="yes"\nCHSH="no"\nDISABLE_UPDATE_PROMPT="true"\nDISABLE_AUTO_UPDATE="true"\nSKIP_CHSH="yes"' install.sh
# 运行安装脚本
sh install.sh
# 清理安装脚本
rm install.sh
# 将主题改为 ys
sed -i '/^ZSH_THEME=/ s/=.*$/="ys"/' ~/.zshrc
#防止中文乱码
sed -i 's/^# export LANG=en_US.UTF-8$/export LC_ALL=en_US.UTF-8\nexport LANG=en_US.UTF-8/' ~/.zshrc
# 安装插件
sed -i -E 's/^(\s*plugins=\([^\)]*\))/plugins=(command-not-found extract docker-compose zsh-syntax-highlighting zsh-autosuggestions zsh-completions)/' ~/.zshrc
# 下载并安装 zsh 插件
mkdir -p ~/.oh-my-zsh/custom/plugins
cd ~/.oh-my-zsh/custom/plugins
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
git clone https://github.com/zsh-users/zsh-autosuggestions.git
git clone https://github.com/zsh-users/zsh-completions.git
cd ~
# 将默认 shell 改为 zsh
chsh -s $(which zsh)
echo "终端美化完成!"
# 删除本脚本
rm ~/zsh-enhancer.sh
# 开一个新的Zsh Shell终端
zsh
二、手动安装
使用脚本不行也可以按照下文手动安装
2.1 安装zsh
这里直接把文档粘贴过来了
一句话总结就是:
1.Debian/Ubuntu/CentOS 用apt/yum -y install zsh
,macOS用brew install zsh
2.完了再执行chsh -s $(which zsh)
3.重新ssh连接,echo $SHELL
回车,看见有zsh
三个字母就行了。
2.1.1 Zsh?
In order for Oh My Zsh to work, Zsh must be installed.
- Please run
zsh --version
to confirm. - Expected result:
zsh 5.0.8
or more recent
- Please run
Additionally, Zsh should be set as your default shell.
- Please run
echo $SHELL
from a new terminal to confirm. - Expected result:
/usr/bin/zsh
or similar
- Please run
2.1.2 Install and set up zsh as default
If necessary, follow these steps to install Zsh:
There are two main ways to install Zsh:
- With the package manager of your choice, e.g.
sudo apt install zsh
(see below for more examples) - From source, following the instructions from the Zsh FAQ.
- With the package manager of your choice, e.g.
- Verify installation by running
zsh --version
. Expected result:zsh 5.0.8
or more recent. Make it your default shell:
chsh -s $(which zsh)
or usesudo lchsh $USER
if you are on Fedora.Note that this will not work if Zsh is not in your authorized shells list (
/etc/shells
)
or if you don't have permission to usechsh
. If that's the case you'll need to use a different procedure.- If you use
lchsh
you need to type/bin/zsh
to make it your default shell.
- If you use
- Log out and log back in again to use your new default shell.
- Test that it worked with
echo $SHELL
. Expected result:/bin/zsh
or similar. - Test with
$SHELL --version
. Expected result: 'zsh 5.8' or similar
2.1.3 How to install zsh on many platforms
2.1.3.1 macOS
Try zsh --version
before installing it from Homebrew. Preferably newer than or equal to 5.0.8
.
brew install zsh
To set zsh as your default shell, execute the following assuming a default install of Homebrew
Recent macOS versions:
For m1 macs:
chsh -s /opt/homebrew/bin/zsh
For intel macs:
chsh -s /usr/local/bin/zsh
macOS High Sierra and older:
chsh -s /bin/zsh
Assuming you have Homebrew installed. If not, most versions of
macOS ship zsh by default, but it's normally an older version. Alternatively, you may
also use MacPorts
sudo port install zsh zsh-completions
Ubuntu, Debian & derivatives (Windows 10 WSL | Native Linux kernel with Windows 10 build 1903)
apt install zsh
If you don't have apt
, the recommended package manager for end users
[[1]](https://askubuntu.com/a/446484)
[[2]](https://askubuntu.com/a/775264)
[[3]](https://help.ubuntu.com/lts/serverguide/apt.html)
[[4]](https://www.howtogeek.com/234583/simplify-command-line-package-management-with-apt-instead-of-apt-get/)
, you can try apt-get
or aptitude
.
Other distributions that apply include:
Linux Mint, elementary OS, Zorin OS, Raspbian, MX Linux, Deepin.
OpenSUSE
zypper install zsh
Arch Linux or Manjaro
pacman -S zsh
Void Linux
xbps-install zsh
Fedora
dnf install zsh
OpenBSD
To install the package:
pkg_add zsh
FreeBSD
To install the package:
pkg install zsh
To install the port:
cd /usr/ports/shells/zsh/ && make install clean
To reduce memory usage, optionally enable zsh-mem options with
make config
before running "make install".
Centos/RHEL
sudo yum update && sudo yum -y install zsh
Cygwin
Install the zsh package using the installer. Unfortunately Cygwin doesn't have a standard command line interface. You could, however, setup apt-cyg and install zsh as follows:
apt-cyg install zsh
The easiest way to change the default shell is to set your SHELL user environment variable. Search for "Edit Environment variables for your account" to bring up the environment variables window, create a new variable named "SHELL" and give it the value "/usr/bin/zsh/".
Alternatively:
Open Cygwin (in BASH) then type:
sudo nano ~/.bashrc
Once the .bashrc file is open, add this line to the very top:
exec zsh
Close and save the file.
Close and reopen Cygwin.
It will execute the command every time you load the terminal and run your zsh shell.
Solus
eopkg it zsh
Funtoo/Gentoo
emerge app-shells/zsh
Alpine Linux
apk add zsh
MSYS2
pacman -S zsh
Termux (Android)
Termux is an terminal emulator for Android but has modern feature like Debian and Ubuntu (Termux has Bash shell and Busybox GNU-like programs). For the package manager, Termux using an Debian/Ubuntu package manager, APT.
To install the package, run this command:
pkg install zsh
The command looks like FreeBSD package manager (pkg
). Or you can run this command:
apt update && apt upgrade
apt install zsh
To set zsh as your default shell, run this command:
chsh -s zsh
KISS Linux
To install zsh, you must add the community repo to your $KISS_PATH
.
kiss b zsh && kiss i zsh
安装oh-my-zsh
下面三个安装方式选一个就行
wget
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
curl
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
fetch
sh -c "$(fetch -o - https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
执行完成其中一个即可看见oh-my-zsh的界面。
配置主题
在~/.zshrc
中第11行左右找到ZSH_THEME="robbyrussell"
,将其改为ZSH_THEME="ys"
,然后保存退出。
也可以使用其他主题,比如agnoster
,agnoster
等,所有主题列表与效果可查看这里。
配置插件
安装插件
在~/.zshrc
中第73行左右找到plugins=(git)
,将其改为plugins=(command-not-found extract docker-compose zsh-syntax-highlighting zsh-autosuggestions zsh-completions)
,然后保存退出。
也可自行选需要的其他插件,所有插件列表与效果可查看这里。
其中zsh-syntax-highlighting
, zsh-autosuggestions
, zsh-completions
需要手动下载:
这三个无脑安装就是了,所有人都能用到,超级好用
执行以下命令:
cd ~/.oh-my-zsh/custom/plugins
git clone https://github.com/zsh-users/zsh-syntax-highlighting
git clone https://github.com/zsh-users/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-completions
cd ~
最后source一下就能看见效果了:
source ~/.zshrc
本教程中安装的每个插件的介绍:
zsh-syntax-highlighting
:高亮命令行输入的命令,如果命令错误,会高亮显示错误的部分。zsh-autosuggestions
:自动提示命令,比如你输入git
,然后按下Tab
键,会自动提示git add
、git commit
、git push
等命令。zsh-completions
:自动补全命令,比如你输入git c
,然后按下Tab
键,会自动补全为git commit
。command-not-found
:当你输入一个命令,但是没有安装时,会自动提示你安装该命令。extract
:解压缩命令,比如你输入extract test.zip
,会自动解压缩test.zip
文件。docker-compose
:docker-compose命令补全。
extract插件命令介绍
extract plugin
This plugin defines a function called extract
that extracts the archive file you pass it, and it supports a
wide variety of archive filetypes.
This way you don't have to know what specific command extracts a file, you just do extract <filename>
and
the function takes care of the rest.
To use it, add extract
to the plugins array in your zshrc file:
plugins=(... extract)
Supported file extensions
Extension | Description |
---|---|
7z | 7zip file |
Z | Z archive (LZW) |
apk | Android app file |
aar | Android library file |
bz2 | Bzip2 file |
cab | Microsoft cabinet archive |
cpio | Cpio archive |
deb | Debian package |
ear | Enterprise Application aRchive |
gz | Gzip file |
ipa | iOS app package |
ipsw | iOS firmware file |
jar | Java Archive |
lrz | LRZ archive |
lz4 | LZ4 archive |
lzma | LZMA archive |
obscpio | cpio archive used on OBS |
rar | WinRAR archive |
rpm | RPM package |
sublime-package | Sublime Text package |
tar | Tarball |
tar.bz2 | Tarball with bzip2 compression |
tar.gz | Tarball with gzip compression |
tar.lrz | Tarball with lrzip compression |
tar.lz | Tarball with lzip compression |
tar.lz4 | Tarball with lz4 compression |
tar.xz | Tarball with lzma2 compression |
tar.zma | Tarball with lzma compression |
tar.zst | Tarball with zstd compression |
tbz | Tarball with bzip compression |
tbz2 | Tarball with bzip2 compression |
tgz | Tarball with gzip compression |
tlz | Tarball with lzma compression |
txz | Tarball with lzma2 compression |
tzst | Tarball with zstd compression |
war | Web Application archive (Java-based) |
xpi | Mozilla XPI module file |
xz | LZMA2 archive |
zip | Zip archive |
zst | Zstandard file (zstd) |
zpaq | Zpaq file |
See list of archive formats for more information
regarding archive formats.
docker-compose插件命令介绍
Docker-compose
This plugin provides completion for docker-compose as well as some
aliases for frequent docker-compose commands.
To use it, add docker-compose to the plugins array of your zshrc file:
plugins=(... docker-compose)
Aliases
Alias | Command | Description |
---|---|---|
dco | docker-compose | Docker-compose main command |
dcb | docker-compose build | Build containers |
dce | docker-compose exec | Execute command inside a container |
dcps | docker-compose ps | List containers |
dcrestart | docker-compose restart | Restart container |
dcrm | docker-compose rm | Remove container |
dcr | docker-compose run | Run a command in container |
dcstop | docker-compose stop | Stop a container |
dcup | docker-compose up | Build, (re)create, start, and attach to containers for a service |
dcupb | docker-compose up --build | Same as dcup , but build images before starting containers |
dcupd | docker-compose up -d | Same as dcup , but starts as daemon |
dcupdb | docker-compose up -d --build | Same as dcup , but build images before starting containers and starts as daemon |
dcdn | docker-compose down | Stop and remove containers |
dcl | docker-compose logs | Show logs of container |
dclf | docker-compose logs -f | Show logs and follow output |
dcpull | docker-compose pull | Pull image of a service |
dcstart | docker-compose start | Start a container |
dck | docker-compose kill | Kills containers |