Bootstrap

iTerm2 + Oh My Zsh 打造舒适终端体验

一、iTerm2简介

Mac OS自带的终端,用起来虽然有些不太方便,界面也不够友好,iTerm2是一款相对比较好用的终端工具。iTerm2常用操作包括主题选择、声明高亮、自动填充建议、隐藏用户名和主机名、分屏效果等。

效果图如下:
在这里插入图片描述


二、下载以及安装

Mac系统默认使用bash作为终端,但是新的Mac系统已经默认使用zsh,查看方式:

cat /etc/shells

   
   
  • 1

结果:

/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

bash与zsh切换命令:

chsh -s /bin/zsh
chsh -s /bin/bash

   
   
  • 1
  • 2

bash与zsh配置文件:

  • bash的配置文件~/.bash_profile
  • zsh的配置文件~/.zshrc



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

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


也可以直接使用Homebrew进行安装:

brew cask install iterm2

   
   
  • 1



三、iTerm2主题配置

iTerm2 最常用的主题是 Solarized Dark theme。

下载地址:http://ethanschoonover.com/solarized

github地址:https://github.com/altercation/solarized

下载的是压缩文件,解压,然后打开iTerm2,按Command + , 键,打开 Preferences 配置界面,然后 ProfilesColorsColor Presets ,在下拉列表中选择 Import,选择刚才解压的 solarizediterm2-colors-solarizedSolarized Dark.itermcolors文件,导入成功后,在 Color Presets下选择 Solarized Dark 主题,就可以了。
在这里插入图片描述
在这里插入图片描述
效果如下:
在这里插入图片描述


四、配置Oh My Zsh

Oh My Zsh 是对主题的进一步扩展。

下载地址:https://github.com/robbyrussell/oh-my-zsh


1、安装方式

(1)一键安装

via curl安装方式

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

   
   
  • 1

via wget安装方式

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

   
   
  • 1

(2)手动安装

github下载地址:https://github.com/ohmyzsh/ohmyzsh

## 找到仓库, 然后clone到本地
git clone https://github.com/ohmyzsh/ohmyzsh.git
## 把仓库复制到 .oh-my-zsh目录
cp -r ohmyzsh ~/.oh-my-zsh
## 复制.zshrc
cp ohmyzsh/templates/zshrc.zsh-template ~/.zshrc
## shell换成zsh
chsh -s /bin/zsh

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3、切换zsh

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

chsh -s /bin/zsh

   
   
  • 1

4、修改主题

将主题修改为 ZSH_THEME="agnoster"

vim ~/.zshrc

   
   
  • 1

输入 i 进入编辑模式,将 ZSH_THEME="" 编辑为 ZSH_THEME="agnoster"

然后按 esc 键,退出编辑,:wq 保存退出。
在这里插入图片描述
agnoster 是比较常用的 zsh 主题之一,你可以挑选你喜欢的主题。

zsh 主题列表:https://github.com/robbyrussell/oh-my-zsh/wiki/themes

注:上面提到的via wget安装方式需要安装wget,方法如下: 直接终端使用homebrew安装(前提是已经安装了homebrew)

安装命令:

brew install wget

   
   
  • 1

检验是否安装成功:

wget http://www.arefly.com/

   
   
  • 1

在这里插入图片描述



五、配置Meslo字体

使用上面的主题,需要 Meslo 字体支持,要不然会出现乱码的情况。

字体下载地址:Meslo LG M Regular for Powerline.ttf

下载好之后,找到对应的字体,直接在 Mac OS 中安装即可。

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

在这里插入图片描述


六、声明高亮

zsh-syntax-highlighting 插件为 shell zsh 提供语法高亮显示。当命令在 zsh 提示符下输入到交互式终端时,它可以突出显示命令。这有助于在运行命令之前检查命令,特别是捕获语法错误。

Homebrew 安装:

brew install zsh-syntax-highlighting

   
   
  • 1

git 命令安装:

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

   
   
  • 1
  • 2

安装成功之后,编辑 vim ~/.zshrc 文件,找到 plugins,我们需要把高亮插件加上:

plugins=(git zsh-syntax-highlighting)

   
   
  • 1

在这里插入图片描述
注:请务必保证插件顺序,zsh-syntax-highlighting 必须在最后一个。

然后在文件末尾添加:

source ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

   
   
  • 1

在这里插入图片描述
接着保存退出,然后执行下面的命令立即生效:

source ~/.zshrc

   
   
  • 1

高亮显示效果如下:
在这里插入图片描述


七、自动建议填充

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

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

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

   
   
  • 1

然后编辑 vim ~/.zshrc 文件,找到 plugins 配置,增加 zsh-autosuggestions 插件。
在这里插入图片描述
注:请务必保证插件顺序,zsh-syntax-highlighting 必须在最后一个。

有时候因为自动填充的颜色和背景颜色很相似,以至于自动填充没有效果,我们可以手动更改下自动填充的颜色配置,我修改的颜色值为:586e75,例如:
在这里插入图片描述
效果如下:
在这里插入图片描述


八、iTerm2快速隐藏和显示

这个功能也非常使用,就是通过快捷键,可以快速的隐藏和打开 iTerm2

打开 iTerm2,按 Command + , 键,打开 Preferences 配置界面,然后 ProfilesKeysHotkey,自定义一个快捷键就可以了,示例配置(option + Space):
在这里插入图片描述


九、iTerm2隐藏用户名和主机

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

编辑 vim ~/.zshrc 文件,增加 DEFAULT_USER="davis" 配置,示例:
在这里插入图片描述
我们可以通过 whoami 命令,查看当前用户,效果如下:
在这里插入图片描述


十、iTerm2快捷键

命令说明
command + enter进入与返回全屏模式
command + t新建标签
command + w关闭标签
command + 数字 command + 左右方向键切换标签
command + f查找
command + d水平分屏
command + shift + d垂直分屏
command + option + 方向键切换屏幕
command + shift + h查看剪切板历史
ctrl + u清除当前行
ctrl + l清屏
ctrl + a到行首
ctrl + e到行尾
ctrl + f/b前进/后退
ctrl + p上一条命令
ctrl + r搜索命令历史



十一、卸载oh-my-zsh

进入到 .oh-my-zsh/tools 目录,依次执行:

cd .oh-my-zsh/tools
chmod +x uninstall.sh
./uninstall.sh
rm -rif .zshrc

   
   
  • 1
  • 2
  • 3
  • 4



十二、问题

1、问题一:iTerm2打开报错

打开终端一直报如下错误:

compinit:503: no such file or directory: /usr/local/share/zsh/site-functions/_brew_cask

   
   
  • 1

解决方法:

若是装了 brew 的,有可能是缓存记录导致的,执行如下命令可以修正:

brew cleanup

   
   
  • 1

2、问题二:terminal终端乱码

iTerm2 主题字体配置好后,打开Mac自带的 terminal 时出现乱码问题。

解决方法:

打开 terminal 的偏好设置修改字体如下:

Meslo LG Regular for Powerline

   
   
  • 1

在这里插入图片描述





;