前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mac 最强大的终端工具之 iTerm2 详解

Mac 最强大的终端工具之 iTerm2 详解

作者头像
子晋
发布2022-01-18 21:40:05
18.6K0
发布2022-01-18 21:40:05
举报
文章被收录于专栏:子晋城子晋城

iTerm2 介绍

iTerm2 是一款功能强大的终端工具,也可以说是 Terminal 的替代品,也可以说是 iTerm 的后继产品。它适用于 macOS 10.12 或更高版本的 macOS。

它支持分窗口操作、自动补齐、粘贴历史、回放功能、全屏等功能,是一款非常强大、非常值得推荐的终端工具。

安装 iTerm2

  • 可以去 iTerm2 官网,下载并安装。$ brew cask install iterm2
  • 可以直接使用 Homebrew 进行安装:

注意

如果你的 Mac 上没有安装 brew 命令,请先执行如下命令进行安装:

代码语言:javascript
复制
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

配置 iTerm2 主题

iTerm2 最常用的主题是 Solarized Dark theme,下载地址:https://ethanschoonover.com/solarized

下载的是压缩文件,你先解压一下,然后打开 iTerm2,按 【Command + , 】键,打开 Preferences 配置界面,然后 Profiles -> Colors -> Color Presets -> Import,选择刚才解压的 solarized -> iterm2-colors-solarized -> Solarized Dark.itermcolors 文件,导入成功,最后选择 Solarized Dark 主题,就可以了,如下所示:

该工具主题有很多,可以根据自己的爱好进行选择。

配置 oh-my-zsh

oh-my-zsh 是对主题的进一步扩展,Github 地址:https://github.com/ohmyzsh/ohmyzsh

你可以通过 curl 或 wget 命令进行安装:

  • curl 安装
代码语言:javascript
复制
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  • wget 安装
代码语言:javascript
复制
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

例如,我这里通过 curl 命令进行安装:

代码语言:javascript
复制
$ sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Cloning Oh My Zsh...
xcode-select: note: no developer tools were found at '/Applications/Xcode.app', requesting install. Choose an option in the dialog to download the command line developer tools.
Error: git clone of oh-my-zsh repo failed
Eason:Library linan$ sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Cloning Oh My Zsh...
Cloning into '/Users/linan/.oh-my-zsh'...
remote: Enumerating objects: 1089, done.
remote: Counting objects: 100% (1089/1089), done.
remote: Compressing objects: 100% (1030/1030), done.
remote: Total 1089 (delta 23), reused 874 (delta 19), pack-reused 0
Receiving objects: 100% (1089/1089), 716.00 KiB | 65.00 KiB/s, done.
Resolving deltas: 100% (23/23), done.
 
Looking for an existing zsh config...
Using the Oh My Zsh template file and adding it to ~/.zshrc.
 
Time to change your default shell to zsh:
Do you want to change your default shell to zsh? [Y/n] y
Changing the shell...
Changing shell for linan.
Password for linan:
Shell successfully changed to '/bin/zsh'.
 
         __                                     __
  ____  / /_     ____ ___  __  __   ____  _____/ /_
 / __ \/ __ \   / __ `__ \/ / / /  /_  / / ___/ __ \
/ /_/ / / / /  / / / / / / /_/ /    / /_(__  ) / / /
\____/_/ /_/  /_/ /_/ /_/\__, /    /___/____/_/ /_/
                        /____/                       ....is now installed!
 
 
Please look over the ~/.zshrc file to select plugins, themes, and options.
 
p.s. Follow us on https://twitter.com/ohmyzsh
 
p.p.s. Get stickers, shirts, and coffee mugs at https://shop.planetargon.com/collections/oh-my-zsh

安装好之后,需要把 zsh 设置为当前用户的默认 shell(这样新建命令的时候才会使用 zsh):

代码语言:javascript
复制
$ chsh -s /bin/zsh
``` 
 
然后,我们用 vim 命令编辑 ~/.zshrc 文件,将主题配置修改为 ZSH_THEME="agnoster"。
```javascript
$ vim ~/.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="/Users/linan/.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="agnoster"       // 修改主题

agnoster 是比较常用的 zsh 主题之一,你可以挑选你喜欢的主题,zsh 主题列表:https://github.com/ohmyzsh/ohmyzsh/wiki/themes

配置 Meslo 字体

使用上面的主题,需要 Meslo 字体支持,要不然会出现乱码的情况,字体下载地址:Meslo LG M Regular for Powerline.ttf

下载好之后,直接在 macOS 中双击安装即可。

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

当然,如果你觉得默认的12px字体大小不合适,可以自己进行修改。

另外,VS Code 的终端字体,也需要进行配置,打开 VS Code,按 Command + ,键,打开用户配置,搜索 fontFamily,然后将右边的配置增加"terminal.integrated.fontFamily": "Meslo LG M for Powerline",示例:

声明高亮

效果就是上面截图的那样,特殊命令和错误命令,会有高亮显示。

使用 Homebrew 安装:

代码语言:javascript
复制
$ brew install zsh-syntax-highlighting

安装成功之后,用 vim 命令编辑 ~/.zshrc 文件,在最后一行增加下面配置:

代码语言:javascript
复制
...................
# 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"
source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh 

保存后,执行变量生效命令:

代码语言:javascript
复制
source /etc/profile

自动补齐

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

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

代码语言:javascript
复制
$ git clone https://github.com/zsh-users/zsh-autosuggestions ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions

然后,用 vim 命令编辑 ~/.zshrc 文件,找到 plugins 配置,增加 zsh-autosuggestions 插件:

代码语言:javascript
复制
# 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 zsh-autosuggestions)

> 注意:

上面声明高亮,如果配置不生效的话,可以在 plugins 配置,再增加 zsh-syntax-highlighting 插件试试。

有时候因为自动填充的颜色和背景颜色很相似,以至于自动填充没有效果,我们可以手动更改下自动填充的颜色配置,我修改的颜色值为:3a717f,示例:  

左右键跳转

主要是按住 option + → or ← 键,在命令的开始和结尾跳转切换,原本是不生效的,需要手动开启下。

打开 iTerm2,按 Command + , 键,打开 Preferences 配置界面,然后 Profiles → Keys → Load Preset... → Natural Text Editing,就可以了。

iTerm2 快速隐藏和显示

这个功能也非常使用,就是通过快捷键,可以快速的隐藏和打开 iTerm2,示例配置快捷键:( Commond + . ):

iTerm2 隐藏用户名和主机名

有时候我们的用户名和主机名太长,比如我的 pengzhenjin@MacBook-Pro,终端显示的时候会很不好看,我们可以手动去除。用 vim 命令编辑 ~/.zshrc 文件,增加 DEFAULT_USER="pengzhenjin" 配置,如:

代码语言:javascript
复制
# 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/user/.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="pengzhenjin"

iTerm2 配置代理

用 vim 命令编辑 ~/.zshrc 文件,增加下面配置:

代码语言:javascript
复制
# proxy list
alias proxy='export all_proxy=socks5://127.0.0.1:1086'
alias unproxy='unset all_proxy'

iTerm2 需要新建标签页,才有效果: 

代码语言:javascript
复制
$ proxy
$ curl ip.cn
当前 IP:185.225.14.5 来自:美国
 
$ unproxy
$ curl ip.cn
当前 IP:115.236.186.130 来自:浙江省杭州市 电信

我们可以测试下:

代码语言:javascript
复制
$ curl https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
 
<html>
  <head>
    <title>Directory listing for /yum/repos/kubernetes-el7-x86_64/</title>
  </head>
  <body>
    <h2>Index of /yum/repos/kubernetes-el7-x86_64/</h2>
    <p></p>
    <a href="/yum/repos/kubernetes-el7-x86_64/repodata">repodata</a><br />
  </body>
</html>

iTerm2 常用命令快捷键

  • 新建命令窗口:command + t
  • 关闭命令窗口:command + w
  • 切换命令窗口:command + 左右方向键
  • 查找: command + f
  • 垂直分屏:command + d
  • 水平分屏:command + shift + d
  • 自动补齐: command +;
  • 命令历史: command + shift + h
  • 命令回放: command + alt + b
  • 光标移动到行首: ctrl + a
  • 光标移动到行末: ctrl + e
  • 前移一个字符: ctrl + f
  • 后退一个字符: ctrl + b
  • 清屏: ctrl + l
  • 显示历史命令: ctrl + p
  • 删除当前字符: ctrl + d
  • 删除前一字符: ctrl + h
  • 删除光标之前的字符: ctrl + w
  • 删除光标之前的整行: ctrl + n
  • 删除光标之后整行: ctrl + k

参考资料

iTerm2终端工具在Mac OS上使用详解

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-07-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • iTerm2 介绍
  • 安装 iTerm2
  • 配置 iTerm2 主题
  • 配置 oh-my-zsh
  • 配置 Meslo 字体
  • 声明高亮
  • 自动补齐
  • 左右键跳转
  • iTerm2 快速隐藏和显示
  • iTerm2 隐藏用户名和主机名
  • iTerm2 配置代理
  • iTerm2 常用命令快捷键
  • 参考资料
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档