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

一、概述

因个人工作需要,使用终端工具进行运维和开发工作,但是Mac OS 自带的终端工具使用堡垒机登录配置不了,而且使用CRT等终端工具每次登录堡垒机都需要配置密码,操作起来很麻烦。一直想找一款终端工具便于日常工作使用,偶遇了iTerm2,发现很强大的一款终端工具,网上配置很多,但是有些地方会有些不清楚的地方,我这边参照网上文档,边部署边记录,整理了下属文档,便于大家参考和自身查阅。

iTerm2是Terminal的替代品,是iTerm的后继产品。它适用于MacOS 10.12或更高版本的Mac。iTerm2将终端带入了您从未想过一直想要的功能,使其进入了现代时代。

二、 具体介绍

1. 安装 iTerm2

下载地址:https://www.iterm2.com/downloads.html

下载的是压缩文件,解压后是执行程序文件,你可以直接双击,或者直接将它拖到 Applications 目录下。

或者你可以直接使用 Homebrew 进行安装:

1

$ brew cask install iterm2

注意:有些人MAC 上没有brew安装命令,请执行如下命令进行安装:

1

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

2. 配置 iTerm2 主题

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

下载的是压缩文件,你先解压一下,然后打开 iTerm2,按 【Command + , 】键,打开 Preferences 配置界面,然后Profiles -> Colors -> Color Presets -> Import,选择刚才解压的solarized->iterm2-colors-solarized->Solarized Dark.itermcolors文件,导入成功,最后选择 Solarized Dark 主题,就可以了。  该工具主题有很多,可以根据自己的爱好进行选择;

3. 配置 Oh My Zsh

Oh My Zsh 是对主题的进一步扩展,地址:https://github.com/robbyrussell/oh-my-zsh  

一键安装:

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

$ 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/sdone.

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

➜  Library

安装好之后,需要把 Zsh 设置为当前用户的默认 Shell(这样新建标签的时候才会使用 Zsh):  

1

$ chsh -s /bin/zsh

然后,我们编辑vim ~/.zshrc文件,将主题配置修改为ZSH_THEME="agnoster"。

1

2

3

4

5

6

7

8

9

10

11

12

13

$ 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/robbyrussell/oh-my-zsh/wiki/themes  

4. 配置 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 字体。  

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

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

5. 声明高亮

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

使用 Homebrew 安装:

1

$ brew install zsh-syntax-highlighting

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

1

source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh  

具体如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

...................

# 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  

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

1

source /etc/profile

6. 自动建议填充

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

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

1

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

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

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

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

7. 左右键跳转

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

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

8. iTerm2 快速隐藏和显示

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

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

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

编辑vim ~/.zshrc文件,增加DEFAULT_USER="eason"配置,示例:

10. iTerm2 配置代理

编辑~ vim ~/.zshrc,增加下面配置(使用的 shadowsocks):

1

2

3

# proxy list

alias proxy='export all_proxy=socks5://127.0.0.1:1086'

alias unproxy='unset all_proxy'

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

1

2

3

4

5

6

7

$ proxy

$ curl ip.cn

当前 IP:185.225.14.5 来自:美国

$ unproxy

$ curl ip.cn

当前 IP:115.236.186.130 来自:浙江省杭州市 电信

我们可以测试下:  

1

2

3

4

5

6

7

8

9

10

11

12

$ 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>

11. iTerm2 快捷命令

 

iTerm2终端工具在Mac OS上使用详解相关推荐

  1. mac os maven_如何在Mac OS上安装Maven

    mac os maven 在Mac上安装Maven (Install Maven on Mac) Maven is the most widely used build and project dep ...

  2. html5 mac os 开发工具,如何通过TextMate或命令行在Mac OS上使用W3C tidy-html5软件包?...

    我在这里安装了W3C整洁,HTML5束: 〜/库/ Application Support /禽/原始副本/捆绑/整齐-HTML5如何通过TextMate或命令行在Mac OS上使用W3C tidy- ...

  3. 对比Mac OS上的PF与iptables

    昨天同事问我怎么在Mac上配置策略路由,其实我也不知道!由于自己的实际需求,一直以来都想玩Mac网络功能,可是目光总被它炫烂的外表炫晕!今日同事这么一问我,顿时产生一种研究其究竟的欲望,还好,家里的电 ...

  4. Mac OS上用item2连接CentOS7

    Mac OS上用item2连接CentOS7 (1)item2:打开preference: (2)选择"Profile": (3)添加: 在 /Users/你的用户名/Librar ...

  5. mac安装rstudio_在Windows / Linux / Mac OS上安装R和RStudio入门

    mac安装rstudio 在Windows上安装R (Installing R on Windows) Go to r-project.org on your internet browser.在您的 ...

  6. 1.20——golang环境配置(在Mac OS上)【没用过】

    本节主要为大家讲解如何在Mac OS上安装Go语言开发包,大家可以在Go语言官网下载对应版本的的安装包(https://golang.google.cn/dl/),如下 图所示. 安装Go语言开发包 ...

  7. mac memcached_如何在Mac OS上安装Memcached Server

    mac memcached Memcached is an open-source high performance distributed memory object caching system. ...

  8. mac os上配置CDH4.1.1版hbase并启用snappy

    这两天乘周末时间在我的mac机器上配置了CDH4.1.1版的hadoop和hbase.hbase配置在伪分布式的hdfs上.整个配置倒也非常简单.使用CDH版hadoop和hbase比用社区版要简单太 ...

  9. mac电脑使用入门详解

    大家来到MAC系统以后,首先在使用习惯以及界面上,都需要有一个适应的过程,小编为大家整理了这个小白教程,有助于你快速习惯这个MAC系统. 讲个笑话: 我一朋友看到Mac界面很酷,就买了个Mac笔记本, ...

最新文章

  1. 中国碳酸氢钠干粉灭火剂市场产量规模与未来竞争走势研究报告2022年
  2. JavaScript 参考教程(二)——转载
  3. go 写文件_如何在 Ubuntu 20.04 上安装 Go
  4. 蓝桥杯第九届省赛JAVA真题----螺旋折线
  5. python静态方法怎么调用_python实例方法、静态方法和类方法
  6. js中__proto__和prototype的区别和联系
  7. php导出数组到csv格式demo
  8. Axure RP 9 使用详解视频教程-真实操作步骤
  9. 答粉丝问|火狐浏览器插件简介
  10. 清华大学孙富春教授:基于知识智能的机器人技能学习
  11. Android面试你必须要知道的那些知识,已拿offer附真题解析
  12. SWUST OJ 77: 计算员工周工资
  13. 《调色师手册:电影和视频调色专业技法(第2版)》——第2章 调色工作的环境设定 基于监视器的色彩管理...
  14. 夜游经济文化旅游如何打造城市IP
  15. CentOS7虚拟机取消屏幕保护
  16. 同一服务器 多网站,同一服务器 多网站
  17. android StudioSVN版本冲突解决
  18. Toast系列(五):还在被关闭通知无法显示Toast所困扰?解决方案来了
  19. 武汉理工大学《软件工程》复习总括一
  20. python txt 和 xml 格式互转

热门文章

  1. 简述Windows XP与Windows 7系统常见漏洞
  2. 那些年啊,那些事——一个程序员的奋斗史 ——49
  3. 软件开发定制的类型有哪些?
  4. 自媒体视频有哪些平台?这些平台都可以发视频!
  5. 制作单词记录App(二)
  6. android两次返回退出程序,几种退出安卓应用的方式:退回桌面,弹出退出确认框,连续两次返回键退出...
  7. 华为U9508荣耀2四核ROOT及添加GMS
  8. 使用pytorch出现src/tcmalloc.cc:277] Attempt to free invalid pointer 0x7f4685efcd40 Aborted (core dumpe)
  9. 2020.08.28(写点题)
  10. 利用 UMA 使硬件加速器可直接用于 TVM