Linux终端配置

Linux 终端的一些配置(macOS 也适用),令你的Terminal更好用,更美观。

  1. 首先安装 zsh

    sudo apt install zsh(以Ubuntu为例)或者用过源码安装

    如果没有root权限,通过源码安装,下载解压之后:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # 首先配置zsh,自定义安装路径
    ./configure --prefix=$HOME/.local
       
    # 然后编译
    make -j4
       
    # 检查编译是否成功(可选)
    make check
       
    # 如果没有编译错误,则安装zsh
    make install -j4
    

    源码安装完成后,将执行路径加入PATH中。在文件.bash_profile中最后加上export PATH=$HOME/.local/bin:$PATH

  2. 将zsh设置为默认shell

    如果使用root权限安装的zsh,直接终端运行chsh -s $(which zsh)即可。

    如果没有root权限,通过源码安装zsh的话,则解决方法是在每次打开终端时执行exec <zsh-path>来替代当前的shell。在文件.bash_profile中加入:

    1
    [ -f $HOME/.local/bin/zsh ] && exec $HOME/.local/bin/zsh -l
    

    如果上述两种方法都不能修改默认shell,直接跳到第 3 步,安装 oh-my-zsh,安装时会自动切换shell成zsh。

  3. 安装oh-my-zsh

    通过curl

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

    或者通过wget

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

    如果上述方法出现问题,可以按照下面的方法进行:

    1
    2
    3
    4
    cd
    git clone https://github.com/robbyrussell/oh-my-zsh.git
    mv oh-my-zsh .oh-my-zsh
    cp .oh-my-zsh/template/zshrc.zsh-template ~/.zshrc
    
  4. 配置oh-my-zsh

    vim .zshrc,切换ZSH_THEME,可以在这里预览。

    想要隐藏用户名,export DEFAULT_USER="<user-name>"

    插件配置(精髓)

    .zshrc中找到plugins=(git),其中加入以下插件:

    1
    2
    3
    4
    5
    6
    7
    plugins=(
      git
      extract  # 一个命令 `x` 解压全部压缩文件
      z  # cd的加强版,到达任意到过的位置,模糊匹配
      zsh-syntax-highlighting  # 指令高亮
      zsh-autosuggestions  # 命令自动提示,方向键补全
    )
    

    其中插件zsh-syntax-highlightingzsh-autosuggestions需要单独下载,方法如下:

    1
    2
    3
    4
    5
    # 下载zsh-syntax-highlighting
    git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
       
    # 下载zsh-autosuggestions
    git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
    
  5. 配置powerline-theme

    下载字体:

    1
    2
    3
    4
    5
    6
    7
    8
    # clone
    git clone https://github.com/powerline/fonts.git --depth=1
    # install
    cd fonts
    ./install.sh
    # clean-up a bit
    cd ..
    rm -rf fonts
    

    然后选择oh-my-zshpowerline相关主题即可。

  6. 下载powerline-status(可选)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # pip安装powerline-status
    pip install powerline-status
       
    # 获得powerline-status安装路径
    pip show powerline-status
       
    # 配置到.zshrc文件中,在末尾添加
    # 注意.后面有一个空格
    . /Powerline安装路径/powerline/bindings/zsh/powerline.zsh
    

    安装 powerline-gitstatus,配置 Git Branch Info 的显示。

    1
    pip install powerline-gitstatus
    

    然后直接修改 powerline 的配置文件或者创建 ~/.config/powerline 并复制一份配置,

    1
    cp -r /path/to/powerline/config_files/* ~/.config/powerline
    

    ~/.config/powerline/colorschemes/default.json 中添加 group

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    "groups": {
        "gitstatus":                 { "fg": "gray8",           "bg": "gray2", "attrs": [] },
        "gitstatus_branch":          { "fg": "gray8",           "bg": "gray2", "attrs": [] },
        "gitstatus_branch_clean":    { "fg": "green",           "bg": "gray2", "attrs": [] },
        "gitstatus_branch_dirty":    { "fg": "gray8",           "bg": "gray2", "attrs": [] },
        "gitstatus_branch_detached": { "fg": "mediumpurple",    "bg": "gray2", "attrs": [] },
        "gitstatus_tag":             { "fg": "darkcyan",        "bg": "gray2", "attrs": [] },
        "gitstatus_behind":          { "fg": "gray10",          "bg": "gray2", "attrs": [] },
        "gitstatus_ahead":           { "fg": "gray10",          "bg": "gray2", "attrs": [] },
        "gitstatus_staged":          { "fg": "green",           "bg": "gray2", "attrs": [] },
        "gitstatus_unmerged":        { "fg": "brightred",       "bg": "gray2", "attrs": [] },
        "gitstatus_changed":         { "fg": "mediumorange",    "bg": "gray2", "attrs": [] },
        "gitstatus_untracked":       { "fg": "brightestorange", "bg": "gray2", "attrs": [] },
        "gitstatus_stashed":         { "fg": "darkblue",        "bg": "gray2", "attrs": [] },
        "gitstatus:divider":         { "fg": "gray8",           "bg": "gray2", "attrs": [] }
    }
    

    再根据本人喜好在 ~/.config/powerline/colorschemes/default.jsonleftright 中增加:

    1
    2
    3
    4
    {
        "function": "powerline_gitstatus.gitstatus",
        "priority": 40
    }
    

    并删除 vcs.stashvcs.branch 的相关配置,因为它们和 powerline_gitstatus 冲突。其他关于 powerline_gitstatus 的配置可以参见此处

  7. oh-my-zsh主题推荐

    robbyrussellagnosterlambda等。

此时不赞何时赞!