真實有效的Vim配置記錄(macOS)
以前在Ubuntu里配置了兩次Vim,macOS中配置了一次伦乔,都沒有配置成,一度放棄了Vim董习,安心地用Xcode烈和,最近要開始準備省賽了,就抓緊重新配置了一下Vim皿淋。
參考了一些不錯的博客:
從零搭建和配置OSX開發(fā)環(huán)境
Vim中的殺手級插件: YouCompleteMe
Vim配置招刹、插件和使用技巧
Homebrew
在macOS中,有兩款大家常用的管理工具:Homebrew或者MacPorts窝趣。這兩款工具都是為了解決同 樣的問題——為OSX安裝常用庫和工具疯暑。Homebrew與MacPorts的主要區(qū)別,是Homebrew不會破壞OSX 原生的環(huán)境哑舒,這也是推薦Homebrew的主要原因妇拯。同時它安裝的所有文件都是在用戶獨立空間內的,這讓你安裝的所有軟件對于該用戶來說都是可以訪問的散址,不需要使用sudo
命令乖阵。
在安裝前,需要去App Store中下載并且安裝Xcode预麸。
安裝方式
管理一下自帶的Ruby庫瞪浸,直接安裝即可。
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
常用命令
brew list # 查看已經(jīng)安裝的包
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
程序是Linux/UNIX
系統(tǒng)中的一層外殼对蒲,幾乎所有的應用程序都可以運行在shell
環(huán)境 下,常用的有bash
贡翘,zsh
等蹈矮。
下面命令在/etc/shells
文件中查看系統(tǒng)中的各種shell。
cat /etc/shells
而zsh
是macOS
系統(tǒng)原生的shell
之一鸣驱,其功能強大泛鸟,語法相對于bash
更加友好和強大,所以推薦 使用zsh
作為默認的shell
踊东。
下面命令切換zsh
為默認shell
chsh -s $(which zsh)
可以用Homebrew
安裝最新的zsh
北滥,同時也會保留原生的zsh
刚操。
brew install --disable-etcdir zsh
# 將 /usr/local/bin/zsh 添加到下面文件中
sudo vim /etc/shells
# 更換默認shell
chsh -s /usr/local/bin/zsh
參考配置
# 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
iTerm2
參考 iTerm:讓你的命令行也能豐富多彩
之所以用它完全是因為美觀起見韭邓。
iTerm2
可以改變配色咐柜,在Preferrence/Profile/Colors
中可以獲取網(wǎng)上的許多配色方案屋灌。而且字體也可以自己改變制恍。
當然阁谆,最吸引我的是Hotkey
惊畏。
在Preferrence/Keys
中可以設置Hotkey
归斤,可以在桌面上調出偎痛,為半透明司训。感覺很好看构捡。
Vim
終于到重頭戲了。
基礎配置
在Home目錄創(chuàng)建~/.vim
目錄和.vimrc
文件豁遭。
mkdir ~/.vim
vim .vimrc
.vimrc
就是Vim的配置腳本文件了叭喜,如果要自己看懂還是不太容易的,需要參考Vim的文檔蓖谢。
下面放上我的配置文件捂蕴。
" vundle
if filereadable(expand("~/.vimrc.bundles"))
source ~/.vimrc.bundles
endif
set nu
set showcmd
set laststatus=1
set magic
set cursorline
set mouse=a
set go=
set showmatch
set matchtime=1
set nobackup
set ruler
set autoindent
set confirm
set tabstop=4
set shiftwidth=4
set smarttab
set nocompatible
set expandtab
set backspace=indent,eol,start
set wildmenu
set fo=cqrt
set laststatus=2
set textwidth=78
set colorcolumn=+1
set ww=<,>,h,l
set noeb visualbell
let mapleader = ","
filetype on
filetype plugin on
filetype indent on
syntax on
syntax enable
" encoding
set fencs=utf-8,usc-bom,shift-jis,gb18030,gbk,gb2312,cp936
set termencoding=utf-8
set encoding=utf-8
set fileencodings=ucs-bom,utf-8,cp936
set fileencoding=utf-8
" Molokai
colorscheme molokai
highlight NonText guibg=#060606
highlight Folded guibg=#0A0A0A guifg=#9090D0
" some map
map <F5> :call CR()<CR>
func! CR()
exec "w"
exec "!g++ % -o %<"
exec "! ./%<"
endfunc
map <F10> :call RG()<CR>
func! RG()
exec "w"
exec "!g++ % -g -o % <"
exec "! ./%<"
endfunc
map <F2> :call SetTitle()<CR>
func SetTitle()
let l = 0
let l = l + 1 | call setline(l,'/******************************')
let l = l + 1 | call setline(l,' *File name: '.expand("%"))
let l = l + 1 | call setline(l,' *Author: wzhzzmzzy')
let l = l + 1 | call setline(l,' *Created Time: '.strftime("%c"))
let l = l + 1 | call setline(l,' *TODO:')
let l = l + 1 | call setline(l,'******************************/')
let l = l + 1 | call setline(l,'')
let l = l + 1 | call setline(l,'#include <cstdio>')
let l = l + 1 | call setline(l,'#include <cstring>')
let l = l + 1 | call setline(l,'#include <cstdlib>')
let l = l + 1 | call setline(l,'#include <iostream>')
let l = l + 1 | call setline(l,'#include <string>')
let l = l + 1 | call setline(l,'#include <algorithm>')
let l = l + 1 | call setline(l,'#include <vector>')
let l = l + 1 | call setline(l,'#include <queue>')
let l = l + 1 | call setline(l,'#include <set>')
let l = l + 1 | call setline(l,'#include <map>')
let l = l + 1 | call setline(l,'')
let l = l + 1 | call setline(l,'using namespace std;')
let l = l + 1 | call setline(l,'')
endfunc
map <F3> :call SetTitle2()<CR>
func SetTitle2()
let l = 0
let l = l + 1 | call setline(l,'/******************************')
let l = l + 1 | call setline(l,' *File name: '.expand("%"))
let l = l + 1 | call setline(l,' *Author: wzhzzmzzy')
let l = l + 1 | call setline(l,' *Created Time: '.strftime("%c"))
let l = l + 1 | call setline(l,' *TODO:')
let l = l + 1 | call setline(l,'******************************/')
let l = l + 1 | call setline(l,'')
let l = l + 1 | call setline(l,'#include <bits/stdc++.h>')
let l = l + 1 | call setline(l,'using namespace std;')
let l = l + 1 | call setline(l,'')
endfunc
" Nerd Tree
let NERDChristmasTree=0
let NERDTreeWinSize=40
let NERDTreeChDirMode=2
let NERDTreeIgnore=['\~$', '\.pyc$', '\.swp$']
let NERDTreeShowBookmarks=1
let NERDTreeWinPos="left"
autocmd vimenter * if !argc() | NERDTree | endif
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif
nmap <F6> :NERDTreeToggle<CR>
" Tagbar
let g:tagbar_width=35
let g:tagbar_autofocus=1
nmap <F7> :TagbarToggle<CR>
" YouCompleteMe
let g:ycm_autoclose_preview_window_after_completion=1
let g:ycm_server_python_interpreter='/usr/bin/python'
let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'
nnoremap <leader>g :YcmCompleter GoToDefinitionElseDeclaration<CR>
Vundle
Vim最麻煩的地方在于插件的配置,所幸有一個叫做Vundle
的插件管理工具闪幽,用它就能夠簡便非常多啥辨。它能夠搜索、安裝盯腌、更新和移除vim插件溉知,再也不需要手動管理vim插件。
- 安裝
Vundle
腕够。
git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle
- 在
.vimrc
中添加vundle
支持
filetype off
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
而我實際是將Vundle
的安裝插件文檔放在了另一個文本.vimrc.bundles
中级乍,這需要在.vimrc
中加入一段:
" vundle
if filereadable(expand("~/.vimrc.bundles"))
source ~/.vimrc.bundles
endif
在新的配置腳本中添加上面的vundle
支持。
下面的是我的.vimrc.bundles
帚湘,包含了我需要安裝的插件玫荣。
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 'Valloric/YouCompleteMe'
Bundle 'croaky/vim-colors-github'
Bundle 'danro/rename.vim'
Bundle 'majutsushi/tagbar'
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"
Bundle "klen/python-mode"
if filereadable(expand("~/.vimrc.bundles.local"))
source ~/.vimrc.bundles.local
endif
filetype on
- 安裝插件
bundle
分為三類,比較常用就是第二種:
- 在Github vim-scripts 用戶下的repos,只需要寫出repos名稱大诸。
- 在Github其他用戶下的repos, 需要寫出”用戶名/repos名”捅厂。
- 不在Github上的插件,需要寫出git全路徑资柔。
打開Vim焙贷,運行:BundleInstall
,或在shell
中直接運行vim +BundleInstall +qall
贿堰。
- 安裝完插件辙芍,還有可能會有一個問題,就是Vim版本不夠高羹与。
可以使用以下命令更新沸手。
brew install macvim --override-system-vim
然后用以下命令符號連接到/Application
外遇。
brew linkapps macvim
最后在.zshrc
配置文件中使用別名來使用更新后的vim
。
Vim常用插件
Vim有許許多多好用的插件契吉,配合起來甚至可以與IDE相比。
NERD Tree
NERD Tree
是一個樹形目錄插件诡渴,方便瀏覽當前目錄有哪些目錄和文件捐晶。
我在.vimrc
文件中配置NERD Tree
,設置一個啟用或禁用NERD Tree
的鍵映射妄辩。
nmap <F5> :NERDTreeToggle<cr>
配置之后只需按F5鍵就能啟用或禁用NERD Tree
惑灵,NERD Tree
提供一些常用快捷鍵來操作目錄。
通過hjkl來移動光標
o打開關閉文件或目錄眼耀,如果想打開文件英支,必須光標移動到文件名
t在標簽頁中打開
s和i可以水平或縱向分割窗口打開文件
p到上層目錄
YouCompleteMe & syntastic
YouCompleteMe是一個快速、支持模糊匹配的vim代碼補全引擎哮伟。它是基于Clang引擎為C/C++/Objective-C提供代碼提示干花,也支持其他語言代碼提示的引擎,例如基于Jedi的Python代碼補全楞黄,基于OmniSharp的C#代碼補全池凄,基于Gocode的Go代碼補全。
只需敲入代碼鬼廓,就自動提示想輸入的代碼列表肿仑,你可以選擇其中一個,然后tab鍵就可以補全代碼碎税。
- 安裝及編譯
在Vundle
中安裝尤慰,然后編譯之。
老版本的YCM是編譯install.sh
雷蹂,但是現(xiàn)在換成了install.py
伟端。
let g:ycm_global_ycm_extra_conf = 'your path to .ycm_extra_conf.py'
- 配置
不同于很多vim插件,YCM首先需要編譯萎河,另外還需要有配置荔泳。在vim啟動后,YCM會找尋當前路徑以及上層路徑的.ycm_extra_conf.py
虐杯。在~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py
中提供了默認的模板玛歌。
下面是一個修改過的模板,優(yōu)化了標準庫找不到的問題擎椰。
# This file is NOT licensed under the GPLv3, which is the license for the rest
# of YouCompleteMe.
#
# Here's the license text for this file:
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org/>
import os
import ycm_core
# These are the compilation flags that will be used in case there's no
# compilation database set (by default, one is not set).
# CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR.
flags = [
'-Wall',
'-Wextra',
#'-Werror',
#'-Wc++98-compat',
'-Wno-long-long',
'-Wno-variadic-macros',
'-fexceptions',
'-stdlib=libc++',
# THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won't know which
# language to use when compiling headers. So it will guess. Badly. So C++
# headers will be compiled as C headers. You don't want that so ALWAYS specify
# a "-std=<something>".
# For a C project, you would set this to something like 'c99' instead of
# 'c++11'.
'-std=c++11',
# ...and the same thing goes for the magic -x option which specifies the
# language that the files to be compiled are written in. This is mostly
# relevant for c++ headers.
# For a C project, you would set this to 'c' instead of 'c++'.
'-x',
'c++',
'-I',
'.',
'-isystem',
'/usr/include',
'-isystem',
'/usr/local/include',
'-isystem',
'/Library/Developer/CommandLineTools/usr/include',
'-isystem',
'/Library/Developer/CommandLineTools/usr/bin/../lib/c++/v1',
]
# Set this to the absolute path to the folder (NOT the file!) containing the
# compile_commands.json file to use that instead of 'flags'. See here for
# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
#
# Most projects will NOT need to set this to anything; you can just change the
# 'flags' list of compilation flags. Notice that YCM itself uses that approach.
compilation_database_folder = ''
if os.path.exists( compilation_database_folder ):
database = ycm_core.CompilationDatabase( compilation_database_folder )
else:
database = None
SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]
def DirectoryOfThisScript():
return os.path.dirname( os.path.abspath( __file__ ) )
def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
if not working_directory:
return list( flags )
new_flags = []
make_next_absolute = False
path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
for flag in flags:
new_flag = flag
if make_next_absolute:
make_next_absolute = False
if not flag.startswith( '/' ):
new_flag = os.path.join( working_directory, flag )
for path_flag in path_flags:
if flag == path_flag:
make_next_absolute = True
break
if flag.startswith( path_flag ):
path = flag[ len( path_flag ): ]
new_flag = path_flag + os.path.join( working_directory, path )
break
if new_flag:
new_flags.append( new_flag )
return new_flags
def IsHeaderFile( filename ):
extension = os.path.splitext( filename )[ 1 ]
return extension in [ '.h', '.hxx', '.hpp', '.hh' ]
def GetCompilationInfoForFile( filename ):
# The compilation_commands.json file generated by CMake does not have entries
# for header files. So we do our best by asking the db for flags for a
# corresponding source file, if any. If one exists, the flags for that file
# should be good enough.
if IsHeaderFile( filename ):
basename = os.path.splitext( filename )[ 0 ]
for extension in SOURCE_EXTENSIONS:
replacement_file = basename + extension
if os.path.exists( replacement_file ):
compilation_info = database.GetCompilationInfoForFile(
replacement_file )
if compilation_info.compiler_flags_:
return compilation_info
return None
return database.GetCompilationInfoForFile( filename )
def FlagsForFile( filename, **kwargs ):
if database:
# Bear in mind that compilation_info.compiler_flags_ does NOT return a
# python list, but a "list-like" StringVec object
compilation_info = GetCompilationInfoForFile( filename )
if not compilation_info:
return None
final_flags = MakeRelativePathsInFlagsAbsolute(
compilation_info.compiler_flags_,
compilation_info.compiler_working_dir_ )
# NOTE: This is just for YouCompleteMe; it's highly likely that your project
# does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR
# ycm_extra_conf IF YOU'RE NOT 100% SURE YOU NEED IT.
#try:
# final_flags.remove( '-stdlib=libc++' )
#except ValueError:
# pass
else:
relative_to = DirectoryOfThisScript()
final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )
return {
'flags': final_flags,
'do_cache': True
}
這樣支子,差不多就配置完成了YCM。