Mac终端工具配置指南
Table of Contents
前言
《程序员修炼之道:从小工到专家》是我经常拿出来阅读的书籍,这本书涵盖个人责任、职 业发展、编程经验以及开发之道等内容。该书令我感悟颇深的是关于工具的选择与积累,纵 观人类的发展的历史,其实就是工具的进化史,从石器时代到青铜时代再到到铁器时,朝代 更替,都是工具的发展以及积累产生的革命性的变化。而近现代由于科技和技术的发明,几 次工业革命也是彻底改变整个人类的生活方式。
我们生活的信息时代,已经创造许多工具,就软件开发而言,也已经积累大量优秀的工具。 因此决定开发人员的效率,除了个人智商和知识之外,你的工具箱有些什么工具也决定了你 的思考方式和工具效率,工具也并非以数量取胜,而在于工具的质量以及多样性,更在于持 续使用和打磨工具,到达得心应手的程度。正所谓磨刀不误砍柴工,从现在起开始积累丰富 你的工具箱,你会从其中持续获得收益。本文主要讨论如何在Mac上安装、配置iterm2、zsh、 tmux以及emacs,打造高效开发环境。
安装
Homebrew
Homebrew是Mac上最常用的包管理工具,能够方便的安装各种软件,brew使用ruby些的。打 开Mac的终端执行下面的命令进行安装。
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Iterm2
Iterm2是Mac下非常优秀的终端模拟器,比系统自带的Terminal具有更好的扩展性,包括无 鼠标复制粘贴,快捷键定义和修改,字体(可以定制非ASCII字符的字体)和Emoji显示都非常 好,同时也支持更多的主题配色。执行下面的brew命令进行安装。
# 获取iterm2的信息 brew cask info iterm2 # iterm2: 3.2.9 (auto_updates) # https://www.iterm2.com/ # Not installed # From: https://github.com/Homebrew/homebrew-cask/blob/master/Casks/iterm2.rb # ==> Name # iTerm2 # ==> Artifacts # iTerm.app (App) # 执行安装 brew cask install iterm2
Zsh
Zsh是unix环境下的shell程序,它对bash进行了扩展,并且借鉴了许多bash,ksh,tcsh的 功能,它有如下特性:
- 可帮助用户键入常用命令选项及参数的可编程命令行补全功能,自带对数百条命令的支持
- 可与任意Shell共享命令历史
- 可在无需运行外部程序(如find)的情况下通过 文件扩展匹配文件
- 改进变量/数组处理方式
- 在单缓冲区内编辑多行命令
- 拼写检查
- 多种兼容模式(例如,Zsh可在运行为/bin/sh的情况下伪装成Bourne shell)
- 可编程的命令行界面,包括将提示行信息显示在屏幕右侧并在输入过长指令时自动隐藏的功能
- 可加载模块可提供额外支持:完整传输控制协议、Unix域套接字控制、FTP客户端及扩展数学函数。
- 自带where命令,其与which命令类似,但是显示指定于$PATH中所指定指令的全部位置,而不是仅显示所使用指令的位置。
- 目录名称。此功能可让用户设置快捷方式,(如~mydir,与~及~user的工作方式相似)。
brew install zsh
Tmux
Tmux是非常好用的终端复用工具,能够在单个screen里面创建、访问和管理多个终端,同时 也可以脱离screen在后台运行。执行下面的命令查看和安装tmux:
brew info tmux # tmux: stable 2.9 (bottled), HEAD # Terminal multiplexer # https://tmux.github.io/ # /usr/local/Cellar/tmux/2.8 (10 files, 709.5KB) * # Poured from bottle on 2019-03-27 at 23:15:10 # From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/tmux.rb # ==> Dependencies # Build: pkg-config ✔ # Required: libevent ✔, ncurses ✔ # ==> Options # --HEAD # Install HEAD version # ==> Caveats # Example configuration has been installed to: # /usr/local/opt/tmux/share/tmux # Bash completion has been installed to: # /usr/local/etc/bash_completion.d # ==> Analytics # install: 70,198 (30 days), 118,149 (90 days), 385,484 (365 days) # install_on_request: 59,955 (30 days), 103,387 (90 days), 332,667 (365 days) # build_error: 0 (30 days) brew install tmux
Emacs
Emacs最初由Richard Stallman于1975年在MIT协同盖伊·史提尔二世共同完成,是基于Emacs Lisp上的强大编辑器,通常和VI编辑器进行比较。Emacs Lisp是Lisp的方言之一,受到 MacLisp和Common Lisp语言的影响,Emacs编辑器可以用Emacs Lisp进行扩展,其实在Emacs 上执行的每条编辑命令都是Emacs Lisp的函数,Lisp语言本身的强大特性决定了Emacs的内 在优秀基因🧬。随着Emacs将近40多年的演进和发展,积累大量的编辑插件,能够执行各种 编辑任务,其中以Org Mode最为突出,Org Mode可以做日程管理、代办事项、GTD、博客等 许多任务,本文就是在Org Mode下编写然后生成静态站点,托管到Github Pages上。编辑器 是开发主要的工具,我的建议是选择一个合适的编辑器,将它用于80%以上的编辑任务,持 续学习、打磨编辑器,至于选择什么编辑器可由个人喜好决定,比较合适的选择包括 Vim,Vscode,Sublime Text等。
brew cask info emacs # emacs: 26.2 # https://emacsformacosx.com/ # Not installed # From: https://github.com/Homebrew/homebrew-cask/blob/master/Casks/emacs.rb # ==> Name # Emacs # ==> Artifacts # Emacs.app (App) # /Applications/Emacs.app/Contents/MacOS/Emacs -> emacs (Binary) # /Applications/Emacs.app/Contents/MacOS/bin/ebrowse (Binary) # /Applications/Emacs.app/Contents/MacOS/bin/emacsclient (Binary) # /Applications/Emacs.app/Contents/MacOS/bin/etags (Binary) brew cask install emacs
配置
Iterm2
主题
克隆Iterm2的主题库,超过200+多个主题可供选择,安装方法如下:
- brew安装最新的iterm2软件
- 输入CMD+i(⌘+i)
- 找到Color选项卡
- 点击Color Presets
点击import,导入喜欢的主题
git clone https://github.com/mbadolato/iTerm2-Color-Schemes.git
修改按键
为了便于在iterm2中使用emacs,我修改right command键为ctrl键,因为emacs的许多按键 操作都是ctrl开始的。

