工欲善其事,必先利其器git
今天窗外雨儿淅沥,本叔内心异常平静,献上一篇如何打造咱们 Mac 上好用的终端纪实。落霞与孤鹜齐飞,实用与装X并济!程序员
注:如下的步骤我都亲身实践过,环境 macOS Sierra 10.12,我相信不管比我高或者是低版本的系统都可以正确执行,请你们放心。github
最终效果图: shell
iTerm2 是网上你们公认的在 Mac 上最好用的终端工具,比系统自带的 Terminal 智能和强大,详细的操做大法等大叔积累够了再出一篇文章。终端工具通俗理解就是 Windows 下的 CMD。(Emmmmm...没用过的同窗就算了)vim
而 「Oh My Zsh」 就厉害了,不过仍是要说一下 Shell。windows
咱们都知道 Mac 的操做系统是基于 Linux/Unix 进行开发的,其实 Windows 也是,人家一开始不认可,最后面不得已才说借鉴了一部分 Linux/Unix,呵呵。言归正传,Shell是Linux/Unix的一个外壳,你理解成衣服也行。它负责外界与Linux内核的交互,接收用户或其余应用程序的命令,而后把这些命令转化成内核能理解的语言,传给内核,内核是真正干活的,干完以后再把结果返回用户或应用程序。安全
Linux/Unix提供了不少种Shell,为毛要这么多Shell?其实就是那些大牛程序员看到不爽的Shell,就会本身从新写一套,慢慢造成了一些标准,经常使用的Shell有这么几种,sh、bash、csh等,想知道你的系统有几种shell,能够经过如下命令查看:bash
$ cat /etc/shells
复制代码
显示以下:微信
/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
复制代码
这里 Mac 会比 Linux 多预装了一份 zsh。目前经常使用的 Linux 系统和 OS X 系统的默认 Shell 都是 bash,可是真正强大的 Shell 是深藏不露的 zsh。但因为复杂的配置,致使很长一段时间都无人问津,直到有一天,国外有个穷极无聊的程序员开发出了一个可以让你快速上手的 zsh 项目,叫作「oh my zsh」,Github网址:github.com/robbyrussel…session
官方下载网址:www.iterm2.com/
下载好安装就好了,没有什么特别须要注意的。
注意:如下所涉及到的命令,若没有特殊声明,都是在 iTerm2 上运行的。
打开 iTerm2,并在该终端上执行如下命令:
curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh
复制代码
这句命令意思就是下载该脚本并执行
Powerline is a statusline plugin for vim, and provides statuslines and prompts for several other applications, including zsh, bash, tmux, IPython, Awesome, i3 and Qtile.
说白了 Powerline 就是用 Python 写的能够美化终端输出的脚本,并且支持多种终端。
首先查看本机是否有安装 pip:
$ pip --version
复制代码
若是没有成功输出版本信息的话,那就先安装 pip:
$ sudo easy_install pip
复制代码
若是已经确认本机有安装的话,那就来正式安装 Powerline:
$ pip install powerline-status
复制代码
若是在这步提示由于权限不足没法成功安装的话(通常是 macOS Sierra 以上的用户),是由于苹果官方的 SIP 安全保护策略,如下步骤能够暂时关闭,等咱们安装好再从新开启,能够放心操做。
关闭 SIP 操做:
这样就将 SIP 关闭了,重启电脑后从新在 iTerm2 上执行安装 powerline 的命令
安装完后,能够出于安全性考虑,咱们仍是把 SIP 启动,以保护咱们的电脑吧!
打开 SIP 操做:
字体库的 Github 仓库地址:github.com/powerline/f…
Step 1: 将仓库 git clone 下来,而后进入到 install.sh
脚本文件所在的目录;
Step 2: 执行命令安装字体库:
$ ./install.sh
复制代码
安装完成后,会提示全部字体安装到了 ~/Library/Fonts
文件夹下
以下图所示,把 iTerm2 的设置里的 Profile 中的 Text 选项卡中里的 Regular Font
和 Non-ASCII Font
的字体都设置成 Powerline 的字体。(能够注意到,Powerline 字体名称后面都有加 powerline
字样,很好区分)
配色方案的 Github 仓库地址:github.com/altercation…
Step 1: 将仓库 git clone 下来,而后进入到文件夹;
Step 2: 双击 Solarized Dark.itermcolors
和 Solarized Light.itermcolors
两个文件就能够把配置文件导入到 iTerm2 的配色方案列表里;
Step 3: 设置 iTerm2 的配色方案;
主题有不少种,这里选择了 agnoster 主题,你们熟悉操做以后,能够本身寻找主题并安装
agnoster 主题的 Github 仓库:github.com/fcamblor/oh…
Step 1: 将仓库 git clone 下来,而后进入到文件夹;
Step 2: iTerm2 上运行 install 文件,主题将安装到 ~/.oh-my-zsh/themes
目录下
Step 3: 为 zsh 设置该主题
$ vim ~/.zshrc
# 找到 ZSH_THEME 字段,并修改为:ZSH_THEME="agnoster"
复制代码
使用该工具的做用是当咱们在终端上输入正确或成功执行的命令时,会绿色提示;相反不成功或错误命令时,会有红色警告提示。
zsh-syntax-highlighting 的 Github 仓库地址:github.com/zsh-users/z…
Step 1: 进入 ~/.oh-my-zsh/custom/plugins/
目录;
Step 2: 将仓库 git clone 到当前目录;
Step 3: 在 .zshrc
文件中追加以下内容
plugins=(zsh-syntax-highlighting)
source ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
复制代码
Step 4: 最后重载配置文件
$ source ~/.zshrc
复制代码
这样就同时配置好了zsh和oh-my-zsh。
至此,全部配置操做已经结束。能够酷炫地使用属于你的 iTerm2 了!
有时候咱们想在 Finder 的当前目录下打开 iTerm2,以免在 iTerm2 手动进入相应路径,网上有介绍使用 Go2Shell 插件的用法,在这里我推荐使用 Mac 自带的 Automator 建立 AppleScript 以执行完成咱们的需求。
在 Launchpad 中打开 Automator:
选择 文件
-> 新建
-> 服务
:
选择 AppleScript 并粘贴进脚本代码:
代码以下:
on run {input, parameters}
tell application "Finder"
set pathList to (quoted form of POSIX path of (folder of the front window as alias))
set command to "clear; cd " & pathList
end tell
tell application "System Events"
-- some versions might identify as "iTerm2" instead of "iTerm"
set isRunning to (exists (processes where name is "iTerm")) or (exists (processes where name is "iTerm2"))
end tell
tell application "iTerm"
activate
set hasNoWindows to ((count of windows) is 0)
if isRunning and hasNoWindows then
create window with default profile
end if
select first window
tell the first window
if isRunning and hasNoWindows is false then
create tab with default profile
end if
tell current session to write text command
end tell
end tell
end run
复制代码
粘贴完后,按 command+s 以保存,在弹出的提示框内输入该服务的名字,自定义就好。
接下来设置一下直接调用该服务的键盘快键键:
完成!
$ chsh -s /bin/zsh
$ chsh -s /bin/bash
Preference-General-Closing
栏目,将 Confirm "Quit iTerm2(⌘Q)"
command选项勾选去掉就行~/.oh-my-zsh/themes
路径下找到 agnoster.zsh-theme
文件,可以使用文本工具打开,将里面的 build_prompt
下的 prompt_context
字段在前面加#注释掉便可。工做和兴趣上努力让本身变得酷一点,生活上努力让本身变得更温暖一点。
另外,很感谢你阅读完了本文,我相信你不同凡响。