❤️💕💕CS自学指南,大学教育无论是深度还是广度都没有办法支撑我们的职业素养,这个板块算是自己在CS学习中额外补充和记录的。个人博客:http://nsddd.top
[TOC]
不是说Ubuntu的原装界面和终端不好用,而是我们确实可以通过工具来提高界面和终端的质量,从而实现使用的便捷和效率的提升。
在我们美化 Linux 终端前,我们先了解一下以下几个概念:
- 终端(terminal):拥有键盘+显示器的设备。
- 终端模拟器(terminal emulator):采用图形界面模拟 终端 操作的程序。
- shell:操作系统和终端之间的一个“壳”应用,可以将终端发送的字符(命令)发送给系统,并将系统运行命令的结果传递给终端显示。
- Bash:大多数类 UNIX 系统的默认 shell 程序。
sudo apt install gnome-tweak-tool
图形工具,可以选择不要,直接跳过,我的是 CLI 模式
我们尝试使用脚本,也就是命令行来将上面繁杂的过程自动化:
#!/bin/bash
sudo apt install gnome-tweak-tool # 安装美化工具
wget https://welts.xyz/file/Mojave-light-alt.tar # 下载美化主题
sudo mv Mojave-light-alt.tar /usr/share/themes # 移动到主题文件夹
cd /usr/share/themes
sudo tar xvf Mojave-light-alt.tar # 解压文件夹
sudo mv Mojave-light-alt.tar # 移除不必要的文件
cd -
wget https://github.com/keeferrourke/la-capitaine-icon-theme/archive/refs/tags/v0.6.1.zip # 获取图标文件夹
sudo mv v0.6.1.zip /usr/share/icons # 移动到图标文件夹
cd /usr/share/icons
sudo unzip v0.6.1.zip # 解压缩文件
但真正要切换主题,还得打开gnome-tweak-tool
进行切换。
在终端输入命令 echo $SHELL
查看终端类型
echo $SHELL
查看用户默认shell
cat /etc/passwd
查看系统支持shell类型
cat /etc/shells
选择默认终端
chsh -s /bin/zsh root
如有必要,请按照以下步骤安装 Zsh:
- Zsh的安装主要有两种方式:
- 使用您选择的包管理器,例如
sudo apt install zsh
(有关更多示例,请参见下文) - 从源代码开始,遵循Zsh FAQ 中的说明。
- 使用您选择的包管理器,例如
- 通过运行验证安装
zsh --version
。预期结果:zsh 5.0.8
或更新。 - 将其设为默认 shell:
chsh -s $(which zsh)
- 请注意,如果 Zsh 不在您的授权 shell 列表 (
/etc/shells
) 中或者您无权使用chsh
. 如果是这种情况,您将需要使用不同的程序。
- 请注意,如果 Zsh 不在您的授权 shell 列表 (
- 注销并重新登录以使用新的默认 shell。
- 测试它是否适用于
echo $SHELL
. 预期结果:/bin/zsh
或类似。 - 测试
$SHELL --version
。预期结果:'zsh 5.8' 或类似
Ubuntu 可以直接用 apt:
sudo apt install zsh
当你在终端看到一个大大的
____ / /_ ____ ___ __ __ ____ _____/ /_
/ __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \
/ /_/ / / / / / / / / / / /_/ / / /_(__ ) / / /
\____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/
/____/
检查一下:
zsh --version
# ==> Expected result: zsh 5.4.2 or more recent
说明你已经安装成功啦!
设置为终端默认 shell:
chsh -s $(which zsh)
如果设置失败,则进行如下设置:
# ~/.bash_profile 添加如下内容
export SHELL=`which zsh`
[ -z "$ZSH_VERSION" ] && exec "$SHELL" -l
zsh的功能极其强大,只是配置过于复杂,起初只有极客才在用。后来,有个穷极无聊的程序员可能是实在看不下去广大猿友一直只能使用单调的bash, 于是他创建了一个名为oh-my-zsh的开源项目…
自此,只需要简单的安装配置,小白程序员们都可以用上高档大气上档次,狂拽炫酷吊炸天的oh my zsh
# 使用 curl
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# 或者使用 wget
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# 或者手动进行安装
curl -Lo install.sh https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
sh install.sh
当安装完成 Oh-My-Zsh 后,会发现创建了~/.zshrc
文件,这就是 zsh 的配置文件。
Oh-My-Zsh 为 zsh 提供了很多的插件(Plugin)和主题(Theme)配置。
- 插件(Plugin):zsh 插件,简单来说,就是 zsh 可以解释执行的一段 shell 脚本。因此,一个插件可以简单地执行一些命令,也可以提供对特定命令和函数的补全功能。
要使用 插件,首先需要使能插件功能,方法如下:
在~/.zshrc
文件中,添加如下内容:
plugins=(
git
adb
ruby
)
如上就是添加了 git,adb,ruby 插件,更多其他插件,请参考:zsh Plugins
/home/username/.zshrc
文件中是关于zsh的配置脚本,你可以理解为,每当启动zsh
程序时,都会执行这个脚本. 在其中,你还可以更换zsh的主题:
要使用 主题,首先需要使能该功能,方法如下:
在~/.zshrc
文件中,设置ZSH_THEME
为要使用的具体主题名,比如:
ZSH_THEME=robbyrussell
如上就是让 zsh 启用robbyrussell
主题。更多其他主题,请参考:zsh Themes。
注:如果不想使用主题,直接设置:ZSH_THEME=""
这里推荐一个主题:powerlevel10k,安装方法如下:
# 首先下载到 on-my-zsh 的本地目录
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git $ZSH_CUSTOM/themes/powerlevel10k
# ~/.zshrc 设置如下内容
ZSH_THEME="powerlevel10k/powerlevel
通过设置ZSH_THEME
,我们能够切换主题,更多主题参考oh-my-zsh官网
Oh-My-Zsh 对插件和主题的配置虽然很简单,但是对于非内置组件,我们还需要进行手动下载,再进行配置。而这些步骤,完全可以进行自动化。
因此,网络上其实已存在很多的 zsh 的插件管理器,比如:antigen,zgen,zplug...
zinit 是当前加载速度最快的 zsh 插件管理器,并且随着插件的数量增多,zinit 的优势会更大。
zinit 的插件加载速度快的一个主要原因就是:其提供了一个 插件延迟加载(Turbo) 的功能,使得插件可以在 zsh 启功后,再进行加载。
source <(curl -sL init.zshell.dev); zzinit
推荐将
init.zshell.dev
下载到本地,并且放在/root/.oh-my-zsh/init.zsh
# ~/.zsh source /root/.oh-my-zsh/init.zsh; zzinit
安装完成 zinit 后,我们就可以在~/.zshrc
中配置插件/主题,比如,对于上文建议使用的 powerlevel10k 主题,其安装方法如下:
- 在
~/.zshrc
文件中配置主题为 powerlevel10k:
# ~/.zshrc
# 原生的主题配置可去除
# ZSH_THEME="powerlevel10k/powerlevel
zinit ice depth=1; zinit light romkatv/powerlevel10k
- 重新加载
~/.zshrc
:
source ~/.zshrc
Robby 的主题是默认主题。这不是最奇特的。这不是最简单的。这只是正确的(对他而言)。
💡 注意:许多主题需要安装Powerline Font或Nerd Font才能正确呈现。没有它们,这些主题将呈现奇怪的提示符号
sudo apt-get install fonts-powerline
or:
# clone
git clone https://ghproxy.com/https://github.com/powerline/fonts.git --depth=1
cd fonts &&./install.sh
# clean-up a bit
cd .. && rm -rf fonts
Nerd Font:
mkdir -p ~/.local/share/fonts
cd ~/.local/share/fonts && curl -fLo "Droid Sans Mono for Powerline Nerd Font Complete.otf" https://github.com/ryanoasis/nerd-fonts/raw/HEAD/patched-fonts/DroidSansMono/complete/Droid%20Sans%20Mono%20Nerd%20Font%20Complete.otf
如果您觉得精力充沛,可以让计算机在您每次打开新的终端窗口时为您随机选择一个。
ZSH_THEME="random" # (...please let it be pie... please be some pie..)
如果你想从你最喜欢的主题列表中选择随机主题:
ZSH_THEME_RANDOM_CANDIDATES=(
"robbyrussell"
"agnoster"
)
如果您只知道不喜欢哪些主题,则可以将它们类似地添加到忽略列表中:
ZSH_THEME_RANDOM_IGNORED=(pygmalion tjkirch_mod)
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"
# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="Afowler"
# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in $ZSH/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )
# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"
# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"
# Uncomment one of the following lines to change the auto-update behavior
# zstyle ':omz:update' mode disabled # disable automatic updates
# zstyle ':omz:update' mode auto # update automatically without asking
# zstyle ':omz:update' mode reminder # just remind me to update when it's time
# Uncomment the following line to change how often to auto-update (in days).
# zstyle ':omz:update' frequency 13
# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"
# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"
# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"
# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"
# Uncomment the following line to display red dots whilst waiting for completion.
# You can also set it to another string to have that shown instead of the default red dots.
# e.g. COMPLETION_WAITING_DOTS="%F{yellow}waiting...%f"
# Caution: this setting can cause issues with multiline prompts in zsh < 5.7.1 (see #5765)
# COMPLETION_WAITING_DOTS="true"
# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"
# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"
# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder
# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
source $ZSH/oh-my-zsh.sh
# User configuration
# export MANPATH="/usr/local/man:$MANPATH"
# You may need to manually set your language environment
# export LANG=en_US.UTF-8
# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
# export EDITOR='vim'
# else
# export EDITOR='mvim'
# fi
source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
# Compilation flags
# export ARCHFLAGS="-arch x86_64"
# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
#
plugins=(
git
adb
ruby
wakatime
bundler
dotenv
macos
rake
rbenv
zsh-autosuggestions
zsh-syntax-highlighting
sudo
)
#ZSH_THEME="powerlevel10k/powerlevel
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
# ~/.zsh
source /root/.oh-my-zsh/init.zsh; zzinit
#source /etc/profile
# cd $WORKSPACE;ls
alias k=kubectl
zinit ice depth=1; zinit light romkatv/powerlevel10k
# ZSH_THEME="random" # (...please let it be pie... please be some pie..)
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
虽然但是,oh-my-zsh并没有自带命令高亮插件,我们需要自行下载:
sudo apt install zsh-syntax-highlighting
echo "source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ~/.zshrc # 使插件生效
自动补全插件:
git clone https://ghproxy.com/https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
zsh-syntax-highlighting
官网,命令太多,有时候记不住,等输入完了才知道命令输错了,这个插件直接在输入过程中就会提示你,当前命令是否正确,错误红色,正确绿色
git clone https://ghproxy.com/https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
SUDO:
偶尔输入某个命令,提示没有权限,需要加sudo,这个时候按两下ESC,就会在命令行头部加上sudo
git 插件:
也是自带的,但是使用有点意思:
# 终端内
# gb 等于 git branch
# st 等价git status
# gcmsg 等于 git commit -m
# gm 等于git merge
# gco 等于git checkout
自动补全插件:
git clone https://ghproxy.com/https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
zsh-autosuggestions 显示之前运行的命令, 按<control + e>
即可补全.