构建 iTerm2 环境

引言

前段时间调整了一下 iTerm2 的环境,感觉比以前好看多了,并且更加高效,这里做一个记录,希望能给大家一些启发,同时也是想以后能根据此文章快速复现”贝贝猫-iTerm2-Style”。

工具介绍

  • brew:Mac OS 下强大的包管理软件。
  • iTerm2:iTerm2是 Mac OS 终端的替代品,也是iTerm的继承者。iTerm2将终端带入了”只有想不到,没有做不到”的时代。
  • zsh:zsh 是 shell 语言类型,兼容bash,提供强大的交互式命令行功能,比如 tab 补全,自动纠错功能等。但是有一个很大的缺点,就是配置太麻烦。
  • oh-my-zsh:管理 zsh 配置的框架,只需要简单的改改配置文件,就能让 zsh 用的很顺手。

本文叙述的内容主要就围绕这这几款在 Mac OS 下比较流行的工具,brew 帮我们安装一些需要的包,iTerm2 是我们的 shell 终端程序,zsh 给我们极佳的命令行交互体验,oh-my-zsh 帮我们更加方便的配置 zsh。

安装步骤

安装 brew

1
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

如果上述代码失效,可以参考官网

在安装本文所述的所有软件时,可能会因为国内访问国外资源网络比较慢,而效率低下。所以对于那些有代理服务器的朋友,可以在每条命令前加一句 ALL_PROXY=socks5://127.0.0.1:10808,我这里的 ss 代理,开放的本地端口是10808。当然您也可以执行 export ALL_PROXY=socks5://127.0.0.1:10808,使其作用于整个 shell session。

安装 & 配置 iTerm2

安装

1
2
brew tap caskroom/cask
brew cask install iterm2

安装成功后在 Launchpad 中可以看到 iTerm2 的应用图标出现,打开 iTerm2。

配置颜色

先检查下 iTerm2 颜色类型为 xterm-256color,位置在 iTerm2 -> Preferences -> Profiles -> Terminal
256-color
接下来我们调整一下 iTerm2 的配色方案,让它更加好看,这里我们先下载配色合集。

1
2
mkdir ~/iterm2 && cd ~/iterm2
git clone https://github.com/mbadolato/iTerm2-Color-Schemes

然后打开 iTerm2 的配置,调整其配色方案,位置在 iTerm2 -> Preferences -> Profiles -> Default -> Colors -> Color Presets... -> Import,然后选择 ~/iterm2/schemes 下的所有文件。
colors
导入刚才下载好的所有配色方案后,就可以在 Color Preset 中进行选择,我这里选择了 Dracula

配置字符集

配好颜色方案后,开始配置字符集,之后我们在使用 zsh 时,会涉及到许多图标类的字符,这些在默认字符集中是没有的,所以我们这里安装一个包含所有图标的字符集nerd-fonts
nerd-fonts
使用 brew 安装:

1
2
brew tap caskroom/fonts
brew cask install font-hack-nerd-font

安装成功后需要在 iTerm2 中配置一下,在 iTerm2 -> Preferences -> Profiles -> Default -> Text -> Font,Text 下面勾选 Use a different font for non-ASCII text,然后在 Non-ASCII font 中选择 Hack Nerd Font
font

安装 zsh

使用 brew 安装:

1
brew install zsh

然后将默认 shell 切换成 zsh:

1
2
sudo sh -c "echo $(which zsh) >> /etc/shells"
chsh -s $(which zsh)

安装 oh-my-zsh

安装过程如下:

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

如果安装出错,可以参考官方文档

安装好之后可以看到界面发生了一点点变化,同时会产生一个名为 .zshrc 的配置文件,在用户目录下面,我们以后主要就是修改它了。

配置过程

配置主题

oh-my-zsh 的所有主题文件都存储在 ~/.oh-my-zsh/themes 目录下,接下来我们安装一个好看的主题 powerlevel9k

1
git clone https://github.com/bhilburn/powerlevel9k.git ~/.oh-my-zsh/custom/themes/powerlevel9k

然后修改配置文件 open -e ~/.zshrc:

1
ZSH_THEME="powerlevel9k/powerlevel9k"

修改完成后刷新一下配置:

1
source ~/.zshrc

接下来,我们再配置一下 powerlevel9k 的主题,在 ~/.zshrc 中加入如下内容:

1
2
3
4
5
6
7
# 设置字符集
POWERLEVEL9K_MODE="nerdfont-complete"
# Customise the Powerlevel9k prompts
# 命令行开头的元素
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ssh context dir vcs newline status)
# 命令行结尾的元素
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status root_indicator background_jobs history dir_writable time )

上述是我使用的主题配置,更详细的配置项可以参考官方指南 Prompt CustomizationStylizing Your Prompt

添加别名

配置好主题之后,我们在 ~/.zshrc 中,再加入一些命令别名的设置,这能让我们操作的更有效率。

1
2
3
4
5
6
alias cls='clear'
alias ll='ls -l'
alias la='ls -a'
alias vi='vim'
alias proxy="ALL_PROXY=socks5://127.0.0.1:10808"
alias grep='grep --color=auto'

使用插件

zsh 除了上述基本配置外,还有许多不错的插件,这里有一个 插件合集,各位同学可以自由选择,这里只介绍我使用的插件。

fast-syntax-highlighting

zsh-syntax-highlighting 用于高亮你的 zsh 可用命令,比如输入 sleep、cat 这些命令的时候就会高亮。

1
git clone https://github.com/zdharma/fast-syntax-highlighting.git ~/.oh-my-zsh/custom/plugins/fast-syntax-highlighting

下载完成后,将 fast-syntax-highlighting 添加到 ~/.zshrc 的插件列表中 plugins=(here to list your plugins)

zsh-autosuggestions

根据曾经输入过的命令,进行命令行提示,按➡️键就能自动补全。

1
git clone https://github.com/zsh-users/zsh-autosuggestions ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions

下载完成后,将其添加到 ~/.zshrc 的插件列表中plugins=(zsh-autosuggestions)

整体配置

我的最终配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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/CL10060-N/.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="powerlevel9k/powerlevel9k"

# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in ~/.oh-my-zsh/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"

# Uncomment the following line to automatically update without prompting.
# DISABLE_UPDATE_PROMPT="true"

# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS=true

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load?
# Standard plugins can be found in ~/.oh-my-zsh/plugins/*
# Custom plugins may be added to ~/.oh-my-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 fast-syntax-highlighting
)

source $ZSH/oh-my-zsh.sh

# User configuration

# export MANPATH="/usr/local/man:$MANPATH"

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
# export EDITOR='vim'
# else
# export EDITOR='mvim'
# fi

# 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"

# aliases:
alias cls='clear'
alias ll='ls -l'
alias la='ls -a'
alias vi='vim'
alias proxy="ALL_PROXY=socks5://127.0.0.1:10808"
alias grep='grep --color=auto'

POWERLEVEL9K_MODE="nerdfont-complete"
# Customise the Powerlevel9k prompts
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ssh context dir vcs newline status)
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status root_indicator background_jobs history dir_writable time )
POWERLEVEL9K_PROMPT_ADD_NEWLINE=true

IDE 适配

使用 zsh 后,IDE 的默认 Terminal 也可以换成了它,这里以 IntelliJ IDEA 为例,主要需要设置 shell path 和 font 字体集。

shell path 的设置过程:IntelliJ IDEA -> Preferences -> Tools -> Terminal,将其改成自己 zsh 的路径。
idea-shell-path
font 字体集的设置过程:IntelliJ IDEA -> Preferences -> Editor -> Font,将其改成 Hack Nerd Font
idea-font-settings

参考内容

[1] 打造 Mac 下高颜值好用的终端环境

贝克街的流浪猫 wechat
您的打赏将鼓励我继续分享!
  • 本文作者: 贝克街的流浪猫
  • 本文链接: https://www.beikejiedeliulangmao.top/tools/establish-iterm2-environment/
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
  • 创作声明: 本文基于上述所有参考内容进行创作,其中可能涉及复制、修改或者转换,图片均来自网络,如有侵权请联系我,我会第一时间进行删除。