zsh installation and configuration

1. zsh 安装配置

  1. yum 安装zsh

    yum install zsh # CentOS
    chsh -s /bin/zsh # CentOS 改默认shell为zsh
    
  2. 手动编译安装zsh
    下载zsh最新版,解压,root下./configure; make install -j 8安装。

  • By default, make install will install all the files in /usr/local/bin,/usr/local/lib etc.
  • 可以手动指定 --prefix安装目录;指定--exec-prefix zsh执行文件的安装目录。
  • 由于chsh -s可以指定某个路径的shell可执行文件,但是普通用户只能用/etc/shells文件中列出来,除非你是root用户,所以默认编译的话还得在/etc/shells添加/usr/local/bin/zsh路径。

    2. 安装oh-my-zsh

    有多种方式:
  1. 自动安装
    sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
    
    或者
    sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
    
    其实就是sh执行一个安装脚本instlal.sh,可以手动保存,为shell脚本,然后执行,内容如下
main() {
  # Use colors, but only if connected to a terminal, and that terminal
  # supports them.
  if which tput >/dev/null 2>&1; then
      ncolors=$(tput colors)
  fi
  if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
    RED="$(tput setaf 1)"
    GREEN="$(tput setaf 2)"
    YELLOW="$(tput setaf 3)"
    BLUE="$(tput setaf 4)"
    BOLD="$(tput bold)"
    NORMAL="$(tput sgr0)"
  else
    RED=""
    GREEN=""
    YELLOW=""
    BLUE=""
    BOLD=""
    NORMAL=""
  fi

  # Only enable exit-on-error after the non-critical colorization stuff,
  # which may fail on systems lacking tput or terminfo
  set -e

  if ! command -v zsh >/dev/null 2>&1; then
    printf "${YELLOW}Zsh is not installed!${NORMAL} Please install zsh first!\n"
    exit
  fi

  if [ ! -n "$ZSH" ]; then
    ZSH=~/.oh-my-zsh
  fi

  if [ -d "$ZSH" ]; then
    printf "${YELLOW}You already have Oh My Zsh installed.${NORMAL}\n"
    printf "You'll need to remove $ZSH if you want to re-install.\n"
    exit
  fi

  # Prevent the cloned repository from having insecure permissions. Failing to do
  # so causes compinit() calls to fail with "command not found: compdef" errors
  # for users with insecure umasks (e.g., "002", allowing group writability). Note
  # that this will be ignored under Cygwin by default, as Windows ACLs take
  # precedence over umasks except for filesystems mounted with option "noacl".
  umask g-w,o-w

  printf "${BLUE}Cloning Oh My Zsh...${NORMAL}\n"
  command -v git >/dev/null 2>&1 || {
    echo "Error: git is not installed"
    exit 1
  }
  # The Windows (MSYS) Git is not compatible with normal use on cygwin
  if [ "$OSTYPE" = cygwin ]; then
    if git --version | grep msysgit > /dev/null; then
      echo "Error: Windows/MSYS Git is not supported on Cygwin"
      echo "Error: Make sure the Cygwin git package is installed and is first on the path"
      exit 1
    fi
  fi
  env git clone --depth=1 https://github.com/robbyrussell/oh-my-zsh.git "$ZSH" || {
    printf "Error: git clone of oh-my-zsh repo failed\n"
    exit 1
  }


  printf "${BLUE}Looking for an existing zsh config...${NORMAL}\n"
  if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then
    printf "${YELLOW}Found ~/.zshrc.${NORMAL} ${GREEN}Backing up to ~/.zshrc.pre-oh-my-zsh${NORMAL}\n";
    mv ~/.zshrc ~/.zshrc.pre-oh-my-zsh;
  fi

  printf "${BLUE}Using the Oh My Zsh template file and adding it to ~/.zshrc${NORMAL}\n"
  cp "$ZSH"/templates/zshrc.zsh-template ~/.zshrc
  sed "/^export ZSH=/ c\\
  export ZSH=\"$ZSH\"
  " ~/.zshrc > ~/.zshrc-omztemp
  mv -f ~/.zshrc-omztemp ~/.zshrc

  # If this user's login shell is not already "zsh", attempt to switch.
  TEST_CURRENT_SHELL=$(expr "$SHELL" : '.*/\(.*\)')
  if [ "$TEST_CURRENT_SHELL" != "zsh" ]; then
    # If this platform provides a "chsh" command (not Cygwin), do it, man!
    if hash chsh >/dev/null 2>&1; then
      printf "${BLUE}Time to change your default shell to zsh!${NORMAL}\n"
      chsh -s $(grep /zsh$ /etc/shells | tail -1)
    # Else, suggest the user do so manually.
    else
      printf "I can't change your shell automatically because this system does not have chsh.\n"
      printf "${BLUE}Please manually change your default shell to zsh!${NORMAL}\n"
    fi
  fi

  printf "${GREEN}"
  echo '         __                                     __   '
  echo '  ____  / /_     ____ ___  __  __   ____  _____/ /_  '
  echo ' / __ \/ __ \   / __ `__ \/ / / /  /_  / / ___/ __ \ '
  echo '/ /_/ / / / /  / / / / / / /_/ /    / /_(__  ) / / / '
  echo '\____/_/ /_/  /_/ /_/ /_/\__, /    /___/____/_/ /_/  '
  echo '                        /____/                       ....is now installed!'
  echo ''
  echo ''
  echo 'Please look over the ~/.zshrc file to select plugins, themes, and options.'
  echo ''
  echo 'p.s. Follow us at https://twitter.com/ohmyzsh.'
  echo ''
  echo 'p.p.s. Get stickers and t-shirts at https://shop.planetargon.com.'
  echo ''
  printf "${NORMAL}"
  env zsh -l
}

main
  1. 手动安装
    git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh #这一步也可以手动下载传到主目录
    cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
    

    3. zsh 配置

  • Oh-My-Zsh自带的插件一般放置在目录~/.oh-my-zsh/plugins目录下,默认自带已经有很多[点这里查看列表],比如Z跳转插件,而通过互联网我们也可以下载更多的插件一般放置在~/.oh-my-zsh/custom/plugins目录下。
  • 插件的配置很简单,只需要找到配置文件~/.zshrc中的配置项plugins=()的()中添加上插件名称即可。例如添加插件git则是plugins=(git),多个插件使用空格隔开。

    3.1. zsh主题

    ls ~/.oh-my-zsh/themes  # theme文件都放在这个文件夹
    
    默认的主题是robbyrussell,修改.zshrc文件中的ZSH_THEME=ys可以修改主题形式,还可以改成ZSH_THEME=random每次登陆shell使用随机主题。

3.2. zsh扩展

3.2.1. autojump

  1. yum install autojump-zsh # CentOS安装autojump。需要在~/.zshrc中配置一下,除了在plugins中增加autojump之外,还需要添加一行
    [[ -s ~/.autojump/etc/profile.d/autojump.sh ]] && . ~/.autojump/etc/profile.d/autojump.sh
    
  2. 手动装
    git clone git://github.com/wting/autojump.git
    cd autojump
    ./install.py or ./uninstall.py
    
    如上添加环境变量

    3.2.2. zsh-autosuggestions

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

.zshrc中添加plugins=(其他的插件 zsh-autosuggestions)

3.2.3. zsh-syntax-highlighting

作用 平常用的ls、cd等命令输入正确会绿色高亮显示,输入错误会显示其他的颜色。

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

3.3. Cheatsheet

3.3.1. Commands

  • zsh_stats可以看到你的使用频率前20的命令是什么!
  • take 创建一个目录,并进入
  • x / extract 解压文件,可以根据类型自动推定

3.3.2. Alias 命令

Alias    Command
alias    list all aliases
..    cd ..
...    cd ../..
....    cd ../../..
.....    cd ../../../..
/    cd /
~    cd ~
cd +n    switch to directory number n
1    cd -
2    cd -2
md    mkdir -p
rd    rmdir
d    dirs -v (lists last used directories)

3.3.3. 其他

  • alias zconf='vim ~/.oh-my-zsh/custom/my_custom.zsh #快速打开配置文件
  • custom中的plugin貌似只能在.zshrc中指定,在my_custom.zsh配置文件中指定不起作用。