一、iTerm2簡介:(轉載)
Mac OS
自帶的終端,用起來雖然有些不太方便,界面也不夠友好,iTerm2
是一款相對比較好用的終端工具.iTerm2
常用操作包括主題選擇、聲明高亮、自動填充建議、隱藏用戶名和主機名、分屏效果等.
先來看效果圖:
![# 一、iTerm2簡介:
Mac OS
自帶的終端,用起來雖然有些不太方便,界面也不夠友好,iTerm2
是一款相對比較好用的終端工具.iTerm2
常用操作包括主題選擇、聲明高亮、自動填充建議、隱藏用戶名和主機名、分屏效果等.
先來看效果圖:
二、下載及安裝
補充一些關于shell的介紹:
Mac系統默認使用dash作為終端,但是新的Mac系統已經默認使用zsh.查看方法:
可以自行查看系統當前 shell
$ cat /etc/shells
/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
以及切換shell
chsh -s /bin/zsh
chsh -s /bin/bash
其中,
bash的配置文件是 -/.bash_profile
zsh的配置文件是-/.zshrc
位置在用戶家目錄下,為隱藏文件
1.在finder根目錄中command + shift + .
顯示隱藏文件即可看到.同時可以看到bash 、csh 、dash 、zsh等都在這個目錄下,
iTerm2
下載地址:https://www.iterm2.com/downloads.html
1、
下載的是壓縮文件,解壓后直接雙擊執行程序文件,或者直接將它拖到 Applications 目錄下。
2、
也可以直接使用Homebrew
進行安裝:
$ brew cask install iterm2
安裝完打開,這是iTem最初的樣子,基于此可以安裝各種插件來優化了...
三 、配置 iTerm2 主題
iTerm2 最常用的主題是 Solarized Dark theme,下載地址:
http://ethanschoonover.com/solarized
https://github.com/altercation/solarized github地址
下載的是壓縮文件,解壓,然后打開 iTerm2,按Command +
鍵,打開 Preferences 配置界面,然后Profiles -> Colors -> Color Presets,在下拉列表中選擇 Import,選擇剛才解壓的solarized->iterm2-colors-solarized->Solarized Dark.itermcolors文件.導入成功后,在 Color Presets下選擇 Solarized Dark 主題,就可以了。
效果圖:
三 、設置 iTerm2 背景圖片
打開 iTerm2,按Command +
鍵,打開 Preferences 配置界面Profiles -> Window->Background mage,選擇一張自己喜歡的背景圖.
四 、 配置 Oh My Zsh
Oh My Zsh
是對主題的進一步擴展,下載地址https://github.com/robbyrussell/oh-my-zsh
1.一鍵安裝:
via curl方式:
$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
via wget方式:
$sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
2.手動方式安裝,如下:
(git地址: https://github.com/ohmyzsh/ohmyzsh)
## 找到倉庫, 然后clone到本地
git clone https://github.com/ohmyzsh/ohmyzsh.git
## 把倉庫復制到 .oh-my-zsh目錄
cp -r ohmyzsh ~/.oh-my-zsh
## 復制.zshrc
cp ohmyzsh/templates/zshrc.zsh-template ~/.zshrc
## shell換成zsh
chsh -s /bin/zsh
3.安裝好之后,需要把 Zsh 設置為當前用戶的默認 Shell(這樣新建標簽的時候才會使用 Zsh):
$ chsh -s /bin/zsh
4.然后,將主題修改為ZSH_THEME="agnoster"
。
$ vim ~/.zshrc
輸入i
進入編輯模式,將ZSH_THEME=""
編輯為 ZSH_THEME="agnoster"
按下esc
鍵,退出編輯,:wq
保存退出:
修改成功!
agnoster
是比較常用的 zsh 主題之一,你可以挑選你喜歡的主題,zsh 主題列表:https://github.com/robbyrussell/oh-my-zsh/wiki/themes
補充:
上面提到的via wget安裝方式需要安裝wget,方法如下:
直接終端使用homebrew安裝(前提是已經安裝了homebrew)
安裝命令:
$brew install wget
檢驗是否安裝成功:
$ wget http://www.arefly.com/
五 、配置 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 字體。
六 、聲明高亮
特殊命令和錯誤命令,會有高亮顯示。
使用 Homebrew 安裝:
$ brew install zsh-syntax-highlighting
安裝成功之后,編輯vim ~/.zshrc文件,在最后一行增加下面配置:
source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
方式2
執行下面指令自動安裝
$ git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_CUSTOM/plugins/zsh-syntax-highlighting
然后編輯vim ~/.zshrc
文件,找到plugins配置,增加zsh-autosuggestions
插件。
七、自動建議填充
這個功能是非常實用的,可以方便我們快速的敲命令。
配置步驟,先克隆zsh-autosuggestions項目,到指定目錄:
$ git clone https://github.com/zsh-users/zsh-autosuggestions ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions
然后編輯vim ~/.zshrc
文件,找到plugins配置,增加zsh-autosuggestions
插件。
八、iTerm2 快速隱藏和顯示窗體:
打開 iTerm2,按Command + ,
鍵,打開 Preferences 配置界面,然后Profiles → Keys →Hotkey,自定義一個快捷鍵就可以了。
九、iTerm2 隱藏用戶名和主機名
有時候我們的用戶名和主機名太長,終端顯示的時候會很不好看,我們可以手動去除。
編輯vim ~/.zshrc
文件,增加DEFAULT_USER="SKong"
配置.
但是: 我的電腦沒生效,至今未找到原因~~~
補充:已經找到原因,這個地方應該設置為: DEFAULT_USER="你電腦的用戶名"
比如:我的電腦用戶名為pactepacterara,則DEFAULT_USER="pactepacterara"
感謝網友碼渣!
效果對比:
我們可以通過whoami
命令,查看當前用戶
十、iTerm2 快捷命令
在iterm2中,選中某個路徑或者某個詞匯,iterm2就自動復制了,很方便。
command + enter 進入與返回全屏模式
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 搜索命令歷史
十、卸載 oh-my-zsh
進入到.oh-my-zsh/tools目錄,依次執行
$cd .oh-my-zsh/tools
chmod +x uninstall.sh
./uninstall.sh
rm -rif .zshrc
補充
我們來看一下/.zshrc的文件源碼, 很多zsh的配置都在這里面
vim ~/.zshrc