前言
写完 "Ubuntu 自动化配置" http://www.cnblogs.com/youclk/p/7876035.html 这篇文章后, 每次连服务器心情指数都上升好几个百分点, 于是想着应该将 macOs 的开发环境也梳理梳理, 应该会对开发效率有所增益.
1. iTerm2 配置
虽然 Mac 默认的终端已经非常强大, 但从功能的丰富程度和开发效率上来看, 对于我来说 iTerm2 无疑是更优选择. 安装完毕后第一步就是开启 Hotkey-Windows(Preferences→Keys→Create a Dedicated Hotkey Windows), 我习惯给其录制的快捷键是: Option+Space, 从此终端召之即来挥之即去, 这能给予我一切尽在掌握之中的满足感.
1.1 偏好调整
以下三点我认为还需加以调整才能愉快玩耍:
Ⅰ. 配色问题
iTerm2 默认的配色对蓝色不是很友好, 特别是半透明的情况下很难看清楚, 我一般将其颜色值改为: 268bd2, 前景色直接设为白色, 更改前后对比:
Ⅱ. 快速跳过单词问题
+ ↔ 本是向前或者向后跳过一个单词, 但是却变成了这种字符 [D[C, 修复方法: 在每个窗口设置的 keys 中更改相应的快捷键, 如下图:
Ⅲ. 新建标签页问题
+ T 为新建标签页, 但是在 Hotkey-Windows 中我需要新建同类别的标签页而不是默认, 因此需要明确指定其快捷键:
1.2 常用功能简述
iTerm2 功能丰富, 然而很多是锦上添花, 与其去记复杂的快捷键还不如多操作几步, 我认为有必要去记的也就这么几个:
+N,+T,+↔,+F,+W 这几个快捷键就不解释了.
+D 垂直分屏,++D 水平分屏,++↑↓↔ 切换分屏.
+全屏, + R 清屏.
+A/E 行首 / 尾,+R 查询历史命令.
+↔ 左右跳过单词.
记住这些就差不多了, 脑容量有限, 应能省则省.
2. zsh 配置
使用 zsh 已经很长时间了, 上篇文章 http://www.cnblogs.com/youclk/p/7876035.html 整理了一些 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)"
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 是唯一默认启用的插件, 通用配置在上篇文章 http://www.cnblogs.com/youclk/p/7876035.html 中已经详细说明. 在 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
有必要记录的是更换镜像源, 我选择的是中科大镜像 https://mirrors.ustc.edu.cn/ :
- # 替换 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 开发环境做一个归纳, 敬请期待.
来源: http://www.jianshu.com/p/c33ba7ded475