zsh的使用

380 阅读3分钟

安装

rockylinux9先安装:

sudo dnf install util-linux-user -y
dnf  install epel-release 

文档地址:zhuanlan.zhihu.com/p/50464006
haoyep.com/posts/zsh-c…

  1. There are two main ways to install Zsh:

  2. Verify installation by running zsh --version. Expected result: zsh 5.0.8 or more recent.

  3. Make it your default shell: chsh -s $(which zsh) or use sudo lchsh $USER(rocky上) 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 use chsh. 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.
  4. Log out and log back in again to use your new default shell.

  5. Test that it worked with echo $SHELL. Expected result: /bin/zsh or similar.

  6. Test with $SHELL --version. Expected result: 'zsh 5.8' or similar

安装# Oh my zsh.

gist.github.com/dogrocker/1…

Install with curl

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

Enabling Plugins (zsh-autosuggestions & zsh-syntax-highlighting)

  • Download zsh-autosuggestions by

git clone https://github.com/zsh-users/zsh-autosuggestions.git $ZSH_CUSTOM/plugins/zsh-autosuggestions

  • Download zsh-syntax-highlighting by

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_CUSTOM/plugins/zsh-syntax-highlighting

  • nano ~/.zshrc find plugins=(git)
  • Append zsh-autosuggestions & zsh-syntax-highlighting to plugins() like this

plugins=(git zsh-autosuggestions zsh-syntax-highlighting)


强烈建议主题:

git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

# 中国用户可以使用 gitee.com 上的官方镜像加速下载
git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

在 ~/.zshrc 设置 ZSH_THEME="powerlevel10k/powerlevel10k"。接下来,终端会自动引导你配置 powerlevel10k。 1

sudo wget -O $ZSH_CUSTOM/themes/haoomz.zsh-theme https://cdn.haoyep.com/gh/leegical/Blog_img/zsh/haoomz.zsh-theme

编辑 ~/.zshrc 文件,将 ZSH_THEME 设为 haoomz。当然你也可以设置为其他主题,例如lukerandallrobbyrussell

  • Reopen terminal

安装autojump

$ sudo apt install autojump      [On Debian, Ubuntu and Mint]
$ sudo yum install autojump       [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
$ sudo emerge -a autojump        [On Gentoo Linux]
$ sudo apk add autojump          [On Alpine Linux]
$ sudo pacman -S autojump        [On Arch Linux]
$ sudo zypper install autojump   [On OpenSUSE]   

ZSH_THEME="agnoster"
plugins=(
        kubectl 
        dnf
        git  
        autojump
        zsh-autosuggestions
        zsh-syntax-highlighting
)

source ~/.zshrc

root账号下安装:

需要使用sudo命令

sudo cp -r /home/cyxinda/.oh-my-zsh    /root
sudo cp -r /home/cyxinda/.zshrc    /root

安装好zsh后,需要一些,能够使ctr+w的行为与bash一致:

# Create a new widget.
zle -N backward-kill-space-word
backward-kill-space-word() {
  # Inform the line editor that this widget will kill text.
  zle -f kill

  # Set $WORDCHARS for this command only.
  WORDCHARS='*?_-.[]~=/&;!#$%^(){}<>' zle .backward-kill-word
}

# See comments above.
zle -N backward-kill-bash-word
backward-kill-bash-word() {
  zle -f kill
  WORDCHARS='' zle .backward-kill-word
}

# Bind the widgets to keys.
bindkey '^W' backward-kill-space-word
bindkey '^[^H' backward-kill-bash-word

完整的.zshrc如下:

# 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:$HOME/.local/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="robbyrussell"
ZSH_THEME="powerlevel10k/powerlevel10k"
# 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.
#
#
#
# ========== 代理配置 ==========
# 在这里修改你的代理服务器信息
PROXY_HTTP="http://192.168.3.14:7890"
PROXY_HTTPS="http://192.168.3.14:7890"
# 如果需要用户名密码验证,使用下面格式:
# PROXY_HTTP="http://用户名:密码@192.168.3.14:7890"
# PROXY_HTTPS="http://用户名:密码@192.168.3.14:7890"

# 不需要代理的地址(局域网和本机)
NO_PROXY="localhost,127.0.0.1,::1,192.168.0.0/16,10.0.0.0/8"

# 开启代理的函数
proxy_on() {
    export http_proxy="$PROXY_HTTP"
    export https_proxy="$PROXY_HTTPS"
    export ftp_proxy="$PROXY_HTTP"
    export all_proxy="$PROXY_HTTP"
    export no_proxy="$NO_PROXY"
    
    # 颜色输出提示
    echo -e "\033[32m✓ 代理已开启\033[0m"
    echo -e "   HTTP: \033[33m$http_proxy\033[0m"
    echo -e "   HTTPS: \033[33m$https_proxy\033[0m"
}

# 关闭代理的函数
proxy_off() {
    unset http_proxy
    unset https_proxy
    unset ftp_proxy
    unset all_proxy
    unset no_proxy
    
    # 颜色输出提示
    echo -e "\033[31m✗ 代理已关闭\033[0m"
}

# 查看当前代理状态的函数
proxy_status() {
    echo "========== 代理状态 =========="
    
    if [ -n "$http_proxy" ]; then
        echo -e "HTTP  : \033[32m已启用\033[0m → \033[33m$http_proxy\033[0m"
    else
        echo -e "HTTP  : \033[31m未启用\033[0m"
    fi
    
    if [ -n "$https_proxy" ]; then
        echo -e "HTTPS : \033[32m已启用\033[0m → \033[33m$https_proxy\033[0m"
    else
        echo -e "HTTPS : \033[31m未启用\033[0m"
    fi
    
    echo "================================"
}

# 可选:测试代理是否工作
proxy_test() {
    echo "测试代理连接..."
    if curl -s -m 5 https://www.google.com > /dev/null; then
        echo -e "\033[32m✓ 代理工作正常\033[0m"
    else
        echo -e "\033[31m✗ 代理测试失败\033[0m"
    fi
}

# ========== 自动补全(可选)==========
# 为 proxy_on 添加补全
compdef _nothing proxy_on 2>/dev/null || true

# Create a new widget.
zle -N backward-kill-space-word
backward-kill-space-word() {
  # Inform the line editor that this widget will kill text.
  zle -f kill

  # Set $WORDCHARS for this command only.
  WORDCHARS='*?_-.[]~=/&;!#$%^(){}<>' zle .backward-kill-word
}

# See comments above.
zle -N backward-kill-bash-word
backward-kill-bash-word() {
  zle -f kill
  WORDCHARS='' zle .backward-kill-word
}

# Bind the widgets to keys.
bindkey '^W' backward-kill-space-word
bindkey '^[^H' backward-kill-bash-word
# 将双击 Tab 键绑定为接受自动建议
bindkey '\t\t' autosuggest-accept

plugins=(git zsh-autosuggestions zsh-syntax-highlighting z extract web-search autojump)
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

# 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"

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh