iTerm2常用配置教程

3,113 阅读6分钟

1. 安装

1.1 官网安装

官网地址: iterm2.com/

选择对应的安装包下载即可

1.2 使用 Homebrew 安装

打开电脑终端,命令行输入如下命令回车:

brew install iTerm2  

2. 简单配置

2.1 设置iTerm2为默认终端

打开 iTerm2,左上角选择 iTerm2,红框内容点击即可

2.2 iterm2 配置主题

iTerm2主题网站: https://github.com/mbadolato/iTerm2-Color-Schemes

也有人推荐这个:http://ethanschoonover.com/solarized

下载 zip 包并解压到本地,进入解压缩的文件目录,找到 schemes 文件夹

schemes 文件夹中找到 Solarized Dark Higher Contrast.itermcolors 文件,此款主题配色最为流行,下面就以此主题为例进行导入和修改,这里我是下载了全量的配色方案,大家可以根据自己喜好选择不同的主题进行导入。

使用 command + ,打开设置

后续所有关于iTerm2的配置均在此选项下进行,不再赘述。

打开配置页面,Profiles -> Colors -> Color Presets -> Import,选择到刚刚解压的主题文件。

稍微改动

3. 简单配置 Oh My Zsh

什么是Oh-My-Zsh?

Oh My Zsh是一款社区驱动的命令行工具,它基于zsh命令行,提供了主题配置,插件机制,大大提高了可玩(用)性。提升使用效率还能使人愉快hhh

Oh My Zsh is a delightful, open source, community-driven framework for managing your Zsh configuration. It comes bundled with thousands of helpful functions, helpers, plugins, themes, and a few things that make you shout...

网站: ohmyz.sh/

3.1 更换 terminal

bashmacterminal 自带的 shell,把它换成 zsh,这个的功能要多得多。拥有语法高亮,命令行 tab 补全,自动提示符,显示 Git 仓库状态等功能。
使用下面命令设置默认 shellbashzsh 切换

# 切换到bash
chsh -s /bin/bash

# 切换到zsh
chsh -s /bin/zsh

使用命令如下,其中需要输入自己的本机密码

3.2 安装

3.2.1 官方下载

官网提供的两种方式

# via curl
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# via wget
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

结果报错了...

查了一下好像是 IP 的原因

由于一些原因,上面两种方法你都没能安装成功,可以试一下手动安装:

3.2.2 手动安装

下载 oh-my-zsh 源码

# 下载 oh-my-zsh 源码
git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
# 并且把 .zshrc 配置文件拷贝到根目录下
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
# 让 .zshrc 配置文件生效
source ~/.zshrc

第一条命令就报错。

这种错误一般是由于使用 git://*** 下载会出现,改成使用 https:// 方式访问即可。

blog.csdn.net/qq_36393978…

git clone https://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh

然后执行上方最后的两行命令, 效果如下:

3.3 下载到本地

github.com/ohmyzsh/ohm… 直接下载压缩包,zip -> 解压 -> 移动 oh-my-zsh 目录到根目录:

并依次执行下方命令

cd ~/Downloads
mv ohmyzsh-master ~/.oh-my-zsh
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
source ~/.zshrc

3.3 .zshrc

这个文件非常关键,是 oh-my-zsh 的配置文件,它的位置在根目录下,可以通过 vim ~/.zshrc 查看。

每一次修改它之后,如果想要立即生效需要手动执行 source ~/.zshrc

文件的备份:

# 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=$HOME/.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/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="agnoster"
DEFAULT_USER=$USER
# DEFAULT_USER="liudaxiang@LiuDaXiangdeMacBook-Pro"


# 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 $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 one of the following lines to change the auto-update behavior
# zstyle ':omz:update' mode disabled  # disable automatic updates
# zstyle ':omz:update' mode auto      # update automatically without asking
# zstyle ':omz:update' mode reminder  # just remind me to update when it's time

# Uncomment the following line to change how often to auto-update (in days).
# zstyle ':omz:update' frequency 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.
# You can also set it to another string to have that shown instead of the default red dots.
# e.g. COMPLETION_WAITING_DOTS="%F{yellow}waiting...%f"
# Caution: this setting can cause issues with multiline prompts in zsh < 5.7.1 (see #5765)
# 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 $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)
plugins=(git zsh-autosuggestions zsh-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"

3.4 配置 Oh My Zsh 插件

$ZSH/plugins/ 目录中提供了很多的插件。

ls $ZSH/plugins/

这些插件主要可以提供快捷键等功能。

每个插件的目录下有一个 READMD.md 文件,里面详细介绍了插件的使用。下面以 git 为例:

ls $ZSH/plugins/gitcat$ZSH/plugins/git
cat ZSH/plugins/gitcatZSH/plugins/git/README.md

这样就可以列出 $ZSH/plugins/git 目录下的文件,并查看 $ZSH/plugins/gitcat 文件夹里的 README.md 文件的内容。

配置插件: 编辑 ~/.zshrc文件,在 plugins 选项中加入想要配置的插件,之后使用 source 生效。

3.5 配置 Oh My Zsh 主题

ohmyzsh提供了很多的主题(theme),完整的样例可以在下方开源地址看到:

开源地址: github.com/ohmyzsh/ohm…

每种主题都有自己的格式与一个唯一对应的名称。

配置方法: 编辑 ~/.zshrc 文件,设置 ZSH_THEME 的内容为自己想要配置选项的值。

针对本文中使用的 iterm2 主题,我们使用 open -e ~/.zshrc 编辑 ~/.zshrc 文件,将主题配置修改为 ZSH_THEME="agnoster"

如果你想每天都过得不一样,可以设置成 random ,每次打开 iTerm2 的都会随机使用一种配色方案。

agnoster 是比较常用的 zsh 主题之一

zsh 主题列表: github.com/robbyrussel…

最后当然是source一下,让改变生效

source ~/.zshrc

4. 配置 Meslo 字体

使用上面的主题,需要 Meslo 字体支持,要不然会出现乱码的情况,字体下载地址:github.com/powerline/f…

下载好之后,直接在 Mac OS 中安装即可。

然后打开 iTerm2,按 Command + , 键,打开 Preferences 配置界面,然后 Profiles -> Text -> Font -> Chanage Font ,选择 Meslo LG M Regular for Powerline 字体。

5. 实现命令声明高亮

概念: 默认情况下,系统命令都是以白色显示。为了与内容区分开来,可以安装下面的插件,安装完成之后pwd,ls等系统命令都会以高亮显示。

开源地址如下: github.com/zsh-users/z…

配置方法: 首先将配置目录移动/拷贝到$ZSH_CUSTOM/plugins/目录下,然后编辑~/.zshrc文件,在plugins参数中添加zsh-syntax-highlighting,最后使用source生效。

cp -r zsh-syntax-highlighting $ZSH_CUSTOM/plugins/ vim ~/.zshrc

在这个路径下 cd ~/.oh-my-zsh/custom/plugins 安装下载

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git

然后激活这个插件,通过在

open -e  ~/.zshrc

中找到 plugins 加入插件的名字,其中 zsh-autosuggestions 是下一节“自动补充”所用的插件,本节直接加上

plugins=(git zsh-autosuggestions zsh-syntax-highlighting)

编辑后保存

最后 source 一下,让改变生效

source ~/.zshrc

出现这个错误

这个错误上面提到了,直接忽略,进行下一步。

6. 实现命令自动补充

概念: 普通的命令补全需要按 2 下【tab】键,安装了补齐插件之后,系统会自动显示补全命令。

开源地址如下: github.com/zsh-users/z…

配置方法: 首先将配置目录移动/拷贝到 $ZSH_CUSTOM/plugins/ 目录下,然后编辑~/.zshrc文件,在plugins中添加zsh-autosuggestions,最后使用source生效。

这个功能是非常实用的,可以方便我们快速的敲命令。

配置步骤,先克隆 zsh-autosuggestions 项目,到指定目录:

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

最后当然是 source 一下,让改变生效

source ~/.zshrc

效果如下:

蓝色为高亮,绿色为输入完整的命令,红色为提示的命令

7. Iterm状态栏的配置

Iterm 的状态栏默认情况下是关闭的,可以通过下面的方式开启:Perferences->Profiles->Session->底部勾选Status bar enabled

配置状态栏:状态栏提供了很多选项卡,可以配置在状态栏中。方法如下:

Status bar enabled 右侧点击 Configure Status Bar ->将想要的选项卡拖动到Active Components中 ->如果想要改变配色,可以设置下面的Auto-Rainbow选项

选择后:

调整状态栏的位置:例如下面将状态栏设置为在底部显示。


此时效果为:

换一种效果

效果为:

8. iTerm2 隐藏用户名和主机名

8.1 oh my zsh 修改用户名和主机名

有时候我们的用户名和主机名太长,比如我的 liudaxiang@LiuDaXiangdeMacBook-Pro ,终端显示的时候会很不好看(上面图片中可以看到),我们可以手动去除。

我们用的是 item2 + oh-my-zsh 组合,假如你用的主题是 agnoster,修改方法是进入 oh-my-zsh/themes/ 然后 vi agnoster.zsh-theme,编辑主题配置文件,找到如下代码:

# Context: user@hostname (who am I and where am I)
prompt_context() {
  if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
   # 修改如下代码,@Mac可以你自己定义
   # prompt_segment black default "%(!.%{%F{yellow}%}.)$USER@%m"
   prompt_segment black default "$"
  fi
}

或者换一种方式

8.2 ~/.zshrc 修改用户名和主机名

有时候我们的用户名和主机名太长,终端显示的时候会很不好看,我们可以手动去除。

编辑 open -e ~/.zshrc 文件,增加 DEFAULT_USER="clcnboss" 配置.(这个地方应该设置为: DEFAULT_USER="你电脑的用户名")

然后 source 一下

source ~/.zshrc

不好使...

换一种方式将 DEFAULT_USER=$USER 添加进去

最终效果为:

9. 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    搜索命令历史

10. 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

参考

  1. juejin.cn/post/684490… 超级详细的博客
  2. sspai.com/post/63241
  3. www.cnblogs.com/diyxiaoshit… 字体、高亮、自动补充
  4. www.jianshu.com/p/f45f64cd6… on my zsh 的两种安装方式
  5. blog.csdn.net/qq_36393978… git 报错->https
  6. juejin.cn/post/697306… 配置命令高亮、命令自动补充概念;配置 Oh My Zsh 主题,插件
  7. blog.csdn.net/weixin_4229… 简单调整,iterm 状态栏配置
  8. www.jianshu.com/p/ba08713c2… 卸载 oh my zsh
  9. www.jianshu.com/p/567140962… 隐藏用户名