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:

  1. https://zsh.sourceforge.io/

  2. https://zsh.sourceforge.io/Guide/zshguide.html

  3. Install zsh:

    1
    sudo apt install zsh

    By default, zsh will use ~/.zshrc as config file.

  4. Switch your shell to zsh:

    1
    chsh -s /bin/zsh
  5. 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
2
3
4
5
6
7
8
9
10
 # Path to your oh-my-zsh installation. This includes your downloaded plugins.
export ZSH="$HOME/.oh-my-zsh"
export OH_MY_ZSH=$ZSH
# ...

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

# Execute the `oh-my-zsh` file
source $ZSH/oh-my-zsh.sh
# ...

$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代理所用的实际端口)

  1. 从gitee下载oh-my-zsh的安装脚本:

    1
    wget https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh
  2. 执行install.sh

  3. install.sh会clone github的Oh-my-zsh仓库. 如果访问不了github, 可以修改脚本, 把仓库地址换成gitee的:

    1. vim install.sh

    2. 找到以下部分:

      1
      2
      3
      4
      5
      # Default settings
      ZSH=${ZSH:-~/.oh-my-zsh}
      REPO=${REPO:-ohmyzsh/ohmyzsh}
      REMOTE=${REMOTE:-https://github.com/${REPO}.git}
      BRANCH=${BRANCH:-master}
    3. 将中间两行改为:

      1
      2
      REPO=${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", .

  • 所有插件在安装完毕后,都需要在~/.zshrcplugins中配置:

    1
    plugins=( [custom plugins...] zsh-syntax-highlighting autosuggestion)
  • 修改完配置文件后记得source ~/.zshrc, 然后重新启动shell

zsh-autosuggestions

How to install

这里直接用环境变量+重定向,不需要在指定目录下clone了

  1. 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
  2. Add the plugin to the list of plugins for Oh My Zsh to load (inside ~/.zshrc):

    1
    2
    3
    4
    plugins=( 
    # other plugins...
    zsh-autosuggestions
    )
  3. Start a new terminal session.

zsh-syntax-highlighting

How to install

    1. 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
    2. Activate the plugin in ~/.zshrc:

      1
      plugins=( [plugins...] zsh-syntax-highlighting)
    3. 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
    8
    alias 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 selection

    alias使用:

    1
    2
    3
    4
    5
    6
    7
    8
    a 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 use z cool to jump there.

  • autojump提供了j命令, 效果和fasdz命令一样, 可以作为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:

  1. Clone.

    1
    git clone https://github.com/dracula/zsh.git
  2. Move an rename it to some place. For me, I move it under my dotfiles folder($DOT_FILE_HOME) as zsh-dracula_theme

    1
    mv zsh $DOT_FILE_HOME/zsh-dracula_theme
  3. Create env vatiable for it in ~/.zshrc:

    1
    export DRACULA_THEME="$DOT_FILE_HOME/zsh-dracula_theme"
  4. 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
  5. Go to your ~/.zshrc file and set ZSH_THEME="dracula".

Customization

https://github.com/dracula/zsh

官网上给了一些自定义的配置方式, 我只选择增加username显示.

In ~/.zshrc:

1
export DRACULA_DISPLAY_CONTEXT=1

powlevel10k

Github Repo. 上面也给了中国大陆的下载方式.

  1. Clone the repository:

    1
    git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
  2. Set ZSH_THEME="powerlevel10k/powerlevel10k" in ~/.zshrc

  3. 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.