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 进行安装:
$ brew cask install iterm2
注意:有些人MAC 上没有brew安装命令,请执行如下命令进行安装:
$ /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
一键安装:
$ 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 ➜ Library
安装好之后,需要把 Zsh 设置为当前用户的默认 Shell(这样新建标签的时候才会使用 Zsh):
$ chsh -s /bin/zsh
然后,我们编辑vim ~/.zshrc文件,将主题配置修改为ZSH_THEME="agnoster"。
$ 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 安装:
$ brew install zsh-syntax-highlighting
安装成功之后,编辑vim ~/.zshrc
文件,在最后一行增加下面配置:
source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
具体如下:
...................
# 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
保存后,执行变量生效命令
source /etc/profile
6. 自动建议填充
这个功能是非常实用的,可以方便我们快速的敲命令。
配置步骤,先克隆zsh-autosuggestions
项目,到指定目录:
$ 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):
# proxy list
alias proxy='export all_proxy=socks5://127.0.0.1:1086'
alias unproxy='unset all_proxy'
iTerm2 需要新建标签页,才有效果:
$ proxy
$ curl ip.cn
当前 IP:185.225.14.5 来自:美国 $ unproxy
$ curl ip.cn
当前 IP:115.236.186.130 来自:浙江省杭州市 电信
我们可以测试下:
$ 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上使用详解的更多相关文章
- 如何将NTFS格式的移动硬盘挂接到Mac OS上进行读写(Read/Write)操作
现在硬盘便宜,很多同学都有移动硬盘,如果你同时使用Windows与Mac OS的话,移动硬盘最好不要使用NTFS文件系统,否则在Mac OS上,你只能读你的移动硬盘,不能写. 但是实际上的情况是,移动 ...
- 【Xamarin挖墙脚系列:mac 终端 常用命令+Mac OS X的快捷键+beamoff 】
原文:[Xamarin挖墙脚系列:mac 终端 常用命令+Mac OS X的快捷键+beamoff ] 基本命令 1.列出文件 ls 参数 目录名 例: 看看驱动目录下有什么:ls /S ...
- 在 Mac OS 上创建并运行 ASP.NET Core 1.0 网站
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
- 在 Mac OS 上编译 FFmpeg
本文转自:在 Mac OS 上编译 FFmpeg | www.samirchen.com 安装 Xcode 和 Command Line Tools 从 App Store 上安装 Xcode,并确保 ...
- 转-在Mac OS上搭建Python的开发环境
在Mac OS上搭建Python的开发环境 本文转载自:http://www.jb51.net/article/76931.htm 一. 安装python mac系统其实自带了一个python的执 ...
- 在 Mac OS 上使用 TypeScript 编写 ASP.NET Core 1.0 应用
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
- Mac OS 上设置 JAVA_HOME
Mac OS 上设置 JAVA_HOME 原文链接:http://han.guokai.blog.163.com/blog/static/136718271201301183938165/ 由于需要, ...
- 在Mac OS上搭建Python的开发环境
本文转载自:http://www.jb51.net/article/76931.htm 一. 安装python mac系统其实自带了一个python的执行执行环境,用来运行python还行,但是开发可 ...
- 在 Mac OS 上编译 OBS
本文转自:在 Mac OS 上编译 OBS | www.samirchen.com 安装环境 第一步,做准备工作,安装编译 OBS 所需要的环境,流程如下: // 给当前用户添加 /usr/local ...
随机推荐
- Netty学习:ChannelHandler执行顺序详解,附源码分析
近日学习Netty,在看书和实践的时候对于书上只言片语的那些话不是十分懂,导致尝试写例子的时候遭遇各种不顺,比如decoder和encoder还有HttpObjectAggregator的添加顺序,研 ...
- 通过logmnr找到被修改前的存储过程
1.找到存储过程被修改时的归档日志 SELECT NAME FROM V$ARCHIVED_LOG WHERE FIRST_TIME BETWEEN TO_DATE('20191118080000', ...
- .NET Core部署到linux(CentOS)最全解决方案,高阶篇(Docker+Nginx 或 Jexus)
在前两篇: .NET Core部署到linux(CentOS)最全解决方案,常规篇 .NET Core部署到linux(CentOS)最全解决方案,进阶篇(Supervisor+Nginx) 我们对. ...
- 使用Python对MySQL数据库插入二十万条数据
1.当我们测试的时候需要大量的数据的时候,往往需要我们自己造数据,一条一条的加是不现实的,这时候就需要使用脚本来批量生成数据了. import pymysql import random import ...
- ModuleNotFoundError 模块寻找路径
t = os.path.dirname(os.path.dirname((os.path.dirname(os.path.abspath(__file__)))))os.path.sys.path.a ...
- C++ Primer Plus读书笔记(十)对象和类
1.类 不废话,上定义 class ClassName { public: xxx; private: xxx; protected: xxx; } private部分数据只能通过public 提供的 ...
- IntelliJ Idea 解决 Could not autowire. No beans of 'xxxx' type found 的错误提示
IntelliJ Idea 解决 Could not autowire. No beans of 'xxxx' type found 的错误提示哈,在使用 @Autowired 时,今天又遇一坑,这俩 ...
- 题解 UVA11694 【Gokigen Naname谜题 Gokigen Naname】
题目 题解 考场上连暴力都不会打的码农题,深搜是真的难 /kk 前置问题 怎么输出"\" cout<<"\\"; 2.怎么处理不在一个环里,可以考虑 ...
- windows.open、 window.location.href
windows.open("URL","窗口名称","窗口外观设定");打开新窗口,window对象的方法 不一定打开新窗口,只要有窗口的名 ...
- 最简单直接地理解Java软件设计原则之开闭原则
写在前面 本文属于Java软件设计原则系列文章的其中一篇,后续会继续分享其他的原则.想以最简单的方式,最直观的demo去彻底理解设计原则.文章属于个人整理.也欢迎大家提出不同的想法. 首先是一些理论性 ...