## 2\. 常用工具
本節介紹一些常用的,跟開發沒有直接關系的第三方應用及其設置。
### [](https://github.com/macdao/ocds-guide-to-setting-up-mac#homebrew)[Homebrew](http://brew.sh/)
包管理工具,官方稱之為`The missing package manager for OS X`。
安裝步驟見官網。
有了 brew 以后,要下載工具,比如 MySQL、Gradle、Maven、Node.js 等工具,就不需要去網上下載了,只要一行命令就能搞定:
```source-shell
brew install mysql gradle maven node
```
PS:安裝 brew 的時候會自動下載和安裝 Apple 的 Command Line Tools。
brew 的替代品有?[MacPorts](https://www.macports.org/),現在基本沒人用它。
### [](https://github.com/macdao/ocds-guide-to-setting-up-mac#homebrew-cask)[Homebrew Cask](https://caskroom.github.io/)
brew-cask 允許你使用命令行安裝 OS X 應用。比如你可以這樣安裝 Chrome:`brew cask install google-chrome`。還有 Evernote、Skype、Sublime Text、VirtualBox 等都可以用 brew-cask 安裝。
brew-cask 是社區驅動的,如果你發現 brew-cask 上的應用不是最新版本,或者缺少你某個應用,你可以自己提交 pull request。
安裝步驟見官網。
應用也可以通過 App Store 安裝,而且有些應用只能通過 App Store 安裝,比如 Xcode 等一些 Apple 的應用。App Store 沒有對應的命令行工具,還需要 Apple ID。倒是更新起來很方便。
幾乎所有常用的應用都可以通過 brew-cask 安裝,而且是從應用的官網上下載,所以你要安裝新的應用時,建議用 brew-cask 安裝。如果你不知道應用在 brew-cask 中的 ID,可以先用`brew cask search`命令搜索。
### [](https://github.com/macdao/ocds-guide-to-setting-up-mac#iterm2)[iTerm2](https://www.iterm2.com/)
iTerm2 是最常用的終端應用,是 Terminal 應用的替代品。提供了諸如`Split Panes`等[一群實用特性](https://www.iterm2.com/features.html)。它默認的黑色背景讓我毫不猶豫的拋棄了 Terminal。
安裝:
```source-shell
brew cask install iterm2
```
感謝 brew-cask,我們可以通過命令行自動安裝 iTerm2 了。
在終端里,除了可以用`?E`等快捷鍵(詳見[其他快捷鍵](https://github.com/macdao/ocds-guide-to-setting-up-mac#%E5%85%B6%E4%BB%96%E5%BF%AB%E6%8D%B7%E9%94%AE))之外,還可以使用`?B`、`?F`等快捷鍵(具體可以參考[這里](http://ss64.com/bash/syntax-keyboard.html))。前提是這樣設置一下:
選擇`Iterm`菜單 >?`Preferences`?>?`Profiles`,選擇你在使用的 Profile(默認是`Default`),在`Keys`標簽頁中把`Left option (?) key acts as`和`Right option (?) key acts as`都設置成`+ESC`。
在打開新的窗口/標簽頁的時候,默認情況下新窗口總是 HOME 目錄,還需要我每次敲命令才能進入工作目錄。如果想要這個新窗口在打開的時候就自動進入工作目錄,需要如下設置:
選擇`Iterm`菜單 >?`Preferences`?>?`Profiles`,選擇你在使用的 Profile(默認是Default),在`General`標簽頁中的`Working Directory`部分中選擇`Reuse previous seesion's directory`。
至此,Terminal 應用已經出色的完成了其歷史使命。后面命令行就交給 iTerm2 啦。
在 iTerm2 中雙擊會自動選中對應的詞,三擊會選中對應的整行。選中的內容會自動進入剪貼板,不需要再按`?C`復制。
### [](https://github.com/macdao/ocds-guide-to-setting-up-mac#oh-my-zsh)[Oh My Zsh](http://ohmyz.sh/)
默認的 Bash 是黑白的,沒有色彩。而 Oh My Zsh 可以帶你進入彩色時代。Oh My Zsh 同時提供一套插件和工具,可以簡化命令行操作。后面我們會看到很多介紹,你會看到我愛死這家伙了。
安裝方法見官網。
目前我使用的插件有:`git z sublime history rbenv bundler rake`
Oh My Zsh 使用了 Z shell(zsh),一個和 Bash 相似的 Shell,而非 Bash。
在 Z shell 中,`~/.zshrc`是最重要的配置文件。Oh My Zsh 在安裝的時候會把當前環境的`$PATH`寫入`~/.zshrc`中。這并不是我期望的行為,因為使用了 brew,我們基本不再需要去定制`$PATH`,而 Oh My Zsh 提供的默認`$PATH`值`$HOME/bin:/usr/local/bin:$PATH`是非常合適的一個值,它把`$HOME/bin`加入了`$PATH`,可以讓我們把自己用的腳本放到`$HOME/bin`下。
所以建議把`~/.zshrc`重置:
```source-shell
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
```
> [2016年6月17號的一次提交](https://github.com/robbyrussell/oh-my-zsh/commit/551abfcbb48a0c001eadef80abc3276af4e9ad26)后,`zshrc.zsh-template`就不再修改`$PATH`了。請找到`# export PATH=$HOME/bin:/usr/local/bin:$PATH`這一行,把前面的`#`去掉。
Oh My Zsh 還有很多[有價值的插件](https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins-Overview)。
替代品有?[Oh My Fish](https://github.com/oh-my-fish/oh-my-fish)。基于?[Fishshell](http://fishshell.com/)?。
### [](https://github.com/macdao/ocds-guide-to-setting-up-mac#stow)[Stow](http://www.gnu.org/software/stow/)
GNU stow 是管理符號鏈接(symlink)的一個小公舉。主要用于 symlink 你的?[dotfiles](http://dotfiles.github.io/)?如 emacs,git,fish/zsh 的配置文件。安裝只需要
~~~
brew install stow
~~~
安裝了 stow 之后,我們可以開始 symlink 一些 dotfiles 了。完整使用 stow 和 dotfiles 的流程可以參考?[https://github.com/jcouyang/dotfiles](https://github.com/jcouyang/dotfiles)
當你的 dotfiles 都妥妥的 symlink 到?`~/dotfiles`?后,push 到 github 上就再也不怕換電腦了。
### [](https://github.com/macdao/ocds-guide-to-setting-up-mac#git-%E5%B8%B8%E7%94%A8%E5%88%AB%E5%90%8D)Git 常用別名
幾乎每個人都會使用一些方法比如 Git 別名來提高效率,幾乎所有人都會把使用`git st`來代替`git status`。然而這需要手動設置,每個人也都不完全一樣。
Oh My Zsh 提供了一套系統別名(alias),來達到相同的功能。比如`gst`作為`git status`的別名。而且 Git 插件是 Oh My Zsh 默認啟用的,相當于你使用了 Oh My Zsh,你就擁有了一套高效率的別名,而且還是全球通用的。是不是棒棒噠?下面是一些我常用的別名:
| Alias | Command |
| --- | --- |
| gapa | `git add --patch` |
| gc! | `git commit -v --amend` |
| gcl | `git clone --recursive` |
| gclean | `git reset --hard && git clean -dfx` |
| gcm | `git checkout master` |
| gcmsg | `git commit -m` |
| gco | `git checkout` |
| gd | `git diff` |
| gdca | `git diff --cached` |
| glola | `git log --graph --pretty = format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --all` |
| gp | `git push` |
| grbc | `git rebase --continue` |
| gst | `git status` |
| gup | `git pull --rebase` |
| gwip | `git add -A; git rm $(git ls-files --deleted) 2> /dev/null; git commit -m "--wip--"` |
完整列表請參考:[https://github.com/robbyrussell/oh-my-zsh/wiki/Plugin:git](https://github.com/robbyrussell/oh-my-zsh/wiki/Plugin:git)
### [](https://github.com/macdao/ocds-guide-to-setting-up-mac#scroll-reverser)Scroll Reverser
當你在瀏覽一個很長的網頁時,你看完了當前顯示的內容,想要看后續的內容,你可以在 Trackpad 上雙指上滑,或者鼠標滾輪向上滾動。這是被稱作“自然”的滾動方向。
然而在 Windows 里鼠標滾動的行為是相反的:鼠標滾輪向下滾動才會讓瀏覽器顯示后續的內容,向上滾動會達到頁面的頂部。你可以在 OS X 的系統偏好設置里修改(選擇`System Preferences`?>?`Trackpad`,在`Scroll & Zoom`標簽頁中不選中`Scroll direction: natural`),但是這樣會同時改變鼠標滾輪的方向和 Trackpad 的方向。
要想只改變鼠標滾輪的方向,而保持 Trackpad 依舊是“自然”的,我們需要 Scroll Reverser:
```source-shell
brew cask install scroll-reverser
```
PS:這貨會讓三指點擊失效
### [](https://github.com/macdao/ocds-guide-to-setting-up-mac#shiftit)ShiftIt
原生 OS X 下只能手動調整窗口大小,所以我們需要窗口管理工具。我用過很多窗口管理工具,可惜大部分工具都存在快捷鍵沖突的問題(對我來說主要是 IntelliJ IDEA)。ShiftIt 是少見的沒有沖突的窗口管理工具:
```source-shell
brew cask install shiftit
```
PS:ShiftIt的舊版本需要安裝 X11,最新版本已經修正了這個問題。
替代者有 SizeUp,主要快捷鍵和 ShiftIt 相同。
當然如果喜歡 hacking,[Slate](https://github.com/jigish/slate)?是個不錯的 hackable 的窗口管理工具。配置可以參照?[http://thume.ca/howto/2012/11/19/using-slate/](http://thume.ca/howto/2012/11/19/using-slate/)
### [](https://github.com/macdao/ocds-guide-to-setting-up-mac#sublime-text-2)Sublime Text 2
安裝:
```source-shell
brew cask install sublime-text
```
在命令行中指定使用 Sublime Text 打開某文件,是一個非常常用的功能,一般我們會按照?[OS X Command Line](https://www.sublimetext.com/docs/2/osx_command_line.html)?中所說執行?`ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/bin/subl`?來增加`subl`鏈接。但是如果你用 brew-cask 安裝的話,恭喜你,你不需要運行這個命令,因為 brew-cask 自動幫你做了這件事情。而且你卸載 Sublime Text 的時候 brew-cask 會自動刪掉這個鏈接。
同時 Oh My Zsh 也提供了 Sublime Text 插件,叫做`sublime`。參考:[https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins/sublime](https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins/sublime),這個插件和通過 brew-cask 安裝的 Sublime Text 完美兼容。
替代品有 Atom、TextMate、Sublime Text 3 等,跟 Sublime Text 2 一樣,用 brew-cask 安裝的話命令行工具會被自動加入`$PATH`。
### [](https://github.com/macdao/ocds-guide-to-setting-up-mac#macdown)MacDown
MacDown 是 Markdown 編輯器。由于 Mou 一直不支持代碼高亮,我就轉向了 MacDown。完美支持?[GFM](https://help.github.com/articles/github-flavored-markdown/)。
我特別喜歡?[Markdown](https://daringfireball.net/projects/markdown/),我用 Makdown 來寫文章(包括本文),寫幻燈片([reveal.js](https://github.com/hakimel/reveal.js/))。Markdown 可以讓我專注于內容本身,而無需花精力在排版和樣式上。
安裝:
```source-shell
brew cask install macdown
```
### [](https://github.com/macdao/ocds-guide-to-setting-up-mac#z)z
在打開終端后,你是怎么進入項目的工作目錄?是`cd xxx`,`?R`還是用別名?
[z](https://github.com/rupa/z)?工具可以幫你快速進入目錄。比如在我的 Mac 上運行`z cask`就會進入`/usr/local/Library/Taps/caskroom/homebrew-cask/Casks`目錄。
這貨的安裝非常方便,甚至都不需要下載任何東西,因為它已經整合在了 Oh My Zsh 中。編輯`~/.zshrc`文件,在`plugins=(git)`這行中加上`z`變成`plugins=(git z)`,然后運行`source ~/.zshrc`重新加載配置文件,就可以使用 z 了。
替代品有 autojump。autojump 需要使用 brew 安裝。
### [](https://github.com/macdao/ocds-guide-to-setting-up-mac#vimium)[Vimium](https://vimium.github.io/)
Vimium 是一個 Google Chrome 擴展,讓你可以純鍵盤操作 Chrome,把你的 Chrome 變成“黑客的瀏覽器”。
安裝方法請參考官方網站。
其他瀏覽器也有類似的工具,比如 FireFox 的?[KeySnail](https://github.com/mooz/keysnail)。
### [](https://github.com/macdao/ocds-guide-to-setting-up-mac#lastpass)[LastPass](https://lastpass.com/)
LastPass 是管理密碼的工具,支持二次驗證,提供所有瀏覽器插件以及 Mac 桌面版本。
最重要的是,它提供?命令行?的版本,可以直接通過 brew 安裝
```source-shell
brew install lastpass-cli --with-pinentry
```
之后,只需要登陸:
```source-shell
lpass login you@email.com
```
就可以拷貝密碼或者集成到其他命令中了:
```source-shell
lpass show --password gmail.com -c
```
### [](https://github.com/macdao/ocds-guide-to-setting-up-mac#sourcetree)[SourceTree](https://www.sourcetreeapp.com/)
SourceTree 是 Atlassian 公司出品的一款優秀的 Git 圖形化客戶端。如果你發現命令行無法滿足你的要求,可以試試 SourceTree。
安裝:
```source-shell
brew cask install sourcetree
```
用 brew-cask 安裝會自動增加命令行工具`stree`到`$PATH`里。在命令行中輸入`stree`可以快速用 SourceTree 打開當前 Git 倉庫。詳細用法請參見`stree --help`。
### [](https://github.com/macdao/ocds-guide-to-setting-up-mac#cheatsheet)[CheatSheet](http://www.mediaatelier.com/CheatSheet/)
CheatSheet 能夠顯示當前程序的快捷鍵列表,默認的快捷鍵是長按`?`。
[](https://camo.githubusercontent.com/e92cfabcadbf461b7af191f57ace06b5f9f73c68/687474703a2f2f7777772e6d656469616174656c6965722e636f6d2f436865617453686565742f696d67732f6d61696e2e706e67)
安裝:
```source-shell
brew cask install cheatsheet
```
### [](https://github.com/macdao/ocds-guide-to-setting-up-mac#alfred)[Alfred](https://www.alfredapp.com/)
Mac 用戶不用鼠標鍵盤的必備神器,配合大量 Workflows,習慣之后可以大大減少操作時間。
上手簡單,調教成本在后期自定義 Workflows,不過有大量雷鋒使用者提供的現成擴展,訪問[這里](http://www.alfredworkflow.com/)挑選喜歡的,并可以極其簡單地根據自己的需要修改。
安裝:
```source-shell
brew cask install alfred
```