Skip to content

Instantly share code, notes, and snippets.

@ahem
Created January 22, 2019 08:42
Show Gist options
  • Save ahem/68876d08e7eba9f6e929553ccb7f3830 to your computer and use it in GitHub Desktop.
Save ahem/68876d08e7eba9f6e929553ccb7f3830 to your computer and use it in GitHub Desktop.
Neovim configuration before switching to CoC autocomplete
set laststatus=2
set splitright
set splitbelow
set visualbell
set autoindent
set history=50 " keep 50 lines of command line history
set ruler " show the cursor position all the time
set showcmd " display incomplete commands
set incsearch " do incremental searching
set noignorecase " do casesensitive searching (\c anywhere in pattern for insensitive)
set backspace=indent,eol,start " allow backspacing over stuff in insert mode
set hidden " make buffers work better
set shiftwidth=4
set tabstop=4
set smarttab
set softtabstop=4
set shiftround
set expandtab
set lazyredraw
colorscheme desert
let mapleader = " "
nnoremap <Leader>g :e#<CR>
nmap <Leader>c :nohlsearch<cr>
imap <f1> <esc>
nmap <f1> <esc>
nmap <c-j> <c-w>j
nmap <c-k> <c-w>k
nmap <c-h> <c-w>h
nmap <c-l> <c-w>l
nmap <leader>f :CtrlP<cr>
nmap <leader>b :CtrlPBuffer<cr>
"moll/vim-bbye
nmap <leader>q :Bdelete<CR>
set guioptions-=T "remove toolbar
set guioptions-=t "remove tearoff menus
if &encoding != 'utf-8'
set encoding=utf-8
endif
" ============= Plugins ====================== {{{
if empty(glob('~/.local/share/nvim/site/autoload/plug.vim'))
silent !curl -s -fLo ~/.local/share/nvim/site/autoload/plug.vim --create-dirs
\ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif
call plug#begin('~/.local/share/nvim/plugged')
Plug 'jimsei/winresizer'
Plug 'moll/vim-bbye'
Plug 'sjl/gundo.vim'
Plug 'tpope/vim-commentary'
Plug 'tpope/vim-repeat'
Plug 'tpope/vim-surround'
Plug 'ctrlpvim/ctrlp.vim'
Plug 'FelikZ/ctrlp-py-matcher'
Plug 'tpope/vim-fugitive'
Plug 'tpope/vim-unimpaired'
Plug 'reedes/vim-pencil'
Plug 'editorconfig/editorconfig-vim'
" syntax
Plug 'pangloss/vim-javascript'
Plug 'mxw/vim-jsx'
Plug 'tikhomirov/vim-glsl'
Plug 'rgrinberg/vim-ocaml'
"colorschemes
Plug 'lifepillar/vim-solarized8'
"linting
Plug 'w0rp/ale'
" completion
Plug 'prabirshrestha/async.vim'
Plug 'prabirshrestha/asyncomplete.vim'
Plug 'prabirshrestha/asyncomplete-buffer.vim'
Plug 'yami-beta/asyncomplete-omni.vim'
Plug 'prabirshrestha/asyncomplete-file.vim'
Plug 'prabirshrestha/asyncomplete-flow.vim'
Plug 'prabirshrestha/vim-lsp'
Plug 'prabirshrestha/asyncomplete-lsp.vim'
Plug '~/vim-extras'
call plug#end()
if has('nvim')
set inccommand=split
set t_Co=256
set termguicolors
autocmd TermOpen * setlocal scrollback=10000
tnoremap <esc><esc> <c-\><c-n>
command! -nargs=+ ExecInSplit :vsplit | term "<q-args>"
endif
augroup PersonalFiletypeSettings
autocmd BufNewFile,BufRead * if expand('%:t') == 'Jenkinsfile' | setfiletype groovy | endif
augroup END
" ============= Functions =============== {{{
function! FindInParentDir(dir, fn)
if a:dir == '/'
return ''
endif
let l:path = a:dir . '/' . a:fn
return empty(glob(path)) ? FindInParentDir(fnamemodify(a:dir, ':h'), a:fn) : l:path
endfunction
function! StrTrim(txt)
return substitute(a:txt, '^\n*\s*\(.\{-}\)\n*\s*$', '\1', '')
endfunction
" }}}
" ============= Backups ====================== {{{
set backup
" Make Vim able to edit crontab files again.
set backupskip=/tmp/*,/private/tmp/*"
let s:myBackupDir = $VIMHOME.'/tmp/backup'
silent! call mkdir(expand(s:myBackupDir), "p")
let &backupdir = s:myBackupDir."//"
let s:mySwapDir = $VIMHOME.'/tmp/swap'
silent! call mkdir(expand(s:mySwapDir), "p")
let &directory = s:mySwapDir."//"
if has('persistent_undo')
let s:myUndoDir = $VIMHOME.'/tmp/undo'
silent! call mkdir(expand(s:myUndoDir), "p")
let &undodir = s:myUndoDir."//"
set undofile
endif
" }}}
" ============= Statusline =================== {{{
set noshowmode
function! InsertStatuslineColor(mode)
if a:mode == 'i' || a:mode == 'r'
redir => l:result
silent highlight statusline
redir END
let l:colors = matchlist(l:result, '\vguifg\=([^\s]+).*guibg\=([^\s]+)')[1:2]
let s:InsertStatuslineColor__old_guibg = l:colors[1]
highlight statusline guibg=#689acc
else
if exists("s:InsertStatuslineColor__old_guibg")
execute ":highlight statusline guibg=" . s:InsertStatuslineColor__old_guibg
unlet s:InsertStatuslineColor__old_guibg
endif
endif
endfunction
augroup InsertStatuslineColor
autocmd!
autocmd InsertEnter * call InsertStatuslineColor(v:insertmode)
autocmd InsertChange * call InsertStatuslineColor(v:insertmode)
autocmd InsertLeave * call InsertStatuslineColor('')
augroup END
" }}}
" ============= CTRL-P =========================== {{{
let g:ctrlp_match_func = { 'match': 'pymatcher#PyMatch' }
let g:ctrlp_match_window = 'bottom,order:btt,min:1,max:10,results:1000'
if executable('rg') " Use rip-grep if available https://github.com/BurntSushi/ripgrep
set grepprg=rg\ --vimgrep
let g:ctrlp_user_command = 'rg %s --files -g ""'
let g:ctrlp_use_caching = 0
elseif executable('ag') " Use if available The Silver Searcher https://github.com/ggreer/the_silver_searcher
set grepprg=ag\ --nogroup\ --nocolor
let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'
let g:ctrlp_use_caching = 0
endif
" }}}
" ============= Completion ======================= {{{
inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
inoremap <expr> <cr> pumvisible() ? "\<C-y>" : "\<cr>"
imap <c-space> <Plug>(asyncomplete_force_refresh)
let g:asyncomplete_remove_duplicates = 1
let g:loaded_sql_completion = 0 "Prevent SQLCompletion from complaining. And also from working...
augroup PersonalCompletion
autocmd!
autocmd User asyncomplete_setup call asyncomplete#register_source(asyncomplete#sources#flow#get_source_options({
\ 'name': 'flow',
\ 'whitelist': ['javascript', 'javascript.jsx'],
\ 'completor': function('asyncomplete#sources#flow#completor'),
\ 'priority': 1,
\ 'config': {
\ 'prefer_local': 1,
\ 'show_typeinfo': 1
\ },
\ }))
autocmd User asyncomplete_setup call asyncomplete#register_source(asyncomplete#sources#file#get_source_options({
\ 'name': 'file',
\ 'whitelist': ['*'],
\ 'priority': 10,
\ 'completor': function('asyncomplete#sources#file#completor')
\ }))
autocmd User asyncomplete_setup call asyncomplete#register_source(asyncomplete#sources#omni#get_source_options({
\ 'name': 'omni',
\ 'whitelist': ['*'],
\ 'blacklist': ['c', 'cpp', 'html', 'javascript', 'javascript.jsx'],
\ 'completor': function('asyncomplete#sources#omni#completor')
\ }))
if executable('pyls')
" pip install python-language-server
autocmd User lsp_setup call lsp#register_server({
\ 'name': 'pyls',
\ 'cmd': {server_info->['pyls']},
\ 'whitelist': ['python'],
\ })
endif
if executable('clangd')
" I am using a prebuild binary from
" https://storage.googleapis.com/vim-clangd/796005d4a195f37222d7f63dc38b7b8ffe8cef6d/clangd-download-index.json
" which I have installed in ~/bin. Check sometime if clangd version >6.0.1 have been released.
autocmd User lsp_setup call lsp#register_server({
\ 'name': 'clangd',
\ 'cmd': {server_info->['clangd']},
\ 'whitelist': ['c', 'cpp', 'objc', 'objcpp'],
\ })
endif
if executable('ocaml-language-server')
autocmd User lsp_setup call lsp#register_server({
\ 'name': 'ocaml-language-server',
\ 'cmd': {server_info->['ocaml-language-server', '--stdio']},
\ 'whitelist': ['ocaml'],
\ })
endif
augroup END
" }}}
" ===================== Linting =================== {{{
let g:ale_fixers = {
\ 'javascript': ['eslint'],
\}
let g:ale_linters = {
\ 'javascript': ['eslint', 'flow'],
\ 'c': ['clangtidy'],
\ 'ocaml': []
\}
let g:ale_fix_on_save = 1
let g:ale_completion_enabled = 0
function! s:ConfigurePylint()
let l:pylint_cfg = FindInParentDir(getcwd(), 'pylint.cfg')
if l:pylint_cfg != ''
let g:ale_python_pylint_options = '--rcfile=' . l:pylint_cfg
else
let g:ale_python_pylint_options = ''
endif
endfunction
augroup PersonalALESettings
autocmd FileType python call s:ConfigurePylint()
" }}}
" ============= Flow ============================== {{{
let g:javascript_plugin_flow = 1
function! FlowTypeAtPos()
let l:context = { 'stdout_buffered': 1 }
function! context.on_exit(job_id, exit_code, event)
let l:result = json_decode(join(self.stdout, ''))
if a:exit_code == 0
echo l:result['type']
else
echo l:result['exit']['msg']
endif
endfunction
let l:cursor_position = getpos('.')
let l:cmd = ['npx', 'flow', 'type-at-pos', '--json', '--quiet', '--timeout=1',
\ '--path=' . expand('%:p'),
\ l:cursor_position[1],
\ l:cursor_position[2]]
let l:id = jobstart(l:cmd, context)
call chansend(l:id, join(getline(1,'$'), "\n"))
call chanclose(l:id, 'stdin')
endfunction
command! FlowTypeAtPos :call FlowTypeAtPos()
function! FlowJumpToDef()
let l:context = { 'stdout_buffered': 1 }
function! context.on_exit(job_id, exit_code, event)
if len(self.stdout) < 2
echo "bad response / uncovered code"
return
endif
let l:result = json_decode(join(self.stdout, ''))
if v:shell_error != 0
echo l:result['exit']['msg']
return
endif
if l:result['path'] == ''
echo '<empty>'
return
endif
"work around stupid jumps into flow-typed for connected React components
if l:result['path'] =~ "/flow-typed/npm/react-redux"
execute "normal! gd"
return
endif
"set an entry in the jumplist
execute "normal m'"
"move the cursor (without modifying jumplist)
if l:result['path'] != expand('%:p')
keepjumps execute 'edit ' . l:result['path']
endif
call cursor(l:result['line'], l:result['start'])
endfunction
let l:cursor_position = getpos('.')
let l:cmd = ['npx', 'flow', 'get-def', '--json', '--quiet', '--timeout=1',
\ '--path=' . expand('%:p'),
\ l:cursor_position[1],
\ l:cursor_position[2]]
let l:id = jobstart(l:cmd, context)
call chansend(l:id, join(getline(1,'$'), "\n"))
call chanclose(l:id, 'stdin')
endfunction
command! FlowJumpToDef :call FlowJumpToDef()
function! FlowCheck()
let l:context = { 'stdout_buffered': 1 }
function! context.on_exit(job_id, exit_code, event)
try
let l:result = json_decode(join(self.stdout, ''))
catch
echom "Error running Flow!!!"
return
endtry
let l:s = ''
if has_key(l:result, 'errors') && (len(l:result['errors']) > 0)
for l:err in l:result['errors']
let l:location = l:err['message'][0]['loc']
let l:start = l:location['start']
let l:message = l:err['message'][0]['descr']
let l:s .= join([l:location['source'], l:start['line'], l:start['column'], l:message], ':') . "\n"
endfor
endif
let l:old_fmt = &errorformat
let &errorformat = '%f:%l:%c:%m'
cgetexpr l:s
botright cwindow
let &errorformat = old_fmt
endfunction
call jobstart(['npx', 'flow', '--json', '--quiet', '--timeout=1'], context)
endfunction
command! FlowCheck :call FlowCheck()
augroup PersonalFlowMappings
autocmd!
autocmd FileType javascript,jsx setlocal omnifunc=<cr>
autocmd FileType javascript,jsx nnoremap <buffer> gd :FlowJumpToDef<cr>
autocmd FileType javascript,jsx nnoremap <buffer> K :FlowTypeAtPos<cr>
autocmd BufWritePost *.js FlowCheck
augroup END
" }}}
" ============== OCAML / Merlin ==================== {{{
""
let g:opamshare = substitute(system('opam config var share'),'\n$','','''')
execute "set rtp+=" . g:opamshare . "/merlin/vim"
let g:merlin_disable_default_keybindings = 1
let g:merlin_split_method = "never"
let g:merlin_textobject_grow = 'm'
let g:merlin_textobject_shrink = 'M'
augroup PersonalMerlinSettings
autocmd!
autocmd FileType ocaml setlocal shiftwidth=2
autocmd FileType ocaml setlocal tabstop=2
autocmd Filetype ocaml nnoremap <buffer> gd :MerlinLocate<cr>
autocmd Filetype ocaml nmap <buffer> K :MerlinTypeOf<cr>
autocmd Filetype ocaml nmap <buffer> <f1> :MerlinClearEnclosing<cr>:nohlsearch<cr>
autocmd BufWritePost *.ml MerlinErrorCheck
" The `'cm_refresh_patterns'` is PCRE.
autocmd User CmSetup call cm#register_source({'name' : 'cm-ocaml',
\ 'priority': 1,
\ 'scoping': 1,
\ 'scopes': ['ocaml'],
\ 'abbreviation': 'ocaml',
\ 'cm_refresh_patterns':[':\s+\w*$'],
\ 'cm_refresh': {'omnifunc': 'merlin#Complete'},
\ })
augroup END
" }}}
" =================== LSP Settings ====================== {{{
augroup PersonalLSPSettings
autocmd!
autocmd Filetype python,c nnoremap <buffer> gd :LspDefinition<cr>
autocmd Filetype python,c nnoremap <buffer> K :LspHover<cr>
augroup END
" }}}
" ============= VIM-Commentary ==================== {{{
augroup PersonalVimCommentary
autocmd!
autocmd Filetype cpp setlocal commentstring=//\ %s
augroup END
" }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment