1. 安裝 iTerm2
下載地址:https://www.iterm2.com/downloads.html
下載的是壓縮文件,解壓后是執行程序文件,你可以直接雙擊,或者直接將它拖到 Applications 目錄下。
或者你可以直接使用 Homebrew 進行安裝:
$ brew cask install iterm2
2. 配置 iTerm2 主題
iTerm2 最常用的主題是 Solarized Dark theme,下載地址:http://ethanschoonover.com/solarized
下載的是壓縮文件,你先解壓一下,然后打開 iTerm2,按Command + ,
鍵,打開 Preferences 配置界面,然后Profiles -> Colors -> Color Presets -> Import
,選擇剛才解壓的solarized->iterm2-colors-solarized->Solarized Dark.itermcolors
文件,導入成功,最后選擇 Solarized Dark 主題,就可以了。
3. 配置 Oh My Zsh
Oh My Zsh 是對主題的進一步擴展,地址:https://github.com/robbyrussell/oh-my-zsh
一鍵安裝:
$ sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
安裝好之后,需要把 Zsh 設置為當前用戶的默認 Shell(這樣新建標簽的時候才會使用 Zsh):
$ chsh -s /bin/zsh
然后,我們編輯vim ~/.zshrc
文件,將主題配置修改為ZSH_THEME="agnoster"
。
agnoster
是比較常用的 zsh 主題之一,你可以挑選你喜歡的主題,zsh 主題列表:https://github.com/robbyrussell/oh-my-zsh/wiki/themes
4. 配置 Meslo 字體
使用上面的主題,需要 Meslo 字體支持,要不然會出現亂碼的情況,字體下載地址:Meslo LG M Regular for Powerline.ttf
下載好之后,直接在 Mac OS 中安裝即可。
然后打開 iTerm2,按Command + ,
鍵,打開 Preferences 配置界面,然后Profiles -> Text -> Font -> Chanage Font
,選擇 Meslo LG M Regular for Powerline 字體。
當然,如果你覺得默認的12px
字體大小不合適,可以自己進行修改。
另外,VS Code 的終端字體,也需要進行配置,打開 VS Code,按Command + ,
鍵,打開用戶配置,搜索fontFamily
,然后將右邊的配置增加"terminal.integrated.fontFamily": "Meslo LG M for Powerline"
,
5. 聲明高亮
效果就是上面截圖的那樣,特殊命令和錯誤命令,會有高亮顯示。
使用 Homebrew 安裝:
$ brew install zsh-syntax-highlighting
安裝成功之后,編輯vim ~/.zshrc
文件,在最后一行增加下面配置:
source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
6. 自動建議填充
這個功能是非常實用的,可以方便我們快速的敲命令。
配置步驟,先克隆zsh-autosuggestions
項目,到指定目錄:
$ git clone https://github.com/zsh-users/zsh-autosuggestions ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions
然后編輯vim ~/.zshrc
文件,找到plugins
配置,增加zsh-autosuggestions
插件。
注:上面聲明高亮,如果配置不生效的話,在plugins
配置,再增加zsh-syntax-highlighting
插件試試。
有時候因為自動填充的顏色和背景顏色很相似,以至於自動填充沒有效果,我們可以手動更改下自動填充的顏色配置,我修改的顏色值為:586e75
7. 左右鍵跳轉
主要是按住option + → or ←
鍵,在命令的開始和結尾跳轉切換,原本是不生效的,需要手動開啟下。
打開 iTerm2,按Command + ,
鍵,打開 Preferences 配置界面,然后Profiles → Keys → Load Preset... → Natural Text Editing
,就可以了。
8. iTerm2 快速隱藏和顯示
這個功能也非常使用,就是通過快捷鍵,可以快速的隱藏和打開 iTerm2,示例配置(Commond + .
):
9.隱藏用戶信息
一般終端每一行前都會有xxx@xxxdeMacbook-Pro:我們可以將其隱藏掉。
進入oh-my-zsh的agnoster主題,編輯agnoster.zsh-theme文件
vim ~/.oh-my-zsh/themes/agnoster.zsh-theme # Context: user@hostname (who am I and where am I) prompt_context() { if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then prompt_segment black default "%(!.%{%F{yellow}%}.)%n@%m"-----將該行 注釋( prompt 前面 加 " # " ) 即可
10.修改所在目錄背景色
當前路徑這種藍色背景,需要進行修改
vim ~/.oh-my-zsh/themes/agnoster.zsh-theme
205 # Dir: current working directory 206 prompt_dir() { 207 prompt_segment blue $CURRENT_FG '%~' 208 } 修改藍色背景為: 205 # Dir: current working directory 206 prompt_dir() { 207 prompt_segment 075 $CURRENT_FG '%~' 208 } 保存,source使之生效
iTerm2 快捷命令
快捷命令說明:
命令 | 說明 |
---|---|
command + t | 新建標簽 |
command + w | 關閉標簽 |
command + 數字 command + 左右方向鍵 | 切換標簽 |
command + enter | 切換全屏 |
command + f | 查找 |
command + d | 垂直分屏 |
command + shift + d | 水平分屏 |
command + option + 方向鍵 command + [ 或 command + ] | 切換屏幕 |
command + ; | 查看歷史命令 |
command + shift + h | 查看剪貼板歷史 |
ctrl + u | 清除當前行 |
ctrl + l | 清屏 |
ctrl + a | 到行首 |
ctrl + e | 到行尾 |
ctrl + f/b | 前進后退 |
ctrl + p | 上一條命令 |
ctrl + r | 搜索命令歷史 |