macOS上配置和使用iTerm2

358 阅读1分钟

iTerm2安装

使用brew安装

// 安装 brew : 
// 官网地址提供:https://brew.sh/
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

// 是否安装成功
brew -v

// 安装 iterm2
brew install iterm2

iTerm2基本配置

设置为默认终端

iterm2菜单栏 -> iTerm2 -> Make iTerm2 Default Term

开启启动快捷键

菜单栏 -> iTerm2 -> preferences -> Keys -> Hotkey

设置透明度

菜单栏 -> iTerm2 -> preferences -> Profiles -> Window -> Transparency 拖动

配置zsh

macOS默认安装了zsh,但是需要手动启动

zsh --version

自带的zsh不好用,所以用oh-my-zsh框架来扩展它的功能

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

运行上面命令,可能会出现Failed to connect to raw.githubusercontent.com:443,一般都是翻墙的原因,可以通过配置如下解决

// 换成自己代理的接口即可
export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:789

代码高亮

  1. 下载zsh-syntax-highlighting
cd ~/.oh-my-zsh/custom/plugins
git clone git://github.com/zsh-users/zsh-syntax-highlighting.git

// 注意:如果使用git://报错:Unable to Connect to GitHub.com For Cloning
// 可以把git://换成https://即可,如下链接
// https://github.com/zsh-users/zsh-syntax-highlighting.git
  1. 启用zsh-syntax-highlighting
// 进入编辑器
vim   ~/.zshrc

// 在末尾添加代码
source ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
plugins=(zsh-syntax-highlighting)

// 保存之后运行
source ~/.zshrc

自动提示

  1. 下载zsh-autosuggestions
cd ~/.oh-my-zsh/custom/plugins
git clone git://github.com/zsh-users/zsh-autosuggestions
  1. 启用zsh-autosuggestions
// 编辑文件 
vim ~/.zshrc

// 在末尾添加代码
source ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
plugins=(zsh-autosuggestions)

cd ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions
vim zsh-autosuggestions.zsh 

// 将fg=8 修改 fg=10 
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=10' 

// 保存配置
source ~/.zshrc

配置iTerm2主题为dracula

下载主题

git clone https://github.com/dracula/iterm.git

激活主题

 1、iTerm2 > Preferences > Profiles > Colors Tab
 2Open the Color Presets... drop-down in the bottom right corner
 3Select Import... from the list
 4Select the Dracula.itermcolors file
 5Select the Dracula from Color Presets...

配置iTerm2显示显示git分支

针对macOS中默认显示的zsh脚本解释器

下载脚本到本地,比如命名为~/.git-prompt.sh

 https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh

编辑.zshrc: vim ~/.zshrc

GIT_PS1_SHOWUPSTREAM="auto"
GIT_PS1_SHOWCOLORHINTS="yes"
source ~/.git-prompt.sh
setopt PROMPT_SUBST
PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '

保存配置

source ~/.zshrc