Total Pageviews

Sunday, 22 February 2026

打造mac上的最强终端程序iterm2

 iTerm2 是 Mac OS 中使用最为广泛的终端工具,特有的插件 plugin 提供了主题选择、生命高亮、代码自动填充、分屏等功能,相较于自带的终端实在是强大太多。
安装iTerm2

下载地址:https://www.iterm2.com/downloads.html,通常有以下几种方式:

    直接下载压缩文件,解压后是执行程序文件,直接双击或拖到 Applications 目录下
    直接使用 Homebrew 进行安装,执行命令 brew cask install iterm2

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

先把 Zsh 设置为当前用户的默认 Shell(这样新建标签的时候才会使用 Zsh),macOS已经自带zsh ,但是它不是默认shell

cat /etc/shells

# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

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

# 输入如下命令更改zsh为默认shell
chsh -s /bin/zsh

# 查看是否切换成功, 返回 /bin/zsh 就对了。
echo $SHELL

安装 oh-my-zsh

sh -c "$(curl -fsSL <https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh>)"

特说说明:上文中的地址可能无法使用,使用https://gitee.com/mirrors/oh-my-zsh/ 此处为国内同步文件。

安装好后的界面如下:

  ____  / /_     ____ ___  __  __   ____  _____/ /_
 / __ \\/ __ \\   / __ `__ \\/ / / /  /_  / / ___/ __ \\
/ /_/ / / / /  / / / / / / /_/ /    / /_(__  ) / / /
\\____/_/ /_/  /_/ /_/ /_/\\__, /    /___/____/_/ /_/
                        /____/                       ....is now installed!
Please look over the ~/.zshrc file to select plugins, themes, and options.

p.s. Follow us at <https://twitter.com/ohmyzsh>.

p.p.s. Get stickers and t-shirts at <http://shop.planetargon.com>.

主题安装
将 zsh的主题更改为 Dracula

# 下载主题文件
git clone <https://github.com/dracula/zsh.git>

# 创建一个指向Oh my zsh主题文件夹的符号链接,DRACULA_THEME是你刚才下载主题的目录
ln -s $DRACULA_THEME/dracula.zsh-theme ~/.oh-my-zsh/themes/dracula.zsh-theme

或者你可以移动主题文件dracula.zsh-theme到~/.oh-my-zsh/themes/下
提示: 显示隐藏文件夹的快捷键为command+shift+.

# 修改zsh主题。编辑~(用户名)下.zshrc文件,修改ZSH_THEME为"dracula"
$ vim ~/.zshrc

修改 iterm 配色为 Dracula

# 克隆文件
git clone <https://github.com/dracula/iterm.git>

# 打开设置
在 iTerm2 中 打开 Preferences 下面的 Profiles 的 Colors Tab 选项,使用 Import 导入配置,导入配置后,选中对应的 dracula 主题背景即可。

插件安装
命令自动补齐插件 zsh-autosuggestions

先克隆zsh-autosuggestions项目,到指定目录,然后编辑vim ~/.zshrc文件,找到plugins配置,增加zsh-autosuggestions插件。

cd ~/.oh-my-zsh/custom/plugins

git clone git://github.com/zsh-users/zsh-autosuggestions

cd ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions

# 修改 ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE 值
vim zsh-autosuggestions.zsh

ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=10'

# 添加代码
vim ~/.zshrc
source ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

# plugins 后增加zsh-autosuggestions
plugins=(git zsh-autosuggestions)

source ~/.zshrc

指令高亮效果插件zsh-autosuggestions

特殊命令和错误命令,会有高亮显示,可以用过编辑~/.zshrc文件,找到plugins配置,增加zsh-autosuggestions插件。

cd ~/.oh-my-zsh/custom/plugins

# 执行指令将工程克隆到当前目录
git clone git://github.com/zsh-users/zsh-syntax-highlighting.git

# 打开`.zshrc`文件,在最后添加下面内容
vim  ~/.zshrc

# 添加代码
source ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
plugins=(zsh-syntax-highlighting)
# 保存文件

#执行
source ~/.zshrc

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 来自:浙江省杭州市 电信

 

全局环境加载

将shell从bash切换到zsh之后,可能有的环境变量会失效,需要将原来在bash配置文件中的配置转移到zsh配置文件中,这里提供两个方式。

将bash配置文件~/.bash_profile中的环境变量等配置复制到zsh配置文件~/.zshrc中,在zsh配置文件~/.zshrc中添加source ~/.bash_profile


其他快捷命令:
command + t 新建标签
command + w 关闭标签

command + 数字 command + 左右方向键    切换标签
command + enter 切换全屏
command + f 查找
command + d 水平分屏
command + shift + d 垂直分屏
command + option + 方向键 command + [ 或 command + ]    切换屏幕
command + ; 查看历史命令
command + shift + h 查看剪贴板历史
ctrl + u    清除当前行
ctrl + l    清屏
ctrl + a    到行首
ctrl + e    到行尾
ctrl + f/b  前进后退
ctrl + p    上一条命令
ctrl + r    搜索命令历史


No comments:

Post a Comment