一、概述
因個人工作需要,使用終端工具進行運維和開發工作,但是Mac OS 自帶的終端工具使用堡壘機登錄配置不了,而且使用CRT等終端工具每次登錄堡壘機都需要配置密碼,操作起來很麻煩。一直想找一款終端工具便於日常工作使用,偶遇了iTerm2,發現很強大的一款終端工具,網上配置很多,但是有些地方會有些不清楚的地方,我這邊參照網上文檔,邊部署邊記錄,整理了下屬文檔,便於大家參考和自身查閱。
iTerm2是Terminal的替代品,是iTerm的后繼產品。它適用於MacOS 10.12或更高版本的Mac。iTerm2將終端帶入了您從未想過一直想要的功能,使其進入了現代時代。
二、 具體介紹
1. 安裝 iTerm2
下載地址:https://www.iterm2.com/downloads.html
下載的是壓縮文件,解壓后是執行程序文件,你可以直接雙擊,或者直接將它拖到 Applications 目錄下。
或者你可以直接使用 Homebrew 進行安裝:
$ brew cask install iterm2
注意:有些人MAC 上沒有brew安裝命令,請執行如下命令進行安裝:
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
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)" Cloning Oh My Zsh... xcode-select: note: no developer tools were found at '/Applications/Xcode.app', requesting install. Choose an option in the dialog to download the command line developer tools. Error: git clone of oh-my-zsh repo failed Eason:Library linan$ sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" Cloning Oh My Zsh... Cloning into '/Users/linan/.oh-my-zsh'... remote: Enumerating objects: 1089, done. remote: Counting objects: 100% (1089/1089), done. remote: Compressing objects: 100% (1030/1030), done. remote: Total 1089 (delta 23), reused 874 (delta 19), pack-reused 0 Receiving objects: 100% (1089/1089), 716.00 KiB | 65.00 KiB/s, done. Resolving deltas: 100% (23/23), done. Looking for an existing zsh config... Using the Oh My Zsh template file and adding it to ~/.zshrc. Time to change your default shell to zsh: Do you want to change your default shell to zsh? [Y/n] y Changing the shell... Changing shell for linan. Password for linan: Shell successfully changed to '/bin/zsh'. __ __ ____ / /_ ____ ___ __ __ ____ _____/ /_ / __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \ / /_/ / / / / / / / / / / /_/ / / /_(__ ) / / / \____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/ /____/ ....is now installed! Please look over the ~/.zshrc file to select plugins, themes, and options. p.s. Follow us on https://twitter.com/ohmyzsh p.p.s. Get stickers, shirts, and coffee mugs at https://shop.planetargon.com/collections/oh-my-zsh ➜ Library
安裝好之后,需要把 Zsh 設置為當前用戶的默認 Shell(這樣新建標簽的時候才會使用 Zsh):
$ chsh -s /bin/zsh
然后,我們編輯vim ~/.zshrc文件,將主題配置修改為ZSH_THEME="agnoster"。
$ vim ~/.zshrc # If you come from bash you might have to change your $PATH. # export PATH=$HOME/bin:/usr/local/bin:$PATH # Path to your oh-my-zsh installation. export ZSH="/Users/linan/.oh-my-zsh" # Set name of the theme to load --- if set to "random", it will # load a random theme each time oh-my-zsh is loaded, in which case, # to know which specific one was loaded, run: echo $RANDOM_THEME # See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes 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
具體如下:
................... # Compilation flags # export ARCHFLAGS="-arch x86_64" # Set personal aliases, overriding those provided by oh-my-zsh libs, # plugins, and themes. Aliases can be placed here, though oh-my-zsh # users are encouraged to define aliases within the ZSH_CUSTOM folder. # For a full list of active aliases, run `alias`. # # Example aliases # alias zshconfig="mate ~/.zshrc" # alias ohmyzsh="mate ~/.oh-my-zsh" source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
保存后,執行變量生效命令
source /etc/profile
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
插件試試。
有時候因為自動填充的顏色和背景顏色很相似,以至於自動填充沒有效果,我們可以手動更改下自動填充的顏色配置,我修改的顏色值為:3a717f,示例:
7. 左右鍵跳轉
主要是按住option + → or ←
鍵,在命令的開始和結尾跳轉切換,原本是不生效的,需要手動開啟下。
打開 iTerm2,按Command + ,
鍵,打開 Preferences 配置界面,然后Profiles → Keys → Load Preset... → Natural Text Editing
,就可以了。
8. iTerm2 快速隱藏和顯示
這個功能也非常使用,就是通過快捷鍵,可以快速的隱藏和打開 iTerm2,示例配置(Commond + .
):
9. iTerm2 隱藏用戶名和主機名
有時候我們的用戶名和主機名太長,比如我的Eason@EasondeMacBook-Pro
,終端顯示的時候會很不好看(上面圖片中可以看到),我們可以手動去除。
編輯vim ~/.zshrc
文件,增加DEFAULT_USER="eason"
配置,示例:
10. iTerm2 配置代理
編輯~ vim ~/.zshrc
,增加下面配置(使用的 shadowsocks):
# proxy list alias proxy='export all_proxy=socks5://127.0.0.1:1086' alias unproxy='unset all_proxy'
iTerm2 需要新建標簽頁,才有效果:
$ proxy $ curl ip.cn 當前 IP:185.225.14.5 來自:美國 $ unproxy $ curl ip.cn 當前 IP:115.236.186.130 來自:浙江省杭州市 電信
我們可以測試下:
$ curl https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64 <html> <head> <title>Directory listing for /yum/repos/kubernetes-el7-x86_64/</title> </head> <body> <h2>Index of /yum/repos/kubernetes-el7-x86_64/</h2> <p></p> <a href="/yum/repos/kubernetes-el7-x86_64/repodata">repodata</a><br /> </body> </html>
11. iTerm2 快捷命令