使用iTerm2和OhMyZsh實現一個強大終端

參考文章:
https://blog.csdn.net/qianghaohao/article/details/79440961
https://www.jianshu.com/p/9c3439cc3bdb
https://www.jianshu.com/p/d194d29e488c?open_source=weibo_search
https://www.jianshu.com/p/a78845c3f476


首先我們看一下最終效果
在這裏插入圖片描述

一、下載iTerm2

官網下載地址:https://www.iterm2.com/


二、安裝zsh

zsh一般Mac已經自帶了,無需額外安裝。可以用cat /etc/shells查看zsh是否安裝,如果列出了/bin/zsh則表明zsh已經安裝了。
接下來修改iTerm2終端的默認Shell,可以用echo $SHELL查看當前Shell是什麼,如果不是/bin/zsh則用如下命令修改iTerm2的默認Shell爲zsh

chsh -s /bin/bash

這個是默認的樣子
在這裏插入圖片描述


三、使用Oh my zsh

zsh的功能極其強大,只是配置過於複雜,起初只有極客纔在用。後來,有個窮極無聊的程序員可能是實在看不下去廣大猿友一直只能使用單調的bash, 於是他創建了一個名爲oh-my-zsh的開源項目

1.安裝Oh my zsh

# curl 安裝方式
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
# wget 安裝方式
sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"

在安裝過程中我們發現總是出現無法下載instal.sh文件的情況,所以下面是install.sh文件的源碼

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

  CHECK_ZSH_INSTALLED=$(grep /zsh$ /etc/shells | wc -l)
  if [ ! $CHECK_ZSH_INSTALLED -ge 1 ]; then
    printf "${YELLOW}Zsh is not installed!${NORMAL} Please install zsh first!\n"
    exit
  fi
  unset CHECK_ZSH_INSTALLED

  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"
  hash 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
}

main

之後執行

sh -c "$(curl -fsSL install.sh)"

2.修改主題

下面我們進行主題修改,主題簡介鏈接:https://github.com/robbyrussell/oh-my-zsh/wiki/themes

(1)打開配置文件
vi ~/.zshrc
(2)主題換成自己喜愛的主題
ZSH_THEME="agnoster"
(3)更新配置
source ~/.zshrc

四、安裝PowerFonts字體

有的同學會發現,執行完上一步後,使用可能會出現亂碼,這是因爲我們缺少PowerFonts字體
安裝字體庫需要首先將項目clone至本地,然後執行源碼中的install.sh

git clone [email protected]:powerline/fonts.git
cd fonts
./install.sh

安裝好字體庫之後,我們來設置iTerm2的字體,具體的操作是iTerm2 -> Preferences -> Profiles -> Text,在Font區域選中Change Font,然後找到Roboto Mono for Powerline字體。


五、安裝配色方案

配色鏈接:https://github.com/mbadolato/iTerm2-Color-Schemes

配色方案在使用VIM或Colorful Log時會變得非常有用,同時界面也不會一片黑綠一樣死板。

1.git clone的方式下載源碼進行安裝:

cd ~/Desktop/OpenSource
git clone https://github.com/altercation/solarized
cd solarized/iterm2-colors-solarized/
open .
  1. 在打開的finder窗口中,雙擊Solarized Dark.itermcolorsSolarized Light.itermcolors安裝明暗兩種配色
  2. 進入iTerm2 -> Preferences -> Profiles -> Colors -> Color Presets
  3. 根據個人喜好選擇solarized darksolarized light兩種配色中的一種即可。

2.手動安裝

但是下面這個是我最喜歡的配色

https://github.com/mbadolato/iTerm2-Color-Schemes/blob/master/schemes/Solarized%20Dark%20Higher%20Contrast.itermcolors

將該配色方案文件(Solarized Dark Higher Contrast.itermcolors)複製出來,保存到本地,文件命名爲 SolarizedDarkHigherContrast.itermcolors,然後雙擊即可安裝。


六、增加高亮

這是oh my zsh的一個插件,安裝方式與theme大同小異:

cd ~/.oh-my-zsh/custom/plugins/
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
vi ~/.zshrc

1.添加plugins

這時我們再次打開zshrc文件進行編輯。找到plugins,此時plugins中應該已經有了git,我們需要把高亮插件也加上:

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

請務必保證插件順序,zsh-syntax-highlighting必須在最後一個。

2.文件的最後一行添加

source ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

3.修改生效:

source ~/.zshrc

七、安裝命令補全

跟代碼高亮的安裝方式一樣,這也是一個zsh的插件,叫做zsh-autosuggestion,用於命令建議和補全。

cd ~/.oh-my-zsh/custom/plugins/
git clone https://github.com/zsh-users/zsh-autosuggestions
vi ~/.zshrc

之後將插件加入zsh配置與上一個一致


八、使用技巧

參考文章:https://www.jianshu.com/p/a78845c3f476

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章