关系
先说下这几样东西的关系:
zsh是一个类似bash的终端。 https://www.zsh.org
ohmyzsh是一个管理zsh配置文件的东西(框架),自带一些主题插件云云。https://github.com/robbyrussell/oh-my-zsh
autojump是一个快速cd工具,可以自动记录你经常cd的目录,实现简短指令就能到达。并不是一定依赖zsh。https://github.com/wting/autojump
fzf是一个文件搜索工具,类似Windows下的everything,同样并不是一定依赖zsh。https://github.com/junegunn/fzf
zsh-git-prompt是一个能够展示更好的git信息的工具,看名字就知道必须zsh。https://github.com/olivierverdier/zsh-git-prompt
zsh的安装
1 2 |
yum install -y zsh #安装zsh chsh -s /usr/bin/zsh #启用zsh |
oh-my-zsh的安装
1 |
wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh |
个人使用的主题,基于clean+fishy,加上zsh-git-prompt。
修改ZSH_THEME=”wbb”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
_fishy_collapsed_wd() { echo $(pwd | perl -pe ' BEGIN { binmode STDIN, ":encoding(UTF-8)"; binmode STDOUT, ":encoding(UTF-8)"; }; s|^$ENV{HOME}|~|g; s|/([^/.])[^/]*(?=/)|/$1|g; s|/\.([^/])[^/]*(?=/)|/.$1|g ') } if [ $UID -eq 0 ]; then NCOLOR="red"; else NCOLOR="white"; fi #PROMPT='%{$fg[$NCOLOR]%}%B%n%b%{$reset_color%}:%{$fg[blue]%}%B%c/%b%{$reset_color%} $(git_prompt_info)%(!.#.$) ' #PROMPT='%{$fg[blue]%}%B%~/%b%{$reset_color%} $(git_prompt_info)%(!.#.$) ' PROMPT='%{$fg[blue]%}%B$(_fishy_collapsed_wd)%b%{$reset_color%} $(git_super_status)%(!.#.$) ' RPROMPT='[%*]' # LS colors, made with https://geoff.greer.fm/lscolors/ export LSCOLORS="Gxfxcxdxbxegedabagacad" export LS_COLORS='no=00:fi=00:di=01;34:ln=00;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=41;33;01:ex=00;32:*.cmd=00;32:*.exe=01;32:*.com=01;32:*.bat=01;32:*.btm=01;32:*.dll=01;32:*.tar=00;31:*.tbz=00;31:*.tgz=00;31:*.rpm=00;31:*.deb=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.lzma=00;31:*.zip=00;31:*.zoo=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.tb2=00;31:*.tz2=00;31:*.tbz2=00;31:*.avi=01;35:*.bmp=01;35:*.fli=01;35:*.gif=01;35:*.jpg=01;35:*.jpeg=01;35:*.mng=01;35:*.mov=01;35:*.mpg=01;35:*.pcx=01;35:*.pbm=01;35:*.pgm=01;35:*.png=01;35:*.ppm=01;35:*.tga=01;35:*.tif=01;35:*.xbm=01;35:*.xpm=01;35:*.dl=01;35:*.gl=01;35:*.wmv=01;35:*.aiff=00;32:*.au=00;32:*.mid=00;32:*.mp3=00;32:*.ogg=00;32:*.voc=00;32:*.wav=00;32:' |
最终效果是可以简短展示路径、可以显示git状态。
简单不卡版: PROMPT='%B%F{red}%m %B%F{blue}%~%f%b $(git_prompt_info)%f%b# '
目前常用版:PROMPT='%B%F{red}%m %B%F{blue}%~%f%b # '
zsh-autosuggestions安装
这个插件可以根据你的history结合tab来加快指令输入,并自动提示
1 |
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions |
autojump安装
1 2 3 |
git clone https://github.com/wting/autojump.git cd autojump ./install.py |
安装完会出提示,把两行内容复制粘贴到.zshrc后source一下就生效。
简单使用方法:
j:进入权重最高的目录(经常去的目录)
j -s:查看目录权重列表
j m: 进入包含”m”字符的权重最高的目录
j m之后按tab:可以自动列出所有含”m”字符的目录供选择。
j -d:当前目录权重-10
j -i:当前目录权重+2
zsh-git-prompt安装
1 2 3 4 5 6 |
#先在root目录clone项目 git clone https://github.com/olivierverdier/zsh-git-prompt.git #在.zshrc中添加 source /root/zsh-git-prompt/zshrc.sh export ZSH_THEME_GIT_PROMPT_CACHE=yes #这行的作用是开启git缓存 |
这样,前面主题中的git_super_status就有效果了。
fzf安装
1 2 3 4 5 |
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf ~/.fzf/install #fzf会自动添加内容到.zshrc,source一下就生效: source ~/.zshrc |
简单用法介绍
按ctrl+t,会出来一个列表可以选择/搜索目录下任何文件,选中后自动输入,所以可以结合各种指令使用。
按alt+c,会出来一个列表可以选择/搜索目录下的任何目录,不需要结合指令,选中后自动进入目录。
小键盘绑定
.zshrc中添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# Keypad # 0 . Enter bindkey -s "^[Op" "0" bindkey -s "^[Ol" "." bindkey -s "^[OM" "^M" # 1 2 3 bindkey -s "^[Oq" "1" bindkey -s "^[Or" "2" bindkey -s "^[Os" "3" # 4 5 6 bindkey -s "^[Ot" "4" bindkey -s "^[Ou" "5" bindkey -s "^[Ov" "6" # 7 8 9 bindkey -s "^[Ow" "7" bindkey -s "^[Ox" "8" bindkey -s "^[Oy" "9" # + - * / bindkey -s "^[Ok" "+" bindkey -s "^[Om" "-" bindkey -s "^[Oj" "*" bindkey -s "^[Oo" "/" |
Home和end失效的办法
.zshrc中添加:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#Rebind HOME and END to do the decent thing: bindkey '\e[1~' beginning-of-line bindkey '\e[4~' end-of-line case $TERM in (xterm*) bindkey '\eOH' beginning-of-line bindkey '\eOF' end-of-line esac #To discover what keycode is being sent, hit ^v #and then the key you want to test. #And DEL too, as well as PGDN and insert: bindkey '\e[3~' delete-char bindkey '\e[6~' end-of-history bindkey '\e[2~' redisplay #Now bind pgup to paste the last word of the last command, bindkey '\e[5~' insert-last-word |
Centos6下可安装最新版本zsh
1 |
yum install http://mirror.ghettoforge.org/distributions/gf/el/6/plus/x86_64/zsh-5.0.7-5.gf.el6.x86_64.rpm |
2025年备注
小键盘等问题,使用linux模拟器可以解决,termius客户端默认解决。xshell在连接设置中将终端机类型修改为linux即可解决。
自用的安装脚本(由chatgpt协助生成):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#!/bin/bash # 1. 安装必要的软件包 echo "安装 zsh 和 git..." yum install -y zsh git # 2. 获取 zsh 路径 ZSH_PATH=$(command -v zsh) # 3. 修改默认 shell USER=$(whoami) if command -v chsh >/dev/null 2>&1; then echo "使用 chsh 修改 $USER 的默认 shell 为 zsh..." chsh -s "$ZSH_PATH" "$USER" else echo "chsh 不存在,直接修改 /etc/passwd..." sed -i "s|^$USER:.*:/bin/bash|$USER:x:$(id -u):$(id -g)::$HOME:$ZSH_PATH|" /etc/passwd fi # 4. 安装 Oh My Zsh(国内 Gitee 镜像) if [ ! -d "$HOME/.oh-my-zsh" ]; then echo "安装 Oh My Zsh..." git clone https://gitee.com/mirrors/oh-my-zsh.git ~/.oh-my-zsh cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc fi # 5. 安装 zsh-autosuggestions 插件(国内 Gitee 镜像) ZSH_CUSTOM=${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom} ZSH_AUTOSUGGESTIONS="$ZSH_CUSTOM/plugins/zsh-autosuggestions" if [ ! -d "$ZSH_AUTOSUGGESTIONS" ]; then echo "安装 zsh-autosuggestions 插件..." git clone https://gitee.com/mirrors/zsh-autosuggestions.git "$ZSH_AUTOSUGGESTIONS" fi # 6. 配置 zsh 主题(修改 .zshrc) ZSHRC="$HOME/.zshrc" THEME_PATH="$HOME/.oh-my-zsh/themes/wbb.zsh-theme" echo "修改 .zshrc,设置主题为 wbb..." sed -i 's|^ZSH_THEME=.*|ZSH_THEME="wbb"|' "$ZSHRC" sed -i 's|^plugins=(.*)|plugins=(zsh-autosuggestions)|' "$ZSHRC" # 7. 创建自定义主题 wbb.zsh-theme echo "创建 zsh 主题 wbb..." mkdir -p "$HOME/.oh-my-zsh/themes" cat > "$THEME_PATH" << EOF PROMPT='%B%F{red}%m %B%F{blue}%~%f%b # ' EOF echo "所有步骤完成。" |