Mac 小記 — iTerm二、Zsh、Homebrew

前言

寫完 「Ubuntu 自動化配置」 這篇文章後,每次連服務器心情指數都上升好幾個百分點,因而想着應該將 macOs 的開發環境也梳理梳理,應該會對開發效率有所增益。
html

1. iTerm2 配置

雖然Mac默認的終端已經很是強大,但從功能的豐富程度和開發效率上來看,對於我來講iterm2無疑是更優選擇。安裝完畢後第一步就是開啓 Hotkey-Window(Preferences→Keys→Create a Dedicated Hotkey Window),我習慣給其錄製的快捷鍵是:Option+Space,今後終端召之即來揮之即去,這能給予我一切盡在掌握之中的知足感。linux

1.1 偏好調整

如下三點我認爲還需加以調整才能愉快玩耍:git

Ⅰ. 配色問題

iterm2 默認的配色對藍色不是很友好,特別是半透明的狀況下很難看清楚,我通常將其顏色值改成:268bd2,前景色直接設爲白色,更改先後對比:
github

Ⅱ. 快速跳過單詞問題

⌥ + ↔ 本是向前或者向後跳過一個單詞,可是卻變成了這種字符 [D[C,修復方法:在每一個窗口設置的 keys 中更改相應的快捷鍵,以下圖:
shell

Ⅲ. 新建標籤頁問題

⌘ + T 爲新建標籤頁,可是在 Hotkey-Window 中我須要新建同類別的標籤頁而不是默認,所以須要明確指定其快捷鍵:
vim

1.2 經常使用功能簡述

iterm2 功能豐富,然而不少是錦上添花,與其去記複雜的快捷鍵還不如多操做幾步,我認爲有必要去記的也就這麼幾個:ruby

  • ⌘+N⌘+T⌘+↔⌘+F⌘+W 這幾個快捷鍵就不解釋了。
  • ⌘+D 垂直分屏、⌘+⇧+D 水平分屏、⌘+⌥+↑↓↔ 切換分屏。
  • ⌘+↩全屏、⌘ + R 清屏。
  • ⌃+A/E 行首/尾、⌃+R 查詢歷史命令。
  • ⌥+↔ 左右跳過單詞。

記住這些就差很少了,腦容量有限,應能省則省。bash

2. zsh 配置

使用 zsh 已經很長時間了,上篇文章 整理了一些 linux 中 bash 的配置,本想擴展一下使其知足 macOs 中更豐富的應用場景。但經歷一番從開始到放棄以後,我決定從新整理一下 zsh 的配置。服務器

2.1 oh-my-zsh

查看當前使用的 shell:echo $SHELL,切換 zsh:chsh -s /bin/zsh。託開源社區的福,平凡和華麗之間只有一行代碼的距離:

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

2.2 別名

zsh 的配置文件是 .zshrc,從名字上就能看的出來,默認已經設置了好多別名,都還蠻有用的:

-='cd -'
...=../..
....=../../..
.....=../../../..
......=../../../../..
1='cd -'
2='cd -2'
3='cd -3'
4='cd -4'
5='cd -5'
6='cd -6'
7='cd -7'
8='cd -8'
9='cd -9'
_=sudo
afind='ack -il'
d='dirs -v | head -10'
g=git
ga='git add'
gaa='git add --all'
gap='git apply'
gapa='git add --patch'
gau='git add --update'
gb='git branch'
gba='git branch -a'
gbd='git branch -d'
gbda='git branch --no-color --merged | command grep -vE "^(\*|\s*(master|develop|dev)\s*$)" | command xargs -n 1 git branch -d'
gbl='git blame -b -w'
gbnm='git branch --no-merged'
gbr='git branch --remote'
gbs='git bisect'
gbsb='git bisect bad'
gbsg='git bisect good'
gbsr='git bisect reset'
gbss='git bisect start'
gc='git commit -v'
'gc!'='git commit -v --amend'
gca='git commit -v -a'
'gca!'='git commit -v -a --amend'
gcam='git commit -a -m'
'gcan!'='git commit -v -a --no-edit --amend'
'gcans!'='git commit -v -a -s --no-edit --amend'
gcb='git checkout -b'
gcd='git checkout develop'
gcf='git config --list'
gcl='git clone --recursive'
gclean='git clean -fd'
gcm='git checkout master'
gcmsg='git commit -m'
'gcn!'='git commit -v --no-edit --amend'
gco='git checkout'
gcount='git shortlog -sn'
gcp='git cherry-pick'
gcpa='git cherry-pick --abort'
gcpc='git cherry-pick --continue'
gcs='git commit -S'
gcsm='git commit -s -m'
gd='git diff'
gdca='git diff --cached'
gdct='git describe --tags `git rev-list --tags --max-count=1`'
gdcw='git diff --cached --word-diff'
gdt='git diff-tree --no-commit-id --name-only -r'
gdw='git diff --word-diff'
gf='git fetch'
gfa='git fetch --all --prune'
gfo='git fetch origin'
gg='git gui citool'
gga='git gui citool --amend'
ggpull='git pull origin $(git_current_branch)'
ggpur=ggu
ggpush='git push origin $(git_current_branch)'
ggsup='git branch --set-upstream-to=origin/$(git_current_branch)'
ghh='git help'
gignore='git update-index --assume-unchanged'
gignored='git ls-files -v | grep "^[[:lower:]]"'
git-svn-dcommit-push='git svn dcommit && git push github master:svntrunk'
gk='\gitk --all --branches'
gke='\gitk --all $(git log -g --pretty=%h)'
gl='git pull'
glg='git log --stat'
glgg='git log --graph'
glgga='git log --graph --decorate --all'
glgm='git log --graph --max-count=10'
glgp='git log --stat -p'
glo='git log --oneline --decorate'
globurl='noglob urlglobber '
glog='git log --oneline --decorate --graph'
gloga='git log --oneline --decorate --graph --all'
glol='git log --graph --pretty='\''%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'\'' --abbrev-commit'
glola='git log --graph --pretty='\''%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'\'' --abbrev-commit --all'
glp=_git_log_prettily
glum='git pull upstream master'
gm='git merge'
gma='git merge --abort'
gmom='git merge origin/master'
gmt='git mergetool --no-prompt'
gmtvim='git mergetool --no-prompt --tool=vimdiff'
gmum='git merge upstream/master'
gp='git push'
gpd='git push --dry-run'
gpoat='git push origin --all && git push origin --tags'
gpristine='git reset --hard && git clean -dfx'
gpsup='git push --set-upstream origin $(git_current_branch)'
gpu='git push upstream'
gpv='git push -v'
gr='git remote'
gra='git remote add'
grb='git rebase'
grba='git rebase --abort'
grbc='git rebase --continue'
grbi='git rebase -i'
grbm='git rebase master'
grbs='git rebase --skip'
grep='grep  --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn}'
grh='git reset HEAD'
grhh='git reset HEAD --hard'
grmv='git remote rename'
grrm='git remote remove'
grset='git remote set-url'
grt='cd $(git rev-parse --show-toplevel || echo ".")'
gru='git reset --'
grup='git remote update'
grv='git remote -v'
gsb='git status -sb'
gsd='git svn dcommit'
gsi='git submodule init'
gsps='git show --pretty=short --show-signature'
gsr='git svn rebase'
gss='git status -s'
gst='git status'
gsta='git stash save'
gstaa='git stash apply'
gstc='git stash clear'
gstd='git stash drop'
gstl='git stash list'
gstp='git stash pop'
gsts='git stash show --text'
gsu='git submodule update'
gts='git tag -s'
gtv='git tag | sort -V'
gunignore='git update-index --no-assume-unchanged'
gunwip='git log -n 1 | grep -q -c "\-\-wip\-\-" && git reset HEAD~1'
gup='git pull --rebase'
gupv='git pull --rebase -v'
gwch='git whatchanged -p --abbrev-commit --pretty=medium'
gwip='git add -A; git rm $(git ls-files --deleted) 2> /dev/null; git commit --no-verify -m "--wip-- [skip ci]"'
history='fc -l 1'
l='ls -lah'
la='ls -lAh'
ll='ls -lh'
ls='ls -G'
lsa='ls -lah'
md='mkdir -p'
please=sudo
po=popd
pu=pushd
rd=rmdir
run-help=man
which-command=whence

我常常記不住打包和壓縮的命令,所以增長如下別名:

alias t="tar -cvf"
alias gz="tar -czvf"
alias bz2="tar -cjvf"

2.3 插件

大頭來了,列舉一些經常使用的:

2.3.1 git

git 是惟一默認啓用的插件,通用配置在上篇文章中已經詳細說明。在 macOs 中建議在用戶目錄中增長全局的忽略文件 .gitignoreglobal 用於忽略 macOS 文件夾屬性文件 .DS_Store,同時執行 git config --global core.excludesfile ~/.gitignoreglobal。多帳號自動切換用戶信息的代碼建議添加到 ~/.oh-my-zsh/lib/git.zsh 中的 git_prompt_info 函數:

function git_prompt_info() {
  local ref
  if [[ "$(command git config --get oh-my-zsh.hide-status 2>/dev/null)" != "1" ]]; then
    ref=$(command git symbolic-ref HEAD 2> /dev/null) || \
    ref=$(command git rev-parse --short HEAD 2> /dev/null) || return 0
    echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX"
    
    # switch user
    if [ -z `git config user.name` ] && [ -z `git config user.email` ]; then
        local git_remote=`git remote -v`
        if [[ $git_remote =~ "github" ]]; then
            `git config user.name "github" && git config user.email "github@youclk.com"`
        elif [[ $git_remote =~ "gitee" ]]; then
            `git config user.name "gitee" && git config user.email "gitee@youclk.com"`
        fi
    fi
    
  fi
}

2.3.2 autojump

當你的工做目錄比較複雜的時候,這個插件的價值就體現出來了。它會讀取歷史記錄,以後只要j dir就能夠愉快地跳轉。安裝也及其簡單:brew install autojump,而後編輯 .zshrc 文件,在插件選項中添加 autojump 便可。

2.3.3 zsh-autosuggestions

我對它能夠說是一見傾心,其實這個插件對於生產效率的提高並不明顯,但耐不住看着舒服呀:

安裝:git clone git://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions,後續步驟同上。

2.3.4 extract

正如我記不住打包和壓縮的命令,解壓等命令也是如此,由於實在是不大經常使用。以前也是靠別名,直到發現這個插件統一瞭解壓命令:x

3. Homebrew 配置

它毫無疑問是 macOS 最流行的包管理器,安裝就一行代碼:/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)",使用也沒啥好說的:

brew install package
brew uninstall package

# 下載第三方應用
brew cask install qq
brew cask uninstall qq

有必要記錄的是更換鏡像源,我選擇的是中科大鏡像

# 替換 brew.git:
# https://github.com/Homebrew/brew
cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git

# 替換 homebrew-core.git:
# https://github.com/Homebrew/homebrew-core
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

# 設置 Homebrew Bottles環境變量
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

結語

以上三個軟件是我認爲拿到 mac 起就必須安裝配置的,不管這臺 mac 將被用於什麼業務,這是愉快玩耍 macOs 的基礎。後續如有空我會將我我的的 macOs 開發環境作一個概括,敬請期待。


個人公衆號《有刻》,咱們共同成長!

相關文章
相關標籤/搜索