1. 程式人生 > >iTerm2 都不會用,還敢自稱老司機?(上)

iTerm2 都不會用,還敢自稱老司機?(上)

對於需要長期與終端打交道的工程師來說,擁有一款稱手的終端管理器是很有必要的,對於 Windows 使用者來說,最好的選擇是 `Xshell`,這個大家都沒有異議。但對於 MacOS 使用者來說,仍然毋庸置疑,`iTerm2` 就是你要的利器,如果你覺得不是,那是你的問題,不是 `iTerm2` 的問題。今天我就來告訴你問題出在哪裡,我將要向你展示的基本上都是你沒見過的配方,擦亮眼睛吧! 本教程總共分為三個部分:第一部分關於 iTerm 自身的設定和優化;第二部分關於連線遠端伺服器的設定和優化;第三部分關於 zsh 的設定和優化。 今天來講第一部分。 ## 1. 懸浮視窗 首先我們來解決第一個問題:**如何在任何介面呼入撥出 iTerm2 的視窗,並且懸浮在介面的頂部?** 相信每個人都會有這樣的使用場景:你正在全屏瀏覽器瀏覽網頁,或者正在全屏編輯器寫程式碼寫文章之類的,突然想到了什麼,或發現了什麼,想快速開啟終端,執行一兩條命令(諸如開啟檔案、啟動服務等),然後關閉。 對於這種情況,我們的願景是可以通過快捷鍵迅速開啟終端,然後用同樣的快捷鍵迅速隱藏它,直到我需要時再次來到我面前。至於實現方式,雖繁瑣但並不複雜,下面跟著我的步驟走: ### 建立新的 Profile 首先開啟 `Preperence → Profiles`,新建一個 `Profiles`,取名 `HotKey Window`。 ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113745327-1914575484.png) ### 背景透明與模糊設定 在`Preperence → Profiles → Window → Window Appearance` 進行設定 ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113746107-1017053602.png) ### 視窗風格配置 在`Preperence → Profiles → Window → Settings for New Windows` 進行設定 ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113746657-1042119096.png) 解釋一下這幾個引數: + **Full-Width Top of Screen** : 這個很好理解,讓終端顯示在螢幕頂部,並佔滿整個寬度。 + **Current Spce** : 表示只顯示在當前的工作空間,舉個例子吧,假設你在當前螢幕打開了終端,你切換到下一個螢幕時它就不會跟到下一個螢幕。 + **Screen width Cursor** : 這個和上面的引數搭配,用來判定哪個螢幕屬於當前的工作空間,表示你的滑鼠在哪,哪裡就是當前的工作空間。 ### 設定 HotKey 在 `Preperence → Profiles → Keys → HotKey Window` 進行設定 選中 `A hotkey opens a dedicated window with this profile`,表示此 profile 可以通過快捷鍵快速開啟快速關閉。 ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113747106-934002829.png) 然後點選 `Configure Hotkey Window`,設定快捷鍵。 ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113747927-922571961.png) 為了只使用一個鍵,可以使用功能鍵作為快捷鍵,我選擇的是 `F12`。 這裡需要說明一點,功能鍵只適用於沒有 `Touch Bar` 的 MacBook,對於新款的帶 `Touch Bar` 的 MacBook,只能使用其他的組合快捷鍵,比如 `Cmd+P` 什麼的。至於原因嘛,那是因為新款 MacBook 的功能鍵只能顯示在 `Touch Bar` 上,你只能分別針對應用開啟功能鍵,也就是說,你只有開啟這個應用時,才會在 Touch Bar 上顯示功能鍵,可是我們要的是在任何地方通過快捷鍵撥出終端,我不能先開啟終端,再使用功能鍵開啟終端。。。這是個先有雞還是先有蛋的問題,所以還是選其他快捷鍵吧。 對於沒有 `Touch Bar` 的 MacBook,我們可以這麼設定,首先開啟系統偏好設定,選擇鍵盤設定。 ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113748627-447368272.png) 選中 **將 F1、F2 等鍵用作標準功能鍵**。 ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113749043-1244199391.png) 這樣你就可以通過 `F12` 來快速開啟關閉終端了。 > 請注意:打開了標準功能鍵之後,以後再想使用功能鍵的特殊功能,比如 `F11` 減小音量、`F12` 增大音量,必須得和 `Fn` 鍵組合使用,例如增大音量就是 `Fn+F12`。 對於新款帶 `Touch Bar` 的 MacBook,可以這麼設定,首先開啟系統偏好設定,選擇鍵盤設定。 ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113749614-217925186.png) "按下 Fn 鍵以" 選擇 “顯示F1、F2等鍵”: ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113750376-788004039.png) 這樣就可以使用 `Fn+F12` 來快速開啟終端。 同時在 `快捷鍵 → 功能鍵` 設定中新增 iTerm 應用,這樣開啟 iTerm2 視窗時就可以直接使用 `F12` 鍵來關閉視窗。 ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113750835-2095951582.png) 到目前為止,懸浮終端的 `Profile` 就配置完成了,你可以按下你設定的`HotKey`來方便快速開啟和隱藏命令列。如果你想新建標籤頁也用這個 Profile,可以將 `Hotkey Window` 設為預設的 Profile: ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113751479-1109883969.png) 下面我們來做一些優化工作。 ## 2. 改變游標形狀 在 `Preperence → Profiles → Text` 進行設定。 ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113752081-1400876995.png) 預設游標形狀是醬紫的: ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113752388-1041418.png) 更改設定之後游標形狀就順眼多了: ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113752652-1289595244.png) ## 3. 為自己代言 如果你要為自己代言,新建任何一個終端視窗都想打上自己的 `Logo`,可以在 `Preperence → Profiles → General` 進行設定。 ![](/Users/cnsgyg/Desktop/螢幕快照 2020-03-10 下午2.56.46.png) 這樣不管我切換到哪個終端,都可以 fuck cloud native! ## 4. 自定義標籤頁標題 在 `Preperence → Profiles → General` 進行設定。 取消勾選 `Applications in terminal may change the title`: ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113753427-1040657104.png) Title 選擇 `Profile Name`,取消勾選 `Job Name`: ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113754893-1567778415.png) 這樣每個標籤頁的標題都會顯示為對應的 `Profile Name`: ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113755296-1822063713.png) ## 5. 自定義配色方案 網上有現成的配色方案,我們可以直接拿來主義,地址在這裡:[https://iterm2colorschemes.com/](https://iterm2colorschemes.com/)。 下載壓縮包,解壓後,開啟 iTerm2 的設定:`Preperence → Profiles → Colors → Color Presets`。點選 `import` 選擇解壓好的主題目錄下 `schemes` 目錄下的你想要的主題匯入。 ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113755858-526644835.png) 匯入之後,再選擇你想要的主題就好了,我當然選的是 Ubuntu 騷紫~~ ## 6. 統一配色 預設情況下標籤的顏色是黑的,即使你改了配色也沒用: ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113756272-319067000.png) 如果想統一配色,需要稍微調整一下配置,開啟 `Appearence → General`,將 Theme 改為 `Minimal`: ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113756516-1198851660.png) ## 7. 關閉啟動介面 如果你不想每次開啟 iTerm2 都開啟預設的視窗,也就是靜默開啟程式,可以在 `Appearence → General` 設定: ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113756776-365162756.png) 勾選 `Exclude from Dock and ...`,更改設定之後下次你再重新開啟 iTerm2 就會變成這個樣子: ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113756984-759048083.png) 它再也不會出現在 Dock 中,也不會在啟動時給你開啟一個預設的視窗,你可以優雅地通過快捷鍵呼入撥出視窗,完美。 ## 8. 使用 shell integration iTerm2 可以與 unix shell 整合在一起,在安裝了 iTerm2 的 shell 整合工具後,可以在 iTerm2 中看到命令歷史、當前工作目錄、主機名、上傳下載檔案等。 可以點選選單欄 `iTerm2 > Install Shell Integration` 或者終端輸入指令: ```bash # 如果你的預設 shell 是 bash,請將 zsh 換成 bash $ curl -L https://iterm2.com/misc/install_shell_integration.sh | zsh ``` 該指令碼會自動安裝當前終端 shell 的對應指令碼,並寫入到對應的 shell 配置檔案中。例如在 zsh shell 中執行完指令碼後,`.zshrc` 中間中寫入了下面語句: ```bash $ test -e "${HOME}/.iterm2_shell_integration.zsh" && source "${HOME}/.iterm2_shell_integration.zsh" ``` 在安裝完 iTerm2 的 `shell integration` 後會在終端介面中最左側多出一個**藍色三角形**的標記。如圖: ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113757139-2139208178.png) 有藍色三角形的標記說明當前 shell 支援 shell integration。如需關閉標記,可以在 `iTerm2 > Preferences > Profiles > (your profile) > Terminal 最下面 > Shell Integration` 關閉 `Show mark indicators` 。 ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113757485-295416966.png) 所有工具: ```bash imgcat filename Displays the image inline. imgls Shows a directory listing with image thumbnails. it2api Command-line utility to manipulate iTerm2. it2attention start|stop|fireworks Gets your attention. it2check Checks if the terminal is iTerm2. it2copy [filename] Copies to the pasteboard. it2dl filename Downloads the specified file, saving it in your Downloads folder. it2setcolor ... Changes individual color settings or loads a color preset. it2setkeylabel ... Changes Touch Bar function key labels. it2ul Uploads a file. it2universion Sets the current unicode version. ``` 例如,可以用 `imgcat` 直接在終端顯示圖片: ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113757835-1983572894.png) ## 9. 奇技淫巧 最後介紹一些 iTerm2 的奇技淫巧。 ### 剪貼簿歷史記錄 iTerm2 允許我們快速檢視剪貼簿內容 只需使用 `Command + Shift + h` 可以撥出貼上歷史,支援模糊檢索。還可以設定將貼上歷史儲存在磁碟上(Preferences -> General) ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113758265-938497621.png) ### 智慧選中 在 iTerm2 中,雙擊選中,三擊選中整行,四擊智慧選中(智慧規則可[配置](http://www.iterm2.com/documentation-smart-selection.html)),可以識別網址,引號引起的字串,郵箱地址等。(很多時候雙擊的選中就已經很智慧了) ### 巧用 Command 鍵 按住 ⌘ 鍵: - 可以拖拽選中的字串; - 點選 url:呼叫預設瀏覽器訪問該網址; - 點選檔案:呼叫預設程式開啟檔案; - 點選資料夾:在 finder 中開啟該資料夾; - 同時按住 `option` 鍵,可以以矩形選中,類似於 vim 中的 `ctrl v` 操作。 ### 將文字內容複製到剪下板 ```bash $ pbcopy < text.md ``` ### 在 Finder 中開啟當前目錄 ```bash $ open . ``` ok,關於 iTerm 自身的設定和優化到這裡就結束了,下一篇將會介紹關於連線遠端伺服器的設定和優化,敬請期待。 ## 微信公眾號 掃一掃下面的二維碼關注微信公眾號,在公眾號中回覆◉加群◉即可加入我們的雲原生交流群,和孫巨集亮、張館長、陽明等大佬一起探討雲原生技術 ![](https://img2020.cnblogs.com/other/1737323/202003/1737323-20200311113759190-308877