iTerm2安裝、配置和使用

一、iTerm2簡(jiǎn)介

iTerm 2 is a terminal emulator for Mac OS X that does amazing things.

1. 安裝

官網(wǎng)下載:http://iterm2.com/

Term2 是一款完全免費(fèi)的,專(zhuān)為 Mac OS 用戶(hù)打造的命令行應(yīng)用。直接在官網(wǎng)上下載。

下載的是壓縮文件,解壓后是執(zhí)行程序文件,你可以直接雙擊,或者直接將它拖到 Applications 目錄下。

或者你可以直接使用 Homebrew 進(jìn)行安裝:

brew cask install iterm2

2. 配置

  • 設(shè)置ITerm2為默認(rèn)終端


    設(shè)置ITerm2為默認(rèn)終端
  • 配置iTerm2主題
    iTerm2 最常用的主題是 Solarized Dark theme,下載地址:http://ethanschoonover.com/solarized
    下載的是壓縮文件,你先解壓一下,然后打開(kāi) iTerm2,按 Command + , 鍵,打開(kāi) Preferences 配置界面,然后 Profiles -> Colors -> Color Presets -> Import,選擇剛才解壓的 solarized->iterm2-colors-solarized->Solarized Dark.itermcolors 文件,導(dǎo)入成功,最后選擇 Solarized Dark 主題,就可以了。

新版本中該字體已經(jīng)存在,不需要導(dǎo)入可直接選擇。

配置 iTerm2 主題

二、安裝 oh-my-zsh

1. 查看系統(tǒng)有幾個(gè)shell

cat /etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

bash是mac中terminal自帶的shell,把它換成zsh,這個(gè)的功能要多得多。擁有語(yǔ)法高亮,命令行tab補(bǔ)全,自動(dòng)提示符,顯示Git倉(cāng)庫(kù)狀態(tài)等功能。

使用下面命令設(shè)置默認(rèn)shell,bash和zsh切換

# 切換到bash
chsh -s /bin/bash

# 切換到zsh
chsh -s /bin/zsh

記得輸入切換命令后,要重新打開(kāi)終端terminal才生效哦!

1.1 安裝最新版本 zsh

github安裝地址:https://github.com/robbyrussell/oh-my-zsh/wiki/Installing-ZSH

在 macOS 中,已經(jīng)自帶了 /bin/zsh,但是它未必是最新版本。

可以通過(guò)執(zhí)行 echo $SHELL 查看 當(dāng)前所使用的是哪個(gè)版本的shells,(所有shells 可在/etc/shells 中查看 )

  • macOS下安裝
brew install zsh zsh-completions

由于是用brew安裝,所以新版 zsh 文件是在 /usr/local/bin/zsh

  • /etc/shells配置中追加 /usr/local/bin/zsh,添加后內(nèi)容如下:
/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

# 追加 brew 安裝的最新版本zsh
/usr/local/bin/zsh

如果不在/etc/shells配置文件中添加 /usr/local/bin/zsh,那在執(zhí)行chsh時(shí)會(huì)報(bào)錯(cuò),提示 non-standard shell。

  • 切換 新 zsh
chsh -s /usr/local/bin/zsh

2. 安裝oh-my-zsh

官網(wǎng)地址:https://ohmyz.sh/
github地址:https://github.com/robbyrussell/oh-my-zsh

  • 使用 curl 安裝:
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
  • 使用 wget 安裝:
sh -c "$(wget -O- https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
  • zsh 設(shè)置為當(dāng)前用戶(hù)的默認(rèn) Shell
chsh -s /bin/zsh

3. 配置oh-my-zsh主題

github主題使用地址:https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
zsh主題列表:https://github.com/robbyrussell/oh-my-zsh/tree/master/themes/

安裝成功后,用戶(hù)$HOME目錄下會(huì)出現(xiàn) .zshrc 文件,用 vim ~/.zshrc 打開(kāi)隱藏文件,將主題配置修改為 agnoster,修改如下

ZSH_THEME="agnoster"

PS:agnoster 是比較常用的 zsh 主題之一,你可以挑選你喜歡的主題 zsh主題列表

4. Powerline 字體下載安裝

Powerline下載:https://github.com/powerline/fonts

iTerm2 改為主題之后,會(huì)出現(xiàn)亂碼的情況。需要安裝 Powerline 字體來(lái)解決。

# clone 先克隆
git clone https://github.com/powerline/fonts.git
# install 后安裝
cd fonts
./install.sh

可以按照以上命令進(jìn)行 powerline 全部字體安裝,也可以選擇部份字體安裝;而我選擇 Meslo 一個(gè)字體安裝

  1. 下載 Meslo 字體下載
  2. 雙擊字體,安裝到 Mac OS 系統(tǒng)字體冊(cè)

安裝完字體庫(kù)之后,打開(kāi) iTerm2,按 Command + , 鍵,打開(kāi) Preferences 配置界面,然后 Profiles -> Text -> Font -> Chanage Font ,選擇 Meslo LG M Regular for Powerline 字體。

配置 Meslo 字體

當(dāng)然,如果你覺(jué)得默認(rèn)的 12px 字體大小不合適,可以自己進(jìn)行修改。

5. 設(shè)置配色方案

通過(guò) oh-my-zsh 設(shè)置完主題后,使用iTerm2自帶的幾個(gè)背景色顯示看得不舒服,下載其他的顏色方案較美觀(guān)。我選用的是 solarized,效果還不錯(cuò)。點(diǎn)開(kāi)官網(wǎng),下載,解壓,然后打開(kāi) iTerm2 下的偏好設(shè)置 preference ,點(diǎn)開(kāi) profiles 下的colors 選項(xiàng),點(diǎn)擊右下角的 Color Presets 選項(xiàng),選擇import ,導(dǎo)入解壓到的 solarized 文件下的Solarized Dark.itermcolors。后面對(duì)vim進(jìn)行配色的時(shí)候也會(huì)用到這個(gè)solarized

到這步我們的終端看上去已經(jīng)非常好看了,這時(shí)我們來(lái)安裝其它插件,讓終端看起來(lái)更加風(fēng)騷。

6. 語(yǔ)法高亮

6.1 Oh-my-zsh下高亮安裝方法【推薦】

github上說(shuō)明地址:https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/INSTALL.md

  • Clone this repository in oh-my-zsh's plugins directory:
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
  • Activate the plugin in ~/.zshrc:
plugins=( [plugins...] zsh-syntax-highlighting)
  • Restart zsh (such as by opening a new instance of your terminal emulator).

6.2 使用 Homebrew 安裝zsh-syntax-highlighting 插件:

brew install zsh-syntax-highlighting
  • 安裝成功之后,編輯 vim ~/.zshrc 文件,在最后一行增加下面配置:
source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

# 其它地方配置參考
# source  /usr/local/Cellar/zsh-syntax-highlighting/0.6.0/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
  • 執(zhí)行 source ~/.zshrc 使生效

7. 自動(dòng)提示命令

當(dāng)我們輸入命令時(shí),終端會(huì)自動(dòng)提示你接下來(lái)可能要輸入的命令,這時(shí)按 便可輸出這些命令,非常方便。

  • 先克隆zsh-autosuggestions項(xiàng)目,到本地 ~/.oh-my-zsh/custom/plugins 路徑下:
git clone git://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-autosuggestions.git $ZSH_CUSTOM/plugins/zsh-autosuggestions
  • 然后編輯vim ~/.zshrc文件,找到plugins配置,增加zsh-autosuggestions插件。默認(rèn)是 plugins=(git)
plugins=(zsh-autosuggestions git)

有時(shí)候因?yàn)樽詣?dòng)提示命令的顏色和背景顏色很相似,以至于自動(dòng)填充沒(méi)有效果,我們可以手動(dòng)更改下自動(dòng)填充的顏色配置,我修改的顏色值為:#586e75,示例:

注:上面聲明語(yǔ)法高亮,如果配置不生效的話(huà),在plugins配置,再增加zsh-syntax-highlighting插件試試。

手動(dòng)更改背景顏色

8. iTerm2 隱藏用戶(hù)名和主機(jī)名

有時(shí)候我們的用戶(hù)名和主機(jī)名太長(zhǎng),比如我的xishuai@xishuaideMacBook-Pro,終端顯示的時(shí)候會(huì)很不好看,我們可以手動(dòng)去除。

編輯 vim ~/.zshrc 文件,增加 DEFAULT_USER="xishuai" 配置,可在文本最好追加

我們可以通過(guò)whoami 命令,查看當(dāng)前用戶(hù)

9. iTerm2 快速隱藏和顯示

這個(gè)功能也非常使用,就是通過(guò)快捷鍵,可以快速的隱藏和打開(kāi) iTerm2,
示例配置(Commond + .):

hotkey配置 快速隱藏和顯示

iTerm2 快捷命令

快捷命令說(shuō)明:

命令 說(shuō)明
command + t 新建標(biāo)簽
command + w 關(guān)閉標(biāo)簽
command + 數(shù)字 command + 左右方向鍵 切換標(biāo)簽
command + enter 切換全屏
command + f 查找
command + d 垂直分屏
command + shift + d 水平分屏
command + option + 方向鍵 command + [ 或 command + ] 切換屏幕
command + ; 查看歷史命令
command + shift + h 查看剪貼板歷史
ctrl + u 清除當(dāng)前行
ctrl + l 清屏
ctrl + a 到行首
ctrl + e 到行尾
ctrl + f/b 前進(jìn)后退
ctrl + p 上一條命令
ctrl + r 搜索命令歷史

oh-my-zsh插件

github插件使用地址:https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins

通過(guò)編輯 ~/.zshrc文件來(lái)啟用所需的插件

plugins=(rails git ruby)

參考資料:
https://www.cnblogs.com/xishuai/p/mac-iterm2.html
http://m.itdecent.cn/p/405956cdaca6

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容