Mac OS自帶的終端,用起來雖然有些不太方便,界面也不夠友好,關鍵是在windows上用習慣了自動補全功能,在Mac上一個個的拼寫單詞是真的難受,逼著我記英文單詞。
經過一天的磨合,我實在忍不了,在網上查到了iTerm2這個款終端工具,這是一款相對比較好用的終端工具。
iTerm2常用操作包括主題選擇、聲明高亮、自動填充建議、隱藏用戶名和主機名、分屏效果等。這里主要介紹我使用的自動補全建議、聲明高亮,另外幾個功能,如果有需求,可以在 參考資料 中找到。
iTerm2 下載與安裝
iTerm2下載地址:https://www.iterm2.com/downloads.html
注意:下載的是壓縮文件,解壓后直接雙擊執行程序文件,或者直接將它拖到 Applications 目錄下。
也可以直接使用Homebrew進行安裝:
brew cask install iterm2
iTerm2主題配置,請參考 Mac下iTerm2的安裝與配置
Homebrew 安裝方法請參考 macbook 安裝 Git 和 安裝 Homebrew
查看 Mac 系統是否使用 zsh
Mac系統默認使用bash作為終端,但是新的Mac系統已經默認使用zsh,查看方式:
cat /etc/shells
/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
bash與zsh切換命令:
chsh -s /bin/zsh
chsh -s /bin/bash
bash與zsh配置文件:
- bash的配置文件:
~/.bash_profile
- zsh的配置文件:
~/.zshrc
安裝配置 zsh
Zsh 和 bash 一樣,是一種 Unix shell,但大多數 Linux 發行版都默認使用 bash shell。但 Zsh有強大的自動補全參數和自定義配置功能等等,Github地址:https://github.com/robbyrussell/oh-my-zsh
- 安裝命令:
sh -c “$(curl -fsSL https: //raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)”
或
- 使用 Homebrew 進行安裝
Mac 系統自帶了zsh, 一般不是最新版,如果需要最新版可通過Homebrew來安裝。
brew install zsh
可通過查看zsh的版本。
zsh --version
安裝完成以后,將zsh設置為默認的Shell
chsh -s /bin/zsh
安裝 oh-my-zsh
oh-my-zsh 幫我們整理了一些常用的 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 into '/root/.oh-my-zsh'...
remote: Counting objects: 712, done.
remote: Compressing objects: 100% (584/584), done.
remote: Total 712 (delta 15), reused 522 (delta 4), pack-reused 0
Receiving objects: 100% (712/712), 443.58 KiB | 27.00 KiB/s, done.
Resolving deltas: 100% (15/15), done.
Checking connectivity... done.
Looking for an existing zsh config...
Using the Oh My Zsh template file and adding it to ~/.zshrc
Copying your current PATH and adding it to the end of ~/.zshrc for you.
Time to change your default shell to zsh!__ ______ / /_ ____ ___ __ __ ____ _____/ /_
/ __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \
/ /_/ / / / / / / / / / / /_/ / / /_(__ ) / / /
\____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_//____/ ....is now installed!Please look over the ~/.zshrc file to select plugins, themes, and options.
p.s. Follow us at https://twitter.com/ohmyzsh.
p.p.s. Get stickers and t-shirts at http://shop.planetargon.com.
安裝完成以后,默認Shell的 ~/.bashrc 文件默認不再加載了,替代的是 ~/.zlogin 和 ~/.zshrc 。所以如果你在 ~/.bashrc 里配置了某些設置,需要把她們復制到 ~/.zshrc 中。
oh my zsh 其他安裝方式參考 Mac下iTerm2的安裝與配置
oh my zsh 目錄結構
進入~/.oh-my-zsh目錄后,看看該目錄的結構
ls ~/.oh-my-zsh
CODE_OF_CONDUCT.md SECURITY.md log themes
CONTRIBUTING.md cache oh-my-zsh.sh tools
LICENSE.txt custom plugins
README.md lib templates
lib 提供了核心功能的腳本庫
tools 提供安裝、升級等功能的快捷工具
plugins 自帶插件的存在放位置
templates 自帶模板的存在放位置
themes 自帶主題文件的存在放位置
custom 個性化配置目錄,自安裝的插件和主題可放這里
安裝自動補全插件
在終端輸入命令:
cd ~/.oh-my-zsh/custom/plugins
git clone https://github.com/zsh-users/zsh-autosuggestions.git
安裝命令高亮插件
在終端輸入命令:
cd ~/.oh-my-zsh/custom/plugins
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
配置 .zshrc
文件
在終端輸入命令:
vim ~/.zshrc
打開.zshrc 文件,進入vim 編輯器模式 (按i
進入編輯模式)
在 ~/.zshrc
中添加以下內容(我添加在文件末尾):
source ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
source ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
在 ~/.zshrc
中修改 plugins=(git)
為:
plugins=(git zsh-autosuggestions zsh-syntax-highlighting)
# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git zsh-autosuggestions zsh-syntax-highlighting)source $ZSH/oh-my-zsh.sh
保存修改
按 esc 鍵 =>輸入:wq!
=>按 enter 鍵 退出編輯
讓配置生效
在終端執行命令
source ~/.zshrc
vim 編輯器基本語法,請參考 macbook 安裝 Git 和 安裝 Homebrew
參考:
Mac下iTerm2的安裝與配置
mac git命令按Tab不能自動補全解決方法
如何配置MAC的終端自動補全命令行,并忽略大小寫
MacOS 安裝配置 zsh
Mac 終端配置 oh-my-zsh 和自動補全以及命令高亮
Mac 常用的 20 款效率神器推薦