如何优雅地使用oh-my-zsh

前言

  • macOS自带的终端超级难用,这才有了iTerm2。iTerm2提供了很多高级设置,让我们无忧无虑地畅游在命令的海洋里。
  • iTerm2 + oh-my-zsh + Go2Shell 配合使用天下无敌,打造最完美的终端,提高你的工作效率。

iTerm2

  • https://www.iterm2.com/下载进行安装
  • 设置iTerm2为默认终端iTerm2 -> Make iTerm2 Default Term
  • 打开多个窗口command + t
  • 设置字体:iTerm2 ->Preferences ->Profiles ->Text->Font->Change Font
  • 更换光标样式为细体:Preferences->Profiles->Text->Cursor->Vertical bar
  • 默认情况下光标只能单个字符移动,这样效率太低了。我们要做的就是在单词间快速移动,提高效率
    • Preferences->Profile->Keys->shift+<-双击->选择Action为Send Escape Sequence->选择ESc+为b
    • Preferences->Profile->Keys->shift+->双击->选择Action为Send Escape Sequence->选择ESc+为f
    • 修改映射之后,再重新启动iTerm2就可以使用shift+<-shift+->来实现光标按单词快速移动了。
  • 在多个窗口情况下,如果不小心拖出一个窗口,这个时候来回切换窗口就会显得特别麻烦,我们需要合并窗口。
    • 依次点击preferences>appearance>tabs>
    • 找到show tab bar even when there is only one tab, 前面打上对勾
    • 关闭preferences,不用重启iTerm2就可以拖拽了

oh-my-zsh

基本介绍

  • 基于 zsh 命令行的一个扩展工具集,提供了丰富的扩展功能,给我们一种全新的方式使用命令行。
  • 官网:https://github.com/robbyrussell/oh-my-zsh
  • 插件:https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins
  • 查看系统支持的shell
    1
    2
    3
    4
    5
    6
    7
     ~ls -l /bin/*sh
    -r-xr-xr-x 1 root wheel 618416 2 5 2019 /bin/bash
    -rwxr-xr-x 1 root wheel 379952 2 5 2019 /bin/csh
    -r-xr-xr-x 1 root wheel 1282864 2 5 2019 /bin/ksh
    -r-xr-xr-x 1 root wheel 618480 2 5 2019 /bin/sh
    -rwxr-xr-x 1 root wheel 379952 2 5 2019 /bin/tcsh
    -rwxr-xr-x 1 root wheel 610224 2 5 2019 /bin/zsh
  • 查看默认使用的shell:echo $0,发现默认使用的是/bin/bash,下面要做的就是更改为/bin/zsh

下载安装

  • 安装oh-my-zsh前确认已经安装过iTerm2
  • 使用curl安装方式
    1
    sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
  • 如果安装失败,说明raw.githubusercontent.com不能访问,执行以下操作
    • 执行sudo vim /etc/hosts,填入以下内容
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      52.74.223.119     github.com
      192.30.253.119 gist.github.com
      54.169.195.247 api.github.com
      185.199.111.153 assets-cdn.github.com
      151.101.76.133 raw.githubusercontent.com
      151.101.76.133 gist.githubusercontent.com
      151.101.76.133 cloud.githubusercontent.com
      151.101.76.133 camo.githubusercontent.com
      151.101.76.133 avatars0.githubusercontent.com
      151.101.76.133 avatars1.githubusercontent.com
      151.101.76.133 avatars2.githubusercontent.com
      151.101.76.133 avatars3.githubusercontent.com
      151.101.76.133 avatars4.githubusercontent.com
      151.101.76.133 avatars5.githubusercontent.com
      151.101.76.133 avatars6.githubusercontent.com
      151.101.76.133 avatars7.githubusercontent.com
      151.101.76.133 avatars8.githubusercontent.com
    • 刷新缓存sudo killall -HUP mDNSResponder
    • 重新安装

卸载

  • 进入工具目录cd ~/.oh-my-zsh/tool
  • 设置可执行权限chmod +x uninstall.sh
  • 执行./uninstall.sh
  • 删除.zshrc文件

更改shell

  • 安装完成后,在/bin目录下会多出一个zsh的文件。
  • Mac系统默认使用bash作为终端,可以使用命令修改默认使用zsh:chsh -s /bin/zsh
  • 如果想修改回默认bash,同样使用chsh命令即可:chsh -s /bin/bash

设置主题

  • Oh My Zsh 自带了大量主题文件。你可以执行以下命令查看自带的主题:ls ~/.oh-my-zsh/themes
  • 对于自带主题的样式和呈现效果,你可以前往 Oh My Zsh 的 官方 Wiki 查看。
  • 本人使用ys主题,编辑:vim ~/.zshrc定位到 ZSH_THEME一行,修改为ZSH_THEME="ys"

zsh-syntax-highlighting

  • 语法高亮插件。直接进入自定义插件目录,下载插件源码。
    1
    2
    cd ~/.oh-my-zsh/custom/plugins/
    git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
  • 修改.zshrc配置文件
    • vim ~/.zshrc进入编辑模式,找到plugins=(,添加高亮插件。
      1
      2
      3
      4
      plugins=(
      git
      zsh-syntax-highlighting
      )
    • 在文件最后一行再加上
      1
      source ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
  • 配置生效
    1
    source ~/.zshrc

zsh-autosuggestion

  • 命令建议和补全插件。
  • 下载
    1
    2
    cd ~/.oh-my-zsh/custom/plugins/
    git clone https://github.com/zsh-users/zsh-autosuggestions
  • 编辑vim ~/.zshrc,找到plugins,加上这个插件即可:
    1
    2
    3
    4
    5
    plugins=(
    git
    zsh-autosuggestions
    zsh-syntax-highlighting
    )
  • 配置生效
    1
    source ~/.zshrc

.bash_profile失效

  • oh-my-zsh的配置文件为.zshrc,而默认shell(bash)的配置文件为.bash_profile。当设置zsh为默认shell是,~/.bash_profile里面定义的命令就会失效。
  • ~/.bash_profile的内容复制一份到~/.zshrc就可以解决问题,但是这样有点太low了,我们可以用下面的方法:
    • 打开Oh My Zsh的配置文件.zshrc(vi ~/.zshrc),在其中添加命令:source ~/.bash_profile
    • 终端执行source ~/.zshrc即可

Go2Shell

  • 配合iTerm2终端快速进行目录跳转和切换。
  • 通过http://zipzapmac.com/Go2Shell下载,不要在AppleStore下载了,上面版本很旧。
  • 执行open -a Go2Shell --args config,打开设置界面
    • Terminal application to use:选择iTerm2,使用iTerm2打开。
    • Open terminal in:选择New Tab,会新建一个选项
    • 点击Add Go2Shell button to Finder Toolabar,自动把图标放到Finder
  • 打开Finder,按住command,把Go2Shell图标从右边拖到左边
  • 每次会自动打开iTerm2,跳转到对应的目录,