1.git(用來下載vim和相關插件)
sudo apt-get install git
2,cmake(用來編譯clang-llvm)
sudo apt-get install build-essential cmake
3.vim,這里手動編譯vim,因為需要python支持,直接apt-get的話會遇到很多麻煩
過程參考 https://github.com/Valloric/YouCompleteMe/wiki/Building-Vim-from-source
先安裝依賴項,注意,Ubuntu 16.04是 `liblua5.1-dev`而不是 `lua5.1-dev`
sudo apt-get install libncurses5-dev libgnome2-dev libgnomeui-dev \
ibgtk2.0-dev libatk1.0-dev libbonoboui2-dev \
libcairo2-dev libx11-dev libxpm-dev libxt-dev python-dev \
python3-dev ruby-dev lua5.1 liblua5.1-dev libperl-dev git
4.驗證安裝
vim --version
查找到python,前面有加號即支持python
python3
注意:雖然 vim --version | grep python
顯示的python前面確實是加號, 但沒有針對Ubuntu 16.04更改配置,導致vim支持還是沒有安裝。
所以個人認為。如果已經有vim的話還是移除它,重新安裝
4.1移除:
sudo apt-get remove vim vim-runtime gvim
sudo apt-get remove vim-tiny vim-common vim-gui-common vim-nox
4.2配置:
cd ~
git clone https://github.com/vim/vim.git
cd vim
./configure --with-features=huge \
--enable-multibyte \
--enable-rubyinterp=yes \
--enable-pythoninterp=yes \
--with-python-config-dir=/usr/lib/python2.7/config-x86_64-linux-gnu \
--enable-python3interp=yes \
--with-python3-config-dir=/usr/lib/python3.5/config-3.5m-x86_64_linux-gnu \
--enable-perlinterp=yes \
--enable-luainterp=yes \
--enable-gui=gtk2 --enable-cscope --prefix=/usr
make VIMRUNTIMEDIR=/usr/share/vim/vim81
4.3安裝:
cd ~/vim
sudo make install
安裝成功后,打開vim,命令模式下輸入 :echo has("python") || has("python3")
,結果是1就代表成功了。
4.4如果卸載的話相反的,需要保留vim文件夾並且安裝checkinstall
sudo apt-get install checkinstall
cd ~/vim
sudo checkinstall
5.安裝Vundle(用來管理vim插件)(類似於Python的pip)
5.1.安裝:
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
5.2.創建並配置.vimrc配置文件
將配置文件添加到你的用戶的home文件夾中:
sudo vim ~/vimrc
添加如下內容(大佬配置一):
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'
Plugin 'Valloric/YouCompleteMe'
" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
Plugin 'tpope/vim-fugitive'
" plugin from http://vim-scripts.org/vim/scripts.html
" Plugin 'L9'
" Git plugin not hosted on GitHub
Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
Plugin 'file:///home/gmarik/path/to/plugin'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" Install L9 and avoid a Naming conflict if you've already installed a
" different version somewhere else.
" Plugin 'ascenator/L9', {'name': 'newL9'}
" 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
或者添加如下內容(大佬配置二):
"vundle
set nocompatible
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
"git interface
Plugin 'tpope/vim-fugitive'
"filesystem
Plugin 'scrooloose/nerdtree'
Plugin 'jistr/vim-nerdtree-tabs'
Plugin 'kien/ctrlp.vim'
"html
" isnowfy only compatible with python not python3
"Plugin 'jtratner/vim-flavored-markdown'
Plugin 'suan/vim-instant-markdown'
Plugin 'nelstrom/vim-markdown-preview'
"python sytax checker
Plugin 'nvie/vim-flake8'
Plugin 'vim-scripts/indentpython.vim'
Plugin 'scrooloose/syntastic'
"auto-completion stuff
"Plugin 'klen/python-mode'
Plugin 'Valloric/YouCompleteMe'
"Plugin 'klen/rope-vim'
"Plugin 'davidhalter/jedi-vim'
Plugin 'ervandew/supertab'
""code folding
Plugin 'tmhedberg/SimpylFold'
"Colors!!!
Plugin 'altercation/vim-colors-solarized'
Plugin 'jnurmine/Zenburn'
call vundle#end()
filetype plugin indent on " enables filetype detection
let g:SimpylFold_docstring_preview = 1
""autocomplete
"let g:ycm_autoclose_preview_window_after_completion=1
" YouCompleteMe
set runtimepath+=~/.vim/bundle/YouCompleteMe
let g:ycm_collect_identifiers_from_tags_files = 1 " 開啟 YCM 基於標簽引擎
let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 注釋與字符串中的內容也用於補全
let g:syntastic_ignore_files=[".*\.py$"]
let g:ycm_seed_identifiers_with_syntax = 1 " 語法關鍵字補全
let g:ycm_complete_in_comments = 1
let g:ycm_confirm_extra_conf = 0
let g:ycm_key_list_select_completion = ['<c-n>', '<Down>'] " 映射按鍵, 沒有這個會攔截掉tab, 導致其他插件的tab不能用.
let g:ycm_key_list_previous_completion = ['<c-p>', '<Up>']
let g:ycm_complete_in_comments = 1 " 在注釋輸入中也能補全
let g:ycm_complete_in_strings = 1 " 在字符串輸入中也能補全
let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 注釋和字符串中的文字也會被收入補全
let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
let g:ycm_show_diagnostics_ui = 0 " 禁用語法檢查
inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<CR>" | " 回車即選中當前項
nnoremap <c-j> :YcmCompleter GoToDefinitionElseDeclaration<CR>| " 跳轉到定義處
"let g:ycm_min_num_of_chars_for_completion=2 " 從第2個鍵入字符就開始羅列匹配
"custom keys
let mapleader=" "
map <leader>g :YcmCompleter GoToDefinitionElseDeclaration<CR>
"
call togglebg#map("<F5>")
"colorscheme zenburn
"set guifont=Monaco:h14
let NERDTreeIgnore=['\.pyc$', '\~$'] "ignore files in NERDTree
"I don't like swap files
set noswapfile
"turn on numbering
set nu
"it would be nice to set tag files by the active virtualenv here
":set tags=~/mytags "tags for ctags and taglist
"omnicomplete
autocmd FileType python set omnifunc=pythoncomplete#Complete
"------------Start Python PEP 8 stuff----------------
" Number of spaces that a pre-existing tab is equal to.
au BufRead,BufNewFile *py,*pyw,*.c,*.h set tabstop=4
"spaces for indents
au BufRead,BufNewFile *.py,*pyw set shiftwidth=4
au BufRead,BufNewFile *.py,*.pyw set expandtab
au BufRead,BufNewFile *.py set softtabstop=4
" Use the below highlight group when displaying bad whitespace is desired.
highlight BadWhitespace ctermbg=red guibg=red
" Display tabs at the beginning of a line in Python mode as bad.
au BufRead,BufNewFile *.py,*.pyw match BadWhitespace /^\t\+/
" Make trailing whitespace be flagged as bad.
au BufRead,BufNewFile *.py,*.pyw,*.c,*.h match BadWhitespace /\s\+$/
" Wrap text after a certain number of characters
au BufRead,BufNewFile *.py,*.pyw, set textwidth=100
" Use UNIX (\n) line endings.
au BufNewFile *.py,*.pyw,*.c,*.h set fileformat=unix
" Set the default file encoding to UTF-8:
set encoding=utf-8
" For full syntax highlighting:
let python_highlight_all=1
syntax on
" Keep indentation level from previous line:
autocmd FileType python set autoindent
" make backspaces more powerfull
set backspace=indent,eol,start
"Folding based on indentation:
autocmd FileType python set foldmethod=indent
"use space to open folds
nnoremap <space> za
"----------Stop python PEP 8 stuff--------------
"js stuff"
autocmd FileType javascript setlocal shiftwidth=2 tabstop=2
5.3.直接運行vim,輸入:PluginInstall
,等待安裝,結束后退出即可。
以后安裝插件,直接可以編輯 .vimrc
文件,例如在.vimrc的 call vundle#begin()
和 call vundle#end()
之間加上 Plugin 'Valloric/YouCompleteMe'
,再運行 :PluginInsstall
即安裝YCM插件,
由於YouCompleteMe有223M左右,所以需要耐心等待,可以時不時用命令 du -s ~/.vim/bundle/YouCompleteMe
觀察文件夾大小
6.YCM(YouCompleteMe)自動補全
(用Vundle下載慢的話,可以先去官網下載:`git clone https://github.com/Valloric/YouCompleteMe ~/.vim/bundle/YouCompleteMe` )
使用的話有些特殊,需要編譯之后才能使用。
6.1. cd ~/.vim/bundle/YouCompleteMe
6.2. 安裝支持補全的語言關聯: ./install.py
# 這里可以使用install --help查看支持哪些補全
./install.py --clang-completer
# 關聯C家族的語言; ./install.py
# 支持python補全
6.3. 配置文件.ycm_extra_conf.py,包含了補全的系統頭文件和源文件出處。(python可以不用配置)
修改flags下的列表即可,在里面添加所需的目錄
sudo cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/
sudo vim ~/.ycm_extra_conf.py
改前
改后
如果出現錯誤:
則 sudo vim ~/.ycm_extra_conf.py
, 然后修改目錄:
把.ycm_extra_conf.py放在當前目錄配置,是因為YCM是先在當前目錄查找再網上走一直遞歸到找到為止,並且只會加載一次,方便修改。
End
附錄博客:
-
Vundle新老版本的Vundle安裝見此文章vundle: vim插件管理安裝之錯誤總結
-
.vimrc大佬配置二無注釋的可以見此文章:Vim配置(python版)
-
YCM的安裝配置見此文章Ubuntu 16.04 64位安裝YouCompleteMe