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

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 快捷命令

 

 愿意同Joker一起探索测试之路,可以关注公众号交流。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
iTerm2Mac OS上一款相对比较好用的终端工具,它具有一些特性,如主题选择、声明高亮、自动填充建议、隐藏用户名和主机名、分屏效果等。此外,iTerm2可以与Unix shell集成,通过安装相应的shell集成工具,在iTerm2中可以看到命令历史、当前工作目录、主机名、上传下载文件等信息。 对于长期与终端打交道的工程师来说,拥有一款称手的终端管理器是很有必要的。在Windows上,Xshell是最好的选择。而对于MacOS用户来说,iTerm2是非常受欢迎的终端工具。如果你对iTerm2感到不满意,那可能是由于个人的偏好或其他问题,并非iTerm2本身的问题。 总而言之,iTerm2是一款功能强大且灵活的终端工具,它在Mac OS终端操作上提供了更多的便利和个性化选项。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [MaciTerm2的安装与配置](https://blog.csdn.net/wangzhongshun/article/details/122089389)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Mac iTerm2 配置教程详细介绍](https://blog.csdn.net/Wjhsmart/article/details/114269257)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Anthony_路人甲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值