• 亮色
  • 深色
  • 自动
  • RSS 订阅

    配置 Linux Shell

    2024-04-14

    Tip

    文中的 表示对已出现过的内容的省略,如果看到 不知道是什么的话,请参考上下文进行阅读。

    要求

    • Debian
    • sudo

    安装必要的依赖

    sudo apt install zsh git curl -y
    

    修改当前用户使用的 Shell 为 zsh

    sudo usermod -s /bin/zsh $USER
    

    安装 ohmyzsh

    sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
    

    安装 p10k

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

    国内可以使用 gitee:

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

    设置 ~/.zshrcZSH_THEME="powerlevel10k/powerlevel10k"

    重启终端。

    安装可选插件

    内置

    可以查看 ohmyzsh/plugins at master · ohmyzsh/ohmyzsh · GitHub ,将自己需要的插件添加到 ~/.zshrc 文件里的 plugins 中,多个插件以空格或换行分隔。

    常见的有 git、docker、docker-compsoe 等。

    第三方

    zsh-autosuggestions

    参考: zsh-autosuggestions/INSTALL.md at master · zsh-users/zsh-autosuggestions · GitHub

    可以在输入命令时在预览可以补全的内容:

    git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
    

    然后添加到 ~/.zshrcplugins 的最后一项(必须是最后一项):

    plugins=(
        # other plugins...
        zsh-autosuggestions
    )
    

    zsh-syntax-highlighting

    参考: zsh-syntax-highlighting/INSTALL.md at master · zsh-users/zsh-syntax-highlighting · GitHub

    可以在输入命令时给命令添加语法高亮:

    git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
    

    然后添加到 ~/.zshrcpluginszsh-autosuggestions 前面:

    plugins=(
        # other plugins...
        zsh-syntax-highlighting
        zsh-autosuggestions
    )
    

    自定义别名和函数

    下面是我自己常用的一些命令的别名和函数。

    除了最后一节,其他的需要你装有相关的应用才可以配置。

    pnpm

    alias pn=pnpm
    

    micromamba

    # >>> mamba extend >>>
    alias c=micromamba
    alias conda=micromamba
    local conda_venv_dir="./.venv"
    function activate_conda_env() {
      if [ -d "${conda_venv_dir}" ]; then
        micromamba activate $conda_venv_dir
      fi
    }
    function create_conda_env() {
      if [ ! -d "${conda_venv_dir}" ]; then
        micromamba create -p $conda_venv_dir -yv python="$1"
      fi
      activate_conda_env
    }
    alias cc=create_conda_env
    alias ca=activate_conda_env
    alias cq="micromamba deactivate"
    activate_conda_env
    # <<< mamba extend <<<
    

    uv

    # >>> mamba extend >>>
    ...
    alias ci="uv pip install"
    ...
    # <<< mamba extend <<<
    
    # >>> uv alias >>>
    alias up="uv pip"
    # <<< uv alias <<<
    

    just

    # >>> just alias >>>
    alias j=just
    alias jl="just --list --unsorted"
    alias ji="just --init"
    # <<< just alias <<<
    

    通用

    function dir() {
      if [ ! -d $1 ]; then
        mkdir $1
      fi
      cd $1
    }
    

    可以将当前用户可执行的二进制文件添加到环境变量里:

    # >>> local binary >>>
    export PATH=/home/nafnix/.local/bin:$PATH
    # <<< local binary <<<
    

    杂项

    要么是 Shell 要么是 ohmyzsh 的配置。

    Shell 语言环境:

    export LANG=zh_CN.UTF-8
    

    命令打错了自动修复(会提示你是否修复):

    ENABLE_CORRECTION="true"
    

    自动更新 ohmyzsh:

    zstyle ':omz:update' mode auto
    

    设置 Shell 编辑器为 neovim:

    export EDITOR='nvim'
    

    WSL 代理:

    proxy_host=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')
    proxy_port=7897
    export all_proxy=http://$proxy_host:$proxy_port
    export http_proxy=http://$proxy_host:$proxy_port
    export https_proxy=http://$proxy_host:$proxy_port
    export ALL_PROXY=http://$proxy_host:$proxy_port
    export HTTP_PROXY=http://$proxy_host:$proxy_port
    export HTTPS_PROXY=http://$proxy_host:$proxy_port