從零搭建和配置OSX開發環境

weixin_34088583發表於2017-07-03

對於每一名開發者來說,更換系統或者更換電腦的時候,都免不了花上不短的時間去折騰開 發環境的問題。我本人也是三番兩次,深知這個過程的繁瑣。所有,根據我自己以往的經驗, 以及參考一下他人的意見,整理一下關於在Mac下做各種開發的配置,包含Java, Ruby, Vim, git, 資料庫等等。歡迎補充與修正。

 Terminal篇

這篇文章包含配置控制檯環境,包括包管理工具, zsh, Vim, git的安裝配置。

Homebrew, 你不能錯過的包管理工具

包管理工具已經成為現在作業系統中不可缺少的一個重要工具了,它能大大減輕軟體安裝的 負擔,節約我們的時間。Linux中常用的有yumapt-get工具,甚至Windows平臺也 有Chocolatey這樣優秀的工具,OSX平臺自然有它獨有的工具。

在OSX中,有兩款大家常用的管理工具:Homebrew或者MacPorts。這兩款工具都是為了解決同 樣的問題——為OSX安裝常用庫和工具。Homebrew與MacPorts的主要區別是Homebrew不會破壞OSX 原生的環境,這也是我推薦Homebrew的主要原因。同時它安裝的所有檔案都是在使用者獨立空間內 的,這讓你安裝的所有軟體對於該使用者來說都是可以訪問的,不需要使用sudo命令。

在安裝Homebrew前,你需要前往AppStore下載並安裝Xcode.

安裝方式:

1
2
3
# OSX系統基本上都自帶Ruby1.9
# 所以無需先安裝Ruby,但是之後我們需要管理Ruby
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Homebrew常用命令:

1
2
3
4
5
6
7
8
9
10
brew list                 # 檢視已經安裝的包
brew update               # 更新Homebrew自身
brew doctor               # 診斷關於Homebrew的問題(Homebrew 有問題時請用它)
brew cleanup              # 清理老版本軟體包或者無用的檔案
brew show ${formula}      # 檢視包資訊
brew search ${formula}    # 按名稱搜尋
brew upgrade ${formula}   # 升級軟體包
brew install ${formula}   # 按名稱安裝
brew uninstall ${formula} # 按名稱解除安裝
brew pin/unpin ${formula} # 鎖定或者解鎖軟體包版本,防止誤升級

zsh,好用的shell

Shell程式就是Linux/UNIX系統中的一層外殼,幾乎所有的應用程式都可以執行在Shell環境 下,常用的有bash, csh, zcsh等。在/etc/shells檔案中可以檢視系統中的各種shell。

1
2
3
4
5
6
7
8
9
10
11
12
cat /etc/shells

# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

而zsh是OSX系統原生的shell之一,其功能強大,語法相對於bash更加友好和強大,所以推薦 使用zsh作為預設的shell。

1
2
# 切換zsh為預設shell
chsh -s $(which zsh)

如果你想使用最新的zsh,你可以使用Homebrew,此方法也會保留原生的zsh,防止你在某個 時刻需要它。

1
2
3
4
5
6
7
8
9
10
11
12
# 檢視最新zsh資訊
brew info zsh

# 安裝zsh
brew install --disable-etcdir zsh

# 新增shell路徑至/etc/shells檔案中
# 將 /usr/local/bin/zsh 新增到下面檔案中
sudo vim /etc/shells

# 更換預設shell
chsh -s /usr/local/bin/zsh

下面貼上我的zsh配置以供參考

我的zsh配置 (zshrc)download

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# modify the prompt to contain git branch name if applicable
git_prompt_info() {
  ref=$(git symbolic-ref HEAD 2> /dev/null)
  if [[ -n $ref ]]; then
    echo " %{$fg_bold[green]%}${ref#refs/heads/}%{$reset_color%}"
  fi
}
setopt promptsubst
export PS1='${SSH_CONNECTION+"%{$fg_bold[green]%}%n@%m:"}%{$fg_bold[blue]%}%c%{$reset_color%}$(git_prompt_info) %# '

# load our own completion functions
fpath=(~/.zsh/completion $fpath)

# completion
autoload -U compinit
compinit

# load custom executable functions
for function in ~/.zsh/functions/*; do
  source $function
done

# makes color constants available
autoload -U colors
colors

# enable colored output from ls, etc
export CLICOLOR=1

# history settings
setopt hist_ignore_all_dups inc_append_history
HISTFILE=~/.zhistory
HISTSIZE=4096
SAVEHIST=4096

# awesome cd movements from zshkit
setopt autocd autopushd pushdminus pushdsilent pushdtohome cdablevars
DIRSTACKSIZE=5

# Enable extended globbing
setopt extendedglob

# Allow [ or ] whereever you want
unsetopt nomatch

# vi mode
bindkey -v
bindkey "^F" vi-cmd-mode
bindkey jj vi-cmd-mode

# handy keybindings
bindkey "^A" beginning-of-line
bindkey "^E" end-of-line
bindkey "^R" history-incremental-search-backward
bindkey "^P" history-search-backward
bindkey "^Y" accept-and-hold
bindkey "^N" insert-last-word
bindkey -s "^T" "^[Isudo ^[A" # "t" for "toughguy"

# use vim as the visual editor
export VISUAL=vim
export EDITOR=$VISUAL

# load rbenv if available
if which rbenv &>/dev/null ; then
  eval "$(rbenv init - --no-rehash)"
fi

# load thoughtbot/dotfiles scripts
export PATH="$HOME/.bin:$PATH"

# mkdir .git/safe in the root of repositories you trust
export PATH=".git/safe/../../bin:$PATH"

# aliases
[[ -f ~/.aliases ]] && source ~/.aliases

# Local config
[[ -f ~/.zshrc.local ]] && source ~/.zshrc.local

好用的編輯器 Vim

對於Vim,無需溢美之詞,作為與emacs並列的兩大編輯器,早已經被無數人奉為經典。而它卻 又以超長的學習曲線,使得很多人望而卻步。長久以來,雖然擁有大量的外掛,卻缺少一個 確之有效的外掛管理器。所幸,Vundle的出現解決了這個問題。

Vundle可以讓你在配置檔案中管理外掛,並且非常方便的查詢、安裝、更新或者刪除外掛。 還可以幫你自動配置外掛的執行路徑和生成幫助檔案。相對於另外一個管理工具pathogen, 可以說有著巨大的優勢。

1
2
# vundle 安裝和配置
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
" 將下面配置檔案加入到.vimrc檔案中
set nocompatible " 必須
filetype off     " 必須

" 將Vundle加入執行時路徑中(Runtime path)
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" 使用Vundle管理外掛,必須
Plugin 'gmarik/Vundle.vim'

"
" 其他外掛
"

call vundle#end() " 必須
filetype plugin indent on " 必須

最後,你只需要執行安裝命令,即可以安裝好所需的外掛。

1
2
3
4
5
# 在vim中
:PluginInstall

# 在終端
vim +PluginInstall +qall

下面列出我的Vim外掛和配置

Vim外掛 (vimrc.bundles)download

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
if &compatible
  set nocompatible
end

filetype off
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()

" Let Vundle manage Vundle
Bundle 'gmarik/vundle'

" Define bundles via Github repos
Bundle 'christoomey/vim-run-interactive'
Bundle 'croaky/vim-colors-github'
Bundle 'danro/rename.vim'
Bundle 'kchmck/vim-coffee-script'
Bundle 'kien/ctrlp.vim'
Bundle 'pbrisbin/vim-mkdir'
Bundle 'scrooloose/syntastic'
Bundle 'slim-template/vim-slim'
Bundle 'thoughtbot/vim-rspec'
Bundle 'tpope/vim-bundler'
Bundle 'tpope/vim-endwise'
Bundle 'tpope/vim-fugitive'
Bundle 'tpope/vim-rails'
Bundle 'tpope/vim-surround'
Bundle 'vim-ruby/vim-ruby'
Bundle 'vim-scripts/ctags.vim'
Bundle 'vim-scripts/matchit.zip'
Bundle 'vim-scripts/tComment'
Bundle "mattn/emmet-vim"
Bundle "scrooloose/nerdtree"
Bundle "Lokaltog/vim-powerline"
Bundle "godlygeek/tabular"
Bundle "msanders/snipmate.vim"
Bundle "jelera/vim-javascript-syntax"
Bundle "altercation/vim-colors-solarized"
Bundle "othree/html5.vim"
Bundle "xsbeats/vim-blade"
Bundle "Raimondi/delimitMate"
Bundle "groenewege/vim-less"
Bundle "evanmiller/nginx-vim-syntax"
Bundle "Lokaltog/vim-easymotion"
Bundle "tomasr/molokai"

if filereadable(expand("~/.vimrc.bundles.local"))
  source ~/.vimrc.bundles.local
endif

filetype on

Vim配置 (vimrc)download

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
" Use Vim settings, rather then Vi settings. This setting must be as early as
" possible, as it has side effects.
set nocompatible

" Highlight current line
au WinLeave * set nocursorline nocursorcolumn
au WinEnter * set cursorline cursorcolumn
set cursorline cursorcolumn

" Leader
let mapleader = ","

set backspace=2   " Backspace deletes like most programs in insert mode
set nobackup
set nowritebackup
set noswapfile    " http://robots.thoughtbot.com/post/18739402579/global-gitignore#comment-458413287
set history=50
set ruler         " show the cursor position all the time
set showcmd       " display incomplete commands
set incsearch     " do incremental searching
set laststatus=2  " Always display the status line
set autowrite     " Automatically :write before running commands
set confirm       " Need confrimation while exit
set fileencodings=utf-8,gb18030,gbk,big5

" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if (&t_Co > 2 || has("gui_running")) && !exists("syntax_on")
  syntax on
endif

if filereadable(expand("~/.vimrc.bundles"))
  source ~/.vimrc.bundles
endif

filetype plugin indent on

augroup vimrcEx
  autocmd!

  " When editing a file, always jump to the last known cursor position.
  " Don't do it for commit messages, when the position is invalid, or when
  " inside an event handler (happens when dropping a file on gvim).
  autocmd BufReadPost *
    \ if &ft != 'gitcommit' && line("'\"") > 0 && line("'\"") <= line("$") |
    \   exe "normal g`\"" |
    \ endif

  " Cucumber navigation commands
  autocmd User Rails Rnavcommand step features/step_definitions -glob=**/* -suffix=_steps.rb
  autocmd User Rails Rnavcommand config config -glob=**/* -suffix=.rb -default=routes

  " Set syntax highlighting for specific file types
  autocmd BufRead,BufNewFile Appraisals set filetype=ruby
  autocmd BufRead,BufNewFile *.md set filetype=markdown

  " Enable spellchecking for Markdown
  autocmd FileType markdown setlocal spell

  " Automatically wrap at 80 characters for Markdown
  autocmd BufRead,BufNewFile *.md setlocal textwidth=80
augroup END

" Softtabs, 2 spaces
set tabstop=2
set shiftwidth=2
set shiftround

相關文章