Last active
January 25, 2021 19:39
-
-
Save edrpls/19536870382e0b63e62c237701215d29 to your computer and use it in GitHub Desktop.
Jan 2021 NeoVim Config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set termguicolors " use terminal colors | |
set number " show line number | |
set ruler " show line and column number of cursor | |
set relativenumber " show lines relative to cursor | |
set cursorline " show a line where the cursor is | |
set clipboard^=unnamed,unnamedplus " use the same clipboard as system | |
" from https://github.com/neoclide/coc.nvim | |
set hidden " hide buffers when abandoned | |
" prevent tsserver from recompiling project on save | |
" https://github.com/neoclide/coc.nvim/issues/649 | |
set nobackup | |
set nowritebackup | |
set cmdheight=2 " Give more space for displaying messages. | |
" Having longer updatetime (default is 4000 ms = 4 s) leads to noticeable | |
" delays and poor user experience. | |
set updatetime=300 | |
" Don't pass messages to |ins-completion-menu|. | |
set shortmess+=c | |
command! -nargs=0 Prettier :CocCommand prettier.formatFile " create Prettier command on start | |
" fzf configuration | |
let $FZF_DEFAULT_COMMAND = 'fd -t f -H' | |
let $FZF_DEFAULT_OPTS = '--border --reverse --inline-info' | |
let g:fzf_layout = { 'window': 'FzfFloatingCenteredWindow' } | |
let g:fzf_buffers_jump = 1 | |
nnoremap <silent> <leader><leader> :Files<CR> | |
nnoremap <M-l> :Lines <C-R><C-w><CR> | |
" refine quickfix results | |
command! RefineQuickfix call fzf#run({ | |
\ 'source': map(getqflist(), function('<sid>qf_to_fzf')), | |
\ 'down': '20', | |
\ 'sink*': function('<sid>fzf_to_qf'), | |
\ 'options': '--multi --bind=ctrl-a:select-all,ctrl-d:deselect-all --prompt "quickfix> "', | |
\ 'window': 'FzfQuickfixFloatingWindow', | |
\ }) | |
command! FzfFloatingCenteredWindow call s:fzf_floating_window({ | |
\ 'width': 150, | |
\ 'height': 20, | |
\ 'relative': 'editor', | |
\ 'anchor': 'SW', | |
\ 'row': (&lines/2) + 10, | |
\ 'col': (&columns/2) - 75 | |
\}) | |
" Matches the height of the quickfix window and places the floating window at | |
" the bottom of the screen. | |
" The idea is to make it look like the quickfix window itself is being edited | |
command! FzfQuickfixFloatingWindow call s:fzf_floating_window({ | |
\ 'height': min([20, len(getqflist())]) + 1, | |
\ 'width': &columns, | |
\ 'relative': 'editor', | |
\ 'anchor': 'SW', | |
\ 'row': &lines, | |
\ 'col': 0, | |
\}) | |
command! -nargs=* Find call fzf#run({ | |
\ 'source': printf('rg -a --vimgrep --color always "%s"', | |
\ escape(empty(<q-args>) ? '^(?=.)' : <q-args>, '"\')), | |
\ 'options': '--ansi --expect=ctrl-t,ctrl-v,ctrl-x --delimiter : --nth 4.. '. | |
\ '--multi --bind=ctrl-a:select-all,ctrl-d:deselect-all '. | |
\ '--color hl:68,hl+:110 -x', | |
\ 'sink*': function('<sid>rg_handler'), | |
\ 'down': '50%', | |
\ }) | |
" Find string inside the CWD | |
nnoremap <leader>p/ :silent! Find <C-R><C-W><CR> | |
vnoremap <leader>p/ y:silent Find <C-R>"<CR> | |
" helpers | |
" auto-adjust window height to a max of N lines | |
function! AdjustWindowHeight(minheight, ...) | |
exe max([min([line("$"), (a:0 >= 1) ? a:1 : a:minheight]), a:minheight]) . "wincmd _" | |
endfunction | |
augroup WindowSizing | |
au! | |
au FileType qf call AdjustWindowHeight(1, 20) | |
augroup END | |
function! s:rg_handler(lines) | |
if len(a:lines) < 2 | return | endif | |
let cmd = get({ 'ctrl-x': 'split' | |
\,'ctrl-v': 'vertical split' | |
\,'ctrl-t': 'tabe' | |
\ } , a:lines[0], 'e' ) | |
let list = map(a:lines[1:], 's:ag_to_qf(v:val)') | |
let first = list[0] | |
execute cmd escape(first.filename, ' %#\') | |
execute first.lnum | |
execute 'normal!' first.col.'|zz' | |
if len(list) > 1 | |
call setqflist(list) | |
copen | |
endif | |
endfunction | |
function! s:qf_to_fzf(key, line) abort | |
let l:filepath = expand('#' . a:line.bufnr . ':p') | |
return l:filepath . ':' . a:line.lnum . ':' . a:line.col . ':' . a:line.text | |
endfunction | |
function! s:fzf_to_qf(filtered_list) abort | |
let list = map(a:filtered_list, 's:ag_to_qf(v:val)') | |
if len(list) > 1 | |
call setqflist(list) | |
copen | |
wincmd p | |
endif | |
endfunction | |
" ({ 'width': number, | |
" 'height': number, | |
" 'message': array<string>, | |
" 'relative': 'editor' | 'cursor', | |
" 'anchor': 'NW' | 'NE' | 'SW' | 'SE', | |
" 'enter': 0 | 1, | |
" 'centered': boolean }) -> window_id | |
function! s:fzf_floating_window(opts) abort | |
let l:buff = nvim_create_buf(v:false, v:true) | |
let l:message = get(a:opts, 'message', []) | |
" Window size | |
" TODO: get the width from the longest line in l:message | |
let l:width = get(a:opts, 'width', 150) | |
let l:height = max([get(a:opts, 'height', 1), len(l:message)]) | |
let l:col = get(a:opts, 'col', 1) | |
let l:row = get(a:opts, 'row', 0) | |
let l:anchor = get(a:opts, 'anchor', 'SW') | |
let l:relative = get(a:opts, 'relative', 'cursor') | |
let l:win_opts = { | |
\ 'relative': l:relative, | |
\ 'anchor': l:anchor, | |
\ 'col': l:col, | |
\ 'row': l:row, | |
\ 'width': l:width, | |
\ 'height': l:height, | |
\ 'style': 'minimal' | |
\ } | |
let l:enter = get(a:opts, 'enter', 0) | |
if len(l:message) > 0 | |
call nvim_buf_set_lines(l:buff, 0, -1, v:true, l:message) | |
else | |
" force the enter option if we're operating on the current buffer | |
let l:enter = 1 | |
endif | |
" Window position | |
if get(a:opts, 'centered', v:false) | |
let l:win_opts.relative = 'editor' | |
let l:win_opts.anchor = 'NW' | |
let l:win_opts.col = (&columns/2) - (l:width/2) | |
let l:win_opts.row = (&lines/2) - (l:height/2) | |
endif | |
" Window id | |
let l:win_id = nvim_open_win(l:buff, l:enter, l:win_opts) | |
doautocmd User FloatingWindowEnter | |
return l:win_id | |
endfunction | |
function! s:ag_to_qf(line) | |
let parts = split(a:line, ':') | |
return { 'filename': parts[0], | |
\ 'lnum': parts[1], | |
\ 'col': parts[2], | |
\ 'text': join(parts[3:], ':') | |
\ } | |
endfunction | |
" auto-reload | |
augroup VimConfig | |
au! | |
au BufWritePost ~/.config/nvim/init.vim so <afile> | |
augroup END | |
" preview replace changes in split window | |
set inccommand=split | |
nmap <bs> :<c-u>TmuxNavigateLeft<cr> | |
"Terminal Mappings | |
"https://neovim.io/doc/user/nvim_terminal_emulator.html | |
tnoremap <A-h> <C-\><C-n><C-w>h | |
tnoremap <A-j> <C-\><C-n><C-w>j | |
tnoremap <A-k> <C-\><C-n><C-w>k | |
tnoremap <A-l> <C-\><C-n><C-w>l | |
nnoremap <A-h> <C-w>h | |
nnoremap <A-j> <C-w>j | |
nnoremap <A-k> <C-w>k | |
nnoremap <A-l> <C-w>l | |
" Conqueror of Completion | |
let g:coc_global_extensions = ['coc-eslint', 'coc-json', 'coc-tsserver', 'coc-html', 'coc-snippets', 'coc-ultisnips', 'coc-pairs', 'coc-prettier', 'coc-emoji'] | |
let g:airline_section_error = '%{airline#util#wrap(airline#extensions#coc#get_error(),0)}' | |
let g:airline_section_warning = '%{airline#util#wrap(airline#extensions#coc#get_warning(),0)}' | |
nmap <silent> <M-h> :call CocActionAsync('doHover')<CR> | |
nmap <silent> <M-d> <Plug>(coc-definition) | |
nmap <silent> <M-t> <Plug>(coc-type-definition) | |
nmap <silent> <M-i> <Plug>(coc-implementation) | |
nmap <silent> <M-f> <Plug>(coc-references) | |
nmap <silent> <M-r> <Plug>(coc-rename) | |
nmap <silent> <M-x> :CocCommand<CR> | |
" Open terminal | |
nnoremap <leader>ot :bot pedit +terminal<CR><C-W><C-j>A | |
au BufRead,BufNewFile *.mdx set filetype=markdown " add syntax highlighting to .mdx | |
" Whitespace stuff | |
set tabstop=8 | |
set shiftwidth=4 | |
set softtabstop=0 | |
set softtabstop=0 | |
set expandtab | |
set smarttab | |
" Wordwrap | |
set wrap | |
set linebreak | |
set nolist " list disables linebreak | |
set textwidth=0 | |
set wrapmargin=0 | |
"Folders | |
set fdm=marker | |
" Searching | |
set hlsearch | |
set incsearch | |
set ignorecase | |
set smartcase | |
" Tab completion | |
set wildmode=list:longest,list:full | |
set wildignore+=*.o,*.obj,.git,*.rbc,*.class,.svn,vendor/gems/* | |
" Status bar | |
set laststatus=2 | |
" make userreal tabs | |
au FileType make set noexpandtab | |
" make ack.vim use ag | |
if executable('ag') | |
let g:ackprg = 'ag --vimgrep' | |
endif | |
" Remember last location in file | |
if has("autocmd") | |
au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | |
\| exe "normal g'\"" | endif | |
endif | |
"fzf regular search | |
set rtp+=~/.fzf | |
let $FZF_DEFAULT_COMMAND = 'ag -l --nocolor -g "" | grep -v ".*(ico|png|jpe?g|gif|svg|ttf|otf|eot|woff|map|dat)\$"' | |
nnoremap <silent> <C-p> :FZF<cr> | |
" [Files] File preview | |
let g:fzf_files_options = '--preview "highlight -O xterm256 -s moria {} 2> /dev/null; or cat {} | head -'.&lines.'"' | |
command! FZFLines call fzf#run({ | |
\ 'source': <sid>buffer_lines(), | |
\ 'sink': function('<sid>line_handler'), | |
\ 'options': '--extended --nth=3..', | |
\ 'down': '60%' | |
\}) | |
nmap <leader>/ :FZFLines<CR> | |
" bb opens fzf Buffers | |
nnoremap <leader>bb :Buffers<CR> | |
"Airline | |
" vim-airline | |
let g:airline_powerline_fonts = 1 | |
let g:airline_theme="dracula" | |
let g:airline#extensions#tabline#enabled = 1 | |
if !exists('g:airline_symbols') | |
let g:airline_symbols = {} | |
endif | |
" let g:airline_symbols.space = '\ua0' | |
let g:airline_left_sep='' | |
let g:airline_right_sep='' | |
let g:airline#extensions#tabline#left_sep = ' ' | |
let g:airline#extensions#tabline#left_alt_sep = '|' | |
" search on open buffers | |
function! s:line_handler(l) | |
let keys = split(a:l, ':\t') | |
exec 'buf' keys[0] | |
exec keys[1] | |
normal! ^zz | |
endfunction | |
function! s:buffer_lines() | |
let res = [] | |
for b in filter(range(1, bufnr('$')), 'buflisted(v:val)') | |
call extend(res, map(getbufline(b,0,"$"), 'b . ":\t" . (v:key + 1) . ":\t" . v:val ')) | |
endfor | |
return res | |
endfunction | |
"No ctrl+w while changing split Not working :( | |
nnoremap <C-J> <C-W><C-J> | |
nnoremap <C-K> <C-W><C-K> | |
nnoremap <C-L> <C-W><C-L> | |
nnoremap <C-H> <C-W><C-H> | |
"UndoTree | |
if has("persistent_undo") | |
set undodir=~/.vim/undodir/ | |
set undofile | |
endif | |
" coc.nvim | |
autocmd FileType json syntax match Comment +\/\/.\+$+ " correct comment highlight | |
" use <tab> for trigger completion and navigate to the next complete item | |
function! s:check_back_space() abort | |
let col = col('.') - 1 | |
return !col || getline('.')[col - 1] =~ '\s' | |
endfunction | |
inoremap <silent><expr> <Tab> | |
\ pumvisible() ? "\<C-n>" : | |
\ <SID>check_back_space() ? "\<Tab>" : | |
\ coc#refresh() | |
"Use <Tab> and <S-Tab> to navigate the completion list: | |
inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>" | |
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>" | |
" coc-snippets | |
" Use <C-l> for trigger snippet expand. | |
imap <C-l> <Plug>(coc-snippets-expand) | |
" Use <C-j> for select text for visual placeholder of snippet. | |
vmap <C-j> <Plug>(coc-snippets-select) | |
" Use <C-j> for jump to next placeholder, it's default of coc.nvim | |
let g:coc_snippet_next = '<c-j>' | |
" Use <C-k> for jump to previous placeholder, it's default of coc.nvim | |
let g:coc_snippet_prev = '<c-k>' | |
" Use <C-j> for both expand and jump (make expand higher priority.) | |
imap <C-j> <Plug>(coc-snippets-expand-jump) | |
" Use <leader>x for convert visual selected code to snippet | |
xmap <leader>x <Plug>(coc-convert-snippet) | |
"vim-plug | |
call plug#begin('~/.config/nvim/plugged') | |
" Make sure you use single quotes | |
" Plugin outside ~/.vim/plugged with post-update hook | |
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' } | |
Plug 'junegunn/fzf.vim' | |
Plug 'junegunn/vim-peekaboo' | |
Plug 'tpope/vim-fugitive' | |
Plug 'tpope/vim-surround' | |
Plug 'tpope/vim-unimpaired' | |
Plug 'tpope/vim-repeat' | |
Plug 'tpope/vim-eunuch' | |
Plug 'tpope/vim-commentary' | |
Plug 'mileszs/ack.vim' | |
Plug 'kshenoy/vim-signature' | |
Plug 'vim-airline/vim-airline' | |
Plug 'vim-airline/vim-airline-themes' | |
Plug 'mbbill/undotree' | |
Plug 'christoomey/vim-tmux-navigator' | |
Plug 'christoomey/vim-tmux-runner' | |
Plug 'vim-scripts/Tabmerge' | |
Plug 'leafgarland/typescript-vim' | |
Plug 'mg979/vim-visual-multi', {'branch': 'master'} | |
Plug 'yuezk/vim-js' | |
Plug 'maxmellon/vim-jsx-pretty' | |
Plug 'machakann/vim-highlightedyank' | |
Plug 'neoclide/coc.nvim', {'branch': 'release'} | |
Plug 'dracula/vim', { 'as': 'dracula' } | |
Plug 'honza/vim-snippets' | |
Plug 'airblade/vim-gitgutter' | |
Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'} | |
function! DoRemote(arg) | |
UpdateRemotePlugins | |
endfunction | |
" Add plugins to &runtimepath | |
call plug#end() | |
set background=dark | |
colorscheme dracula | |
lua require('tree-sitter-config') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment