在 Mac 上使用 iTerm2 和 Oh My Zsh 打造优雅终端

在完成 SSHGit 的配置之后,我希望能使用一个主题优雅的终端环境。为此,我选择了 iTerm2 作为终端程序,并使用 Oh My Zsh 来管理 Zsh 的配置。

安装 iTerm2

首先安装 iTerm2 ,然后打开应用并按照如下方式配置其 Profile(color Presets):

检查当前 Shell

macOS 默认的 ShellZsh ,可以通过以下命令确认当前使用的 Shell

bash 复制代码
echo $SHELL

输出应为:

bash 复制代码
/bin/zsh

安装 Oh My Zsh

在终端中运行以下命令即可安装 Oh My Zsh

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

配置 Oh My Zsh

打开 ~/.zshrc 文件,将 ZSH_THEME="robbyrussell" 修改为 ZSH_THEME="agnoster"

bash 复制代码
# 设置要加载的主题名称 ------ 如果设为 "random",每次加载 Oh My Zsh 时都会使用一个随机主题。
# 若想知道本次加载的是哪个主题,可运行:echo $RANDOM_THEME
# 参见:https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="agnoster"

安装 Powerline 字体

在终端中运行以下命令以安装 Powerline 字体:

bash 复制代码
# 克隆仓库
git clone https://github.com/powerline/fonts.git --depth=1

# 安装字体
cd fonts
./install.sh

# 清理安装文件
cd ..
rm -rf fonts

接着,在 iTerm2 中设置字体为 Meslo LG M DZ for Powerline

安装 Oh My Zsh 插件

zsh-autosuggestionszsh-syntax-highlighting 插件克隆到 $ZSH_CUSTOM/plugins(默认路径为 ~/.oh-my-zsh/custom/plugins):

bash 复制代码
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

然后在 ~/.zshrc 中启用这些插件:

bash 复制代码
plugins=(
    git
    zsh-autosuggestions
    zsh-syntax-highlighting
)

运行以下命令使配置生效:

bash 复制代码
source ~/.zshrc

完成后,你应该可以看到如下效果:

修复安装 Oh My Zsh 后 brew 无法使用的问题

安装 Oh My Zsh 后,我发现 brew 命令无法正常使用。后来我发现,Oh My Zsh 安装过程中会将原有的 .zshrc 文件内容移动到一个新文件 ~/.zshrc.pre-oh-my-zsh 中。

为了解决这个问题,我在新的 .zshrc 文件中添加了以下代码来加载原始配置:

bash 复制代码
# 加载旧配置
if [ -f "$HOME/.zshrc.pre-oh-my-zsh" ]; then
    source "$HOME/.zshrc.pre-oh-my-zsh"
fi

自定义 macOS 的快捷键

我习惯在 Ubuntu 中使用 Ctrl + Alt + T 快捷键快速打开终端,而 macOS 默认没有这个快捷键。因此,我需要自定义一个相同的快捷键。

自定义步骤如下:

  1. 打开系统设置(System Settings);

  2. 点击「键盘」(Keyboard);

  3. 进入「键盘快捷键」(Keyboard Shortcuts);

  4. 点击「应用快捷键」(App Shortcuts);

  5. 点击「+」按钮添加新快捷键。

我设置的快捷键如下:

设置完成后,就可以像在 Ubuntu 一样快捷地打开终端了。

参考资料

相关推荐
山楂树の3 小时前
模型优化——在MacOS 上使用 Python 脚本批量大幅度精简 GLB 模型(通过 Blender 处理)
python·macos·3d·图形渲染·blender
光头才能变强6 小时前
Mac安装Navicat教程Navicat Premium for Mac v17.1.9 Mac安装navicat【亲测】
macos
Frilled Lizard10 小时前
解决mac下git pull、push需要输入密码
git·macos
是小崔啊11 小时前
Mac下的Homebrew
macos
0wioiw01 天前
Apple基础(Xcode①-项目结构解析)
ide·macos·xcode
xulihang2 天前
命令行调用扫描仪扫描文档
前端·python·命令行
名字不要太长 像我这样就好2 天前
【iOS】weak修饰符
macos·ios·objective-c·cocoa·xcode
ghostwritten2 天前
在 Mac 上用 Vagrant 安装 K8s
macos·kubernetes·vagrant
胡萝卜大王驾到通通闪开2 天前
Mac m系列芯片安装node14版本使用nvm + Rosetta 2
macos
java叶新东老师2 天前
ubuntu资源共享samba 安装与配置 mac/windows共享ubuntu文件资源
windows·ubuntu·macos