从零搭建和配置OSX开发环境

对于每一名开发者来说,更换系统或者更换电脑的时候,都免不了花上不短的时间去折腾开 发环境的问题。我本人也是三番两次,深知这个过程的繁琐。所有,根据我自己以往的经验, 以及参考一下他人的意见,整理一下关于在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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值