真实有效的Vim配置记录(macOS)
以前在Ubuntu里配置了两次Vim,macOS中配置了一次,都没有配置成,一度放弃了Vim,安心地用Xcode,最近要开始准备省赛了,就抓紧重新配置了一下Vim。
参考了一些不错的博客:
从零搭建和配置OSX开发环境
Vim中的杀手级插件: YouCompleteMe
Vim配置、插件和使用技巧
Homebrew
在macOS中,有两款大家常用的管理工具:Homebrew或者MacPorts。这两款工具都是为了解决同 样的问题——为OSX安装常用库和工具。Homebrew与MacPorts的主要区别,是Homebrew不会破坏OSX 原生的环境,这也是推荐Homebrew的主要原因。同时它安装的所有文件都是在用户独立空间内的,这让你安装的所有软件对于该用户来说都是可以访问的,不需要使用sudo
命令。
在安装前,需要去App Store中下载并且安装Xcode。
安装方式
管理一下自带的Ruby库,直接安装即可。
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
常用命令
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
程序是Linux/UNIX
系统中的一层外壳,几乎所有的应用程序都可以运行在shell
环境 下,常用的有bash
,zsh
等。
下面命令在/etc/shells
文件中查看系统中的各种shell。
cat /etc/shells
而zsh
是macOS
系统原生的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
中可以获取网上的许多配色方案。而且字体也可以自己改变。
当然,最吸引我的是Hotkey
。
在Preferrence/Keys
中可以设置Hotkey
,可以在桌面上调出,为半透明。感觉很好看。
Vim
终于到重头戏了。
基础配置
在Home目录创建~/.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
,但是现在换成了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
中提供了默认的模板。
下面是一个修改过的模板,优化了标准库找不到的问题。
# 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。