ZSH
Zsh, On-my-zsh and their configs.
Note: This article will only focus on zsh itself. If you want to simply copy my computer configs (ALL OF THEM), you can refer to How to Set Up on A New Machine.
Zsh
Offical docs:
https://zsh.sourceforge.io/
https://zsh.sourceforge.io/Guide/zshguide.html
Install zsh:
1
sudo apt install zsh
By default, zsh will use
~/.zshrc
as config file.Switch your shell to zsh:
1
chsh -s /bin/zsh
Install and config Oh-my-zsh
Oh-my-zsh
Install it via -->repo:
1 | sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" |
Uninstall: If you want to uninstall oh-my-zsh
, just run uninstall_oh_my_zsh
from the command-line. It will remove itself and revert your previous bash
or zsh
configuration. (-->Source)
Details
The Oh-my-zsh itself will be installed at dir $HOME/.oh-my-zsh
, everytime you start zsh
, zsh
will start Oh-my-zsh. You can view this logic in the ~/.zshrc
file:
1 | Path to your oh-my-zsh installation. This includes your downloaded plugins. |
$ZSH/oh-my-zsh.sh
定义了一些环境变量:
ZSH_CUSTOM
: 默认为$ZSH/custom
, 即~/.oh-my-zsh/custom
1
2
3# Set ZSH_CUSTOM to the path where your custom config files Haltarys, 2023-06-21 - fix(docker): use `command docker` (#11766) 1 # and plugins exists, or else we will use the default custom/
if [[ -z "$ZSH_CUSTOM" ]]; then ZSH_CUSTOM="$ZSH/custom"
fi
Install: For China users
对于国内用户:
可能遇到报错: Failed to connect to raw.githubusercontent.com port 443 after 7 ms: Couldn't connect to server.
这是因为域名被墙了导致的. 可以设置代理再重新下载:
1 | export https_proxy=http://127.0.0.1:7890 |
(7890
换成你用的VPN对https代理所用的实际端口)
从gitee下载oh-my-zsh的安装脚本:
1
wget https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh
执行
install.sh
install.sh
会clone github的Oh-my-zsh仓库. 如果访问不了github, 可以修改脚本, 把仓库地址换成gitee的:vim install.sh
找到以下部分:
1
2
3
4
5Default settings
ZSH=${ZSH:-~/.oh-my-zsh}
REPO=${REPO:-ohmyzsh/ohmyzsh}
REMOTE=${REMOTE:-https://github.com/${REPO}.git}
BRANCH=${BRANCH:-master}将中间两行改为:
1
2REPO=${REPO:-mirrors/oh-my-zsh}
REMOTE=${REMOTE:-https://gitee.com/${REPO}.git}
Plugins Installation
Use Oh-My-Zsh to manage plugins.
如果使用brew, yay等包管理工具安装插件, 会把插件安装在
$ZSH/plugins
,这些插件被称为""standard plugins"如果使用git clone安装插件, 则文件需要被clone到到
$ZSH_CUSTOM/plugins
, 这些插件被称为"custom plugins", .所有插件在安装完毕后,都需要在
~/.zshrc
的plugins
中配置:1
plugins=( [custom plugins...] zsh-syntax-highlighting autosuggestion)
修改完配置文件后记得
source ~/.zshrc
, 然后重新启动shell
zsh-autosuggestions
How to install
这里直接用环境变量+重定向,不需要在指定目录下clone了
Clone this repository into
$ZSH_CUSTOM/plugins
(by default~/.oh-my-zsh/custom/plugins
)1
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
Add the plugin to the list of plugins for Oh My Zsh to load (inside
~/.zshrc
):1
2
3
4plugins=(
# other plugins...
zsh-autosuggestions
)Start a new terminal session.
zsh-syntax-highlighting
How to install
Clone this repository in oh-my-zsh's plugins directory:
1
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
Activate the plugin in
~/.zshrc
:1
plugins=( [plugins...] zsh-syntax-highlighting)
Restart zsh (such as by opening a new instance of your terminal emulator).
Note: There're conflicts between zsh-syntax-highlighting and ligature. So if you want to use one of them, you must give up the other.
fasd & autojump
安装
fasd
: 它将文件和目录按最近最常用算法排序, 并提供各种操作1
yay -S fasd
安装后需要配置
~/.zshrc
:1
2
3
4
5
6
7
8alias a='fasd -a' # any
alias s='fasd -si' # show / search / select
alias d='fasd -d' # directory
alias f='fasd -f' # file
alias sd='fasd -sid' # interactive directory selection
alias sf='fasd -sif' # interactive file selection
alias z='fasd_cd -d' # cd, same functionality as j in autojump
alias zz='fasd_cd -d -i' # cd with interactive selectionalias使用:
1
2
3
4
5
6
7
8a foo 列出最近操作的路径匹配 'foo' 的文件与目录
f foo 列出最近操作的路径匹配 'foo' 的文件
d foo 列出最近操作的路径匹配 'foo' 的目录
s foo 列出最近操作的路径匹配 'foo' 的文件与目录,并可以通过序号选择
sf foo 列出最近操作的路径匹配'foo'的文件,并可以通过序号选择
sd foo 列出最近操作的路径匹配'foo'的目录,并可以通过序号选择
z foo cd到最近操作的匹配'foo'并且得分最高的目录
zz foo 列出最近操作的路径匹配'foo'的目录,通过序号选择,然后cd进目录e.g. If you often go to
~/files/cool_project
you can simply usez cool
to jump there.autojump
提供了j
命令, 效果和fasd
的z
命令一样, 可以作为fasd
的替代品:1
yay -S autojump
Zsh Themes
Currently I have 2 themes. You can choose either.
Dracula
Dracula: 该网站收集了Dracula主题配色方案, 可以应用于各种终端模拟器和Shell.
- Dracula for Zsh
Install using Git:
Clone.
1
git clone https://github.com/dracula/zsh.git
Move an rename it to some place. For me, I move it under my dotfiles folder(
$DOT_FILE_HOME
) aszsh-dracula_theme
1
mv zsh $DOT_FILE_HOME/zsh-dracula_theme
Create env vatiable for it in
~/.zshrc
:1
export DRACULA_THEME="$DOT_FILE_HOME/zsh-dracula_theme"
And creating a symbolic link to oh-my-zsh's theme folder:
1
ln -s $DRACULA_THEME/dracula.zsh-theme $OH_MY_ZSH/themes/dracula.zsh-theme
Note, you must set
$OH_MY_ZSH
as oh-my-zsh's theme folder first.By default, the theme folder is already defined in
~/.zshrc
:1
export ZSH="$HOME/.oh-my-zsh"
$OH_MY_ZSH
is just the same as it. So append in~/.zshrc
:1
export OH_MY_ZSH=$ZSH
Go to your
~/.zshrc
file and setZSH_THEME="dracula"
.
Customization
https://github.com/dracula/zsh
官网上给了一些自定义的配置方式, 我只选择增加username显示.
In ~/.zshrc
:
1 | export DRACULA_DISPLAY_CONTEXT=1 |
powlevel10k
Github Repo. 上面也给了中国大陆的下载方式.
Clone the repository:
1
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
Set
ZSH_THEME="powerlevel10k/powerlevel10k"
in~/.zshrc
Enable the update:
1
source ~/.zshrc
Since you've installed
p10k
, an interative program (p10k configure
) will be created after this step. Make your custimization in that program. You can also manually start config:1
p10k configure
The config file of powlevel10k generated by
p10k configure
is~/.p10k.zsh
.