有touchbar的Mac可以对功能键显示进行修改,默认需要Fn+功能键进行操作,但是编程环境 下我们通常需要经常使用功能键,需要对默认行为进行修改。

Zsh
配置文件
Oh My Zsh是开源的社区驱动的zsh配置文件项目,能够方便快捷的配置zsh,实现开箱即用 的终端环境;
安装方式:
插件配置
oy-my-zsh支持插件,能够在命令行下更好的显示相关信息,比如git的信息,在~/.zshrc中 可以修改插件。
plugins=( git bundler dotenv osx rake rbenv ruby )
主题配置
绝大部分主题都需要Powerline字体,因此先安装字体:
cd ~ # clone git clone https://github.com/powerline/fonts.git --depth=1 # install cd fonts ./install.sh # clean-up a bit cd .. rm -rf fonts
安装Powerlevel9K主题:

直接在~/.zshrc中修改主题:
ZSH_THEME="powerlevel9k/powerlevel9k" POWERLEVEL9K_PROMPT_ON_NEWLINE=true POWERLEVEL9K_RPROMPT_ON_NEWLINE=true # 可以添加更多的插件 POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir vcs) POWERLEVEL9K_LEFT_SEGMENT_SEPARATOR=$'\uE0B1' POWERLEVEL9K_RIGHT_SEGMENT_SEPARATOR=$'\uE0B3' # POWERLEVEL9K_COLOR_SCHEME='light' POWERLEVEL9K_MULTILINE_FIRST_PROMPT_PREFIX="↱" POWERLEVEL9K_MULTILINE_LAST_PROMPT_PREFIX="↳ " POWERLEVEL9K_TIME_FOREGROUND='red' POWERLEVEL9K_TIME_BACKGROUND='blue'
Powerlevel9k插件
- backgroundjobs - Indicator for background jobs.
- battery - Current battery status.
- context - Your username and host, conditionalized based on $USER and SSH status.
- date - System date.
- dir - Your current working directory.
- dirwritable - Displays a lock icon, if you do not have write permissions on the current folder.
- diskusage - Disk usage of your current partition.
- history - The command number for the current line.
- host - Your current host name
- ip - Shows the current IP address.
- vpnip - Shows the current VPN IP address.
- publicip - Shows your public IP address.
- load - Your machine's load averages.
- osicon - Display a nice little icon, depending on your operating system.
- ram - Show free RAM.
- rootindicator - An indicator if the user has superuser status.
- status - The return code of the previous command.
- swap - Prints the current swap size.
- time - System time.
- user - Your current username
- vimode- Your prompt's Vi editing mode (NORMAL|INSERT).
- ssh - Indicates whether or not you are in an SSH session.
Tmux
Oy My Tmux是漂亮、灵活的配置文件,基本实现开箱即用,非常强大,后期只需要修改 prefix按键即可。

配置要求
- Tmux版本大于2.1, 运行在Linux, Mac, OpenBSD, Cygwin中;
- 环境变量$TERM必须设置为xterm-256color
配置安装
cd ~
git clone https://github.com/gpakosz/.tmux.git
ln -s -f .tmux/.tmux.conf
cp .tmux/.tmux.conf.local .
Emacs
Emacs的配置参见我写的另一篇文章《从零开始配置emacs编辑器》。
指南
Tmux快捷键指南

Emacs快捷键指南
