Zsh 配置

本文最后更新于:2024年3月18日 凌晨

Zsh 配置

  • zsh 的配置主要集中在~/.zshrc

别名设置

  • zsh不仅可以设置通用别名,还能针对文件类型设置对应的打开程序,比如。
    • alias -s html=vi,意思就是你在命令行输入 hello.html,zsh会为你自动打开vim并读取hello.html
    • alias -s gz='tar -xzvf',表示自动解压后缀为gz的压缩包。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
alias cls='clear'
alias ll='ls -l'
alias la='ls -a'
alias vi='vim'
alias javac="javac -J-Dfile.encoding=utf8"
alias grep="grep --color=auto"
alias -s html=vi # 在命令行直接输入后缀为 html 的文件名,会在 vim 中打开。
alias -s rb=vi # 在命令行直接输入 ruby 文件,会在 vim 中打开。
alias -s py=vi # 在命令行直接输入 python 文件,会用 vim 中打开,以下类似。
alias -s js=vi
alias -s c=vi
alias -s java=vi
alias -s txt=vi
alias -s gz='tar -xzvf'
alias -s tgz='tar -xzvf'
alias -s zip='unzip'
alias -s bz2='tar -xjvf'

主题设置

  • oh my zsh 提供了数十种主题,相关文件在~/.oh-my-zsh/themes目录下。
  • ZSH_THEME="random"主题设置为随机,这样我们每打开一个窗口,都会随机在默认主题中选择一个。
  • 安装powerlevel10k主题。
1
2
3
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
# 或
brew install powerlevel10k
  • ~/.zshrc
1
ZSH_THEME="powerlevel10k/powerlevel10k"
  • 配置主题。
1
$ p10k configure
  • 安装字体。
1
https://aur.archlinux.org/packages/ttf-meslo-nerd-font-powerlevel10k/

插件设置

  • oh my zsh项目提供了完善的插件体系,相关的文件在~/.oh-my-zsh/plugins目录下,默认提供了100多种。
  • 想了解每个插件的功能,只要打开相关目录下的 zsh 文件。
  • 插件也是在~/.zshrc里配置,找到plugins关键字,就可以加载自己的插件了,系统默认加载git,你可以在后面追加内容,如下:
1
2
3
4
5
6
7
8
9
10
11
12
plugins=(git
docker
docker-compose
mvn
node
npm
yarn
sudo
zsh-syntax-highlighting
zsh-autosuggestions
zsh-completions
autojump)
  • 安装 zsh-autosuggestions
1
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
  • 安装 zsh-syntax-highlighting
1
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
  • 安装autojump
1
https://github.com/wting/autojump
  • 安装zsh-completions
1
git clone https://github.com/zsh-users/zsh-completions  ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-completions

vicmd

  • 在shell中使用vim操作。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# vicmd
bindkey -v
bindkey -M vicmd "i" vi-insert
bindkey -M vicmd "I" vi-insert-bol
bindkey -M vicmd "h" vi-backward-char
bindkey -M vicmd "l" vi-forward-char
bindkey -M vicmd "H" vi-beginning-of-line
bindkey -M vicmd "L" vi-end-of-line
bindkey -M vicmd "j" down-line-or-history
bindkey -M vicmd "k" up-line-or-history
bindkey -M vicmd "u" undo
#bindkey -M vicmd "-" vi-rev-repeat-search
bindkey -M vicmd "=" vi-repeat-search

function zle-keymap-select {
if [[ ${KEYMAP} == vicmd ]] || [[ $1 = 'block' ]]; then
echo -ne '\e[1 q'
elif [[ ${KEYMAP} == main ]] || [[ ${KEYMAP} == viins ]] || [[ ${KEYMAP} = '' ]] || [[ $1 = 'beam' ]]; then
echo -ne '\e[5 q'
fi
}
zle -N zle-keymap-select

代理配置

手动配置

  • 在Terminal中的输入。
1
export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890

自动配置

  • 需要将环境变量写入终端的配置当中。
1
2
3
4
5
6
7
8
9
10
#开启代理
function proxy_on(){
export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890
echo -e "proxy on";
}
# 关闭代理
function proxy_off(){
unset https_proxy http_proxy all_proxy;
echo -e "proxy off";
}

检查代理

1
2
3
curl ip.gs
curl ipinfo.io
wget https://www.dropbox.com -v -O /dev/null

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!