对经常使用终端的开发者来说,vim就是一个神器,神器再好,也需要用起来顺手,本文就来记录一下如何打造一个自己用起来顺手的vim。
由于好多Linux系统自带的vim都是8以下的版本,好多插件都要求vim8以上版本,但是从vim7升级到vim8又很麻烦,所有这里使用第三方的neovim,完美兼容vim8插件,并且用起来更顺手。安装也很简单,到github上下载对应的可执行文件,按照说明安装配置即可。下面的操作默认已安装好neovim。
- 前置准备
pip3 install --user pynvim
pip3 install --user jedi
pip3 install --user neovim-remote
安装clang, ctags
tabnine是代码提示的神器
tabnine如果下载失败,可以手动下载,下载地址是:
https://update.tabnine.com/2.8.5/x86_64-unknown-linux-gnu/TabNine
- 插件管理器安装
其实就是下载plug.vim文件放到~/.local/share/nvim/site/autoload/plug.vim中
sh -c 'curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
- 我的配置
配置了python,C/C++,Go开发环境
set runtimepath^=~/.vim runtimepath+=~/.vim/after
let &packpath = &runtimepath
let mapleader = ","
set nu
set autoindent
set smartindent
set tabstop=4
set shiftwidth=4
set noexpandtab
set softtabstop=4
set nocompatible
set fileencodings=utf-8
set noswapfile
"设置支持鼠标操作
set mouse=a
set ignorecase
set smartcase
"高亮当前行
set cursorline
highlight CursorLine cterm=NONE ctermbg=black ctermfg=green guibg=NONE guifg=NONE
" 退出插入模式指定类型的文件自动保存
"au InsertLeave *.go,*.sh,*.py,*.c,*.cpp,*.h write
call plug#begin('~/.vim/plugged')
if has('nvim')
Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
else
Plug 'Shougo/deoplete.nvim'
Plug 'roxma/nvim-yarp'
Plug 'roxma/vim-hug-neovim-rpc'
endif
Plug 'ctrlpvim/ctrlp.vim'
Plug 'rust-lang/rust.vim'
Plug 'fatih/vim-go'
Plug 'relunctance/vim-godef'
Plug 'scrooloose/nerdtree'
Plug 'majutsushi/tagbar'
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
Plug 'hardcoreplayers/gruvbox9'
Plug 'Shougo/deoplete-clangx'
" Use release branch (recommend)
"Plug 'neoclide/coc.nvim'
Plug 'deoplete-plugins/deoplete-jedi'
Plug 'deoplete-plugins/deoplete-go', { 'do': 'make'}
Plug 'tpope/vim-surround'
Plug 'jiangmiao/auto-pairs'
" NerdTree icon
"Plug 'ryanoasis/vim-devicons'
Plug 'Shougo/echodoc.vim'
Plug 'sebastianmarkow/deoplete-rust'
Plug 'tbodt/deoplete-tabnine', { 'do': './install.sh' }
Plug 'voldikss/vim-floaterm'
call plug#end()
" Change clang binary path
call deoplete#custom#var('clangx', 'clang_binary', '/usr/bin/clang')
" Change clang options
call deoplete#custom#var('clangx', 'default_c_options', '')
call deoplete#custom#var('clangx', 'default_cpp_options', '')
call deoplete#custom#var('tabnine', {
'line_limit': 500,
'max_num_results': 20,
})
"map <F3> :NERDTree<CR>
map <F4> :NERDTreeToggle<CR>
let g:deoplete#enable_at_startup = 1
filetype plugin indent on
syntax enable
autocmd FileType go nmap <leader>b <Plug>(go-build)
autocmd FileType go nmap <leader>r <Plug>(go-run)
autocmd FileType go nmap <leader>t <Plug>(go-test)
"" run :GoBuild or :GoTestCompile based on the go file
function! s:build_go_files()
let l:file = expand('%')
if l:file =~# '^f+_test.go$'
call go#test#Test(0, 1)
elseif l:file =~# '^f+.go$'
call go#cmd#Build(0)
endif
endfunction
"autocmd FileType go nmap <leader>b :<C-u>call <SID>build_go_files()<CR>
autocmd FileType go nmap <Leader>c <Plug>(go-coverage-toggle)
let g:go_fmt_command = "goimports"
let g:go_fmt_fail_silently = 1
let g:go_highlight_types = 1
let g:go_highlight_fields = 1
let g:go_highlight_functions = 1
let g:go_highlight_function_calls = 1
let g:go_highlight_operators = 1
let g:go_highlight_extra_types = 1
let g:go_highlight_build_constraints = 1
let g:go_highlight_generate_tags = 1
let g:rehash256 = 1
let g:molokai_original = 1
let g:go_decls_includes = "func,type"
let g:go_def_mode='gopls'
let g:go_info_mode='gopls'
let g:rustfmt_autosave = 1
let g:python3_host_prog='/usr/bin/python3'
"nmap <F8> :TagbarToggle<CR>
set background=dark
colorscheme gruvbox9
let g:gruvbox_filetype_hi_groups = 1
let g:gruvbox_plugin_hi_groups = 1
let g:airline#extensions#tabline#enabled = 1
"set cmdheight=2
let g:echodoc#enable_at_startup = 1
" for nvim
"let g:echodoc#type = 'virtual'
let g:echodoc#type = 'floating'
highlight link EchoDocFloat Pmenu
"imap <F2> <C-x><C-o>
"close preview when autocomplete code
"关闭函数功能提示预览
set completeopt-=preview
"set vb t_vb=
" enable powerline font
let g:airline_powerline_fonts = 1
"按Ctrl+k 向左移动一个buffer,就是vim打开的多个文件
nnoremap <C-k> :bp<CR>
"按Ctrl+l 向右移动一个buffer
nnoremap <C-l> :bn<CR>
"按Ctrl+^ 关闭当前buffer
nnoremap <C-^> :bd<CR>
"inoremap <expr> <SID>org-mapping org#tbl#kbd_cr()
"imap <Enter> <SID>org-mapping<SNR>17_AutoPairsReturn
let g:NERDTreeDirArrowExpandable = ''
let g:NERDTreeDirArrowCollapsible = ''
set encoding=utf8
set guifont=DroidSansMono Nerd Font 11
let g:airline_powerline_fonts = 1
" 设置宽度
let NERDTreeWinSize=24
" 在终端启动vim时,共享NERDTree
"let g:nerdtree_tabs_open_on_console_startup=1
" 忽略一下文件的显示
let NERDTreeIgnore=['.pyc','~$','.swp','.exe','.o', '.a', '.so']
""当NERDTree为剩下的唯一窗口时自动关闭
"autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
" add for deoplete-go
set completeopt+=noselect
let g:deoplete#sources#go#unimported_packages=1
let g:deoplete#sources#go#sort_class = ['package', 'func', 'type', 'var', 'const']
let g:deoplete#sources#go#pointer = 1
let g:deoplete#sources#go#source_importer = 1
let g:deoplete#sources#go#builtin_objects = 1
let g:deoplete#sources#go#fallback_to_source=1
" deoplete-rust
let g:deoplete#sources#rust#racer_binary='/home/colin/.cargo/bin/racer'
let g:deoplete#sources#rust#rust_source_path='/usr/local/rust/src'
let g:deoplete#sources#rust#show_duplicates=1
nmap <buffer> gd <plug>DeopleteRustGoToDefinitionDefault
nmap <buffer> K <plug>DeopleteRustShowDocumentation
" ctags配置
map <F5> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR>
imap <F5> <ESC>:!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR>
set tags=tags
set tags+=./tags " 表示在当前工作目录下搜索tags文件
" vim-floaterm快捷键配置
let g:floaterm_keymap_new = '<F7>'
let g:floaterm_keymap_prev = '<F8>'
let g:floaterm_keymap_next = '<F9>'
let g:floaterm_keymap_toggle = '<F12>'
" 第三方库支持代码提示
call deoplete#custom#option('omni_patterns', { 'go': '[^. *t].w*' })
- 快捷键说明
F4打开/关闭文件树
F5使用ctags生产tags文件
F7在vim中打开一个shell
F8跳转到上一个shell
F9跳转到下一个shell
F12显示/隐藏shell
Ctrl + ] 跳转到函数定义, Ctrl + T返回
对go语言,[, + r] 运行当前代码, [, + b] 编译当前文件,[, + t] 运行测试文件
Ctrl + K/L跳转Buffer, Ctrl + ^ 关闭buffer
在NERDTRee 区域,m键弹出菜单,可以对文件删除添加等操作,r键可以刷新目录树
分屏快捷键 : 垂直分屏:<,vs>,水平分屏:<,sp>
分屏操作: <Ctrl + w + [hjkl/上下左右]> 切换分屏窗口, <Ctrl + w c>关闭分屏
- 最终效果