vim配置
2018-11-12 本文已影响25人
流动码文
使用Vim的过程中会发现其自带的代码补全功能非常的不实用。
今天介绍一下如何使用Tern和YouCompleteMe在Vim中实现Javascript自动补全。
安装Vundle
1,使用如下命令下载 Vundle到.vim/bundle/Vundle.vim目录下
mkdir ~/.vim/bundle
mkdir ~/.vim/bundle/Vundle.vim
cd ~/.vim/bundle/Vundle.vim
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
2,在.vimrc中配置Vundle
vim ~/.vimrc
- 在其中加入如下内容
set nocompatible
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'gmarik/Vundle.vim'
call vundle#end()
filetype plugin indent on
安装 Auto Complete 和 Tern
1,使用Vundle安装YouCompleteMe和Tern
- 打开 .vimrc
vim ~/.vimrc
- 在 Plugin 'gmarik/Vundle.vim' 后面追加如下内容
Plugin 'Valloric/YouCompleteMe'
Plugin 'marijnh/tern_for_vim'
-打开vim,并运行:PluginInstall
2,编译YouComplete
- 编译前需要先安装cmake
cd ~/.vim/bundle/YouCompleteMe/
./install.sh
配置 Tern
1,在Tern目录下执行npm install
cd ~/.vim/bundle/tern_for_vim
npm install
2, 在你的项目根目录创建 .tern_project 文件,并配置
touch .tern_project
在其中加入如下内容
{
"libs": [
"browser",
"underscore",
"jquery"
],
"plugins": {
}
}
在plugin中可以加入node或者angular等关键字,加载该库的补全功能。如下:
"plugins": {
"node": {}
}
.vimrc 配置文件内容
" Set vundle settings here
" git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
set nocompatible " be iMproved, required
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')
" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim' "https://github.com/VundleVim/Vundle.vim
" Custom plugins
Plugin 'scrooloose/nerdtree' "https://github.com/scrooloose/nerdtree
Plugin 'MattesGroeger/vim-bookmarks' "https://github.com/MattesGroeger/vim-bookmarks
Plugin 'maciakl/vim-neatstatus' "https://github.com/maciakl/vim-neatstatus
Plugin 'Valloric/YouCompleteMe'
Plugin 'marijnh/tern_for_vim'
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList - lists configured plugins
" :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
" Vim5 and later versions support syntax highlighting. Uncommenting the
" following enables syntax highlighting by default.
if has("syntax")
syntax on " 语法高亮
endif
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
" Uncomment the following to have Vim jump to the last position when
" reopening a file
if has("autocmd")
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
"have Vim load indentation rules and plugins according to the detected
"filetype on
"filetype plugin indent on
endif
set number
set autoindent
set softtabstop=4 " 设置软制表符的宽度
set shiftwidth=4 " (自动) 缩进使用的4个空格
set tabstop=4 " 设置制表符(tab键)的宽度
set expandtab " 行首tab转换为4个空格
set cindent " 使用 C/C++ 语言的自动缩进方式
set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s "设置C/C++语言的具体缩进方式
set showmatch " 设置匹配模式,显示匹配的括号
set linebreak " 整词换行
set whichwrap=b,s,<,>,[,] " 光标从行首和行末时可以跳到另一行去
set ruler " 标尺,用于显示光标位置的行号和列号,逗号分隔。每个窗口都有自己的标尺。如果窗口有状态行,标尺在那里显示。否则,它显示在屏幕的最后一行上
set showcmd " 命令行显示输入的命令
set showmode " 命令行显示vim当前模式
set incsearch " 输入字符串就显示匹配点
set enc=utf-8 " 文件编码
set cursorline
highlight CursorLine cterm=NONE ctermbg=blue ctermfg=white guibg=NONE guifg=NONE
" highlight CursorColumn cterm=NONE ctermbg=green ctermfg=NONE guibg=NONE guifg=NONE
" NERDTree settings
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif
let NERDTreeWinPos=0
" 关闭NERDTree快捷键
" map <leader>t :NERDTreeToggle<CR>
map <C-n> :NERDTreeToggle<CR>
" 显示行号
let NERDTreeShowLineNumbers=1
let NERDTreeAutoCenter=1
" 是否显示隐藏文件
let NERDTreeShowHidden=1
" 设置宽度
let NERDTreeWinSize=30
" 在终端启动vim时,共享NERDTree
let g:nerdtree_tabs_open_on_console_startup=1
" 忽略一下文件的显示
let NERDTreeIgnore=['\.pyc','\~$','\.swp']
" 显示书签列表
let NERDTreeShowBookmarks=1
" vim不指定具体文件打开是,自动使用nerdtree
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif
" 当vim打开一个目录时,nerdtree自动使用
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists("s:std_in") | exe 'NERDTree' argv()[0] | wincmd p | ene | endif
" 当vim中没有其他文件,值剩下nerdtree的时候,自动关闭窗口
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
" 改变nerdtree的箭头
let g:NERDTreeDirArrowExpandable = '▸'
let g:NERDTreeDirArrowCollapsible = '▾'
" vim-bookmarks settings
let g:bookmark_auto_close = 1
let g:bookmark_save_per_working_dir = 1
let g:bookmark_highlight_lines = 1
let g:bookmark_center = 1
let g:bookmark_location_list = 1
let mapleader = ','
nnoremap <C-l> gt
nnoremap <C-h> gT
nnoremap <leader>t : tabe<CR>
" key mapping
:inoremap { {}<ESC>i
:map <f2> :NERDTreeToggle<CR>