.vimrc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. " essentials
  2. set ruler
  3. set mouse=a
  4. set nocompatible
  5. set bs=2
  6. set nowrap
  7. " give me all of teh colors!!
  8. set t_Co=256
  9. set bg=dark
  10. syntax on
  11. " misc
  12. set path+=** " make :find tab-completion search subdirectories
  13. set wildmenu " show tab-complete menu for : commands
  14. " search
  15. set ignorecase " case insensitive search
  16. set smartcase " auto switch to case-sensitive if uppercase *is* used
  17. set incsearch " live incremental searching
  18. set showmatch " show matches live while typing
  19. set hlsearch " highlight matches
  20. " tabs
  21. set shiftwidth=4
  22. set tabstop=4
  23. set softtabstop=4
  24. " tab based indention behaviors
  25. vmap <Tab> >
  26. nmap <Tab> >>
  27. vmap <S-Tab> <
  28. nmap <S-Tab> <<
  29. imap <S-Tab> <Esc><<i
  30. " folds
  31. set foldmethod=syntax
  32. set foldlevelstart=99
  33. " map Alt+Right/Left arrow to movements
  34. nmap <Esc>f <Esc>e
  35. nmap <Esc>b <Esc>b
  36. imap <Esc>f <Esc>lei
  37. imap <Esc>b <Esc>bi
  38. " simplify working with the system clipboard
  39. vmap <C-c> "+y
  40. nmap <C-c> V"+y
  41. vmap <C-x> "+x
  42. nmap <C-x> V"+x
  43. imap <C-v> <Esc>"+Pi
  44. " no more ex mode!
  45. nmap Q <nop>
  46. " force syntax on a few file types
  47. filetype plugin indent on
  48. "autocmd BufNewFile,BufReadPost *.ts set filetype=typescript syntax=typescript
  49. "autocmd BufNewFile,BufReadPost *.tsx set filetype=typescript syntax=typescriptreact
  50. "autocmd BufNewFile,BufReadPost *.json set filetype=json syntax=javascript
  51. "autocmd BufNewFile,BufReadPost *.md set filetype=markdown
  52. "autocmd BufNewFile,BufReadPost *.go set filetype=go
  53. " force syntax for fenced code blocks in markdown
  54. let g:markdown_fenced_languages = [
  55. \ 'sh',
  56. \ 'bash=sh',
  57. \ 'zsh',
  58. \ 'help',
  59. \ 'log=apache',
  60. \ 'json=javascript',
  61. \ 'javascript=typescript',
  62. \ 'js=typescript',
  63. \ 'jsx=typescriptreact',
  64. \ 'ts=typescript',
  65. \ 'tsx=typescriptreact',
  66. \ 'html',
  67. \ 'css',
  68. \ 'mermaid',
  69. \ 'swift',
  70. \ 'rust',
  71. \ 'zig',
  72. \ 'cpp',
  73. \ 'c',
  74. \ 'py=python',
  75. \ 'go',
  76. \ 'ruby',
  77. \ 'clojure',
  78. \ ]
  79. " formatters
  80. "TODO: failing on simple JSON files...
  81. autocmd FileType * set formatprg=bunx\ -q\ prettier\ --stdin-filepath\ % | let $F=&formatprg | set equalprg=$F
  82. autocmd FileType rust set formatprg=rustfmt | let $F=&formatprg | set equalprg=$F
  83. " auto-format on save
  84. "autocmd BufWritePre * normal gg=G''
  85. " compilers/build tools
  86. "autocmd FileType rust compiler cargo | cabbrev cargo make | command Cargo make
  87. " prefer ripgrep if available
  88. if executable('rg')
  89. set grepprg=rg\ --vimgrep\ $*
  90. set grepformat^=%f:%l:%c:%m
  91. " add a silent Grep and use that instead
  92. command! -nargs=+ -complete=file Grep execute 'silent grep! <args>' | redraw! | copen
  93. endif
  94. " VIM-PLUG PLUGINS (WITH FIRST-RUN AUTOINSTALL)
  95. if empty(glob('~/.vim/autoload/plug.vim'))
  96. silent !D="$HOME/.vim/autoload"; mkdir -p "$D"; U="https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim"; F="$D/plug.vim"; curl -fLo "$F" "$U" 2>/dev/null || wget -qLO "$F" "$U"
  97. autocmd VimEnter * PlugInstall
  98. endif
  99. call plug#begin()
  100. Plug 'tomasiser/vim-code-dark' " VSCode Dark+ theme
  101. Plug 'tpope/vim-commentary' " comment stuff out
  102. Plug 'vim-airline/vim-airline' " more helpful tab line and status lines with colors
  103. let g:airline#extensions#tabline#enabled = !has('gui_running')
  104. let g:airline#extensions#tabline#show_buffers = 0
  105. set laststatus=2
  106. set ttimeoutlen=50
  107. Plug 'editorconfig/editorconfig-vim'
  108. Plug 'vim-syntastic/syntastic' " multi-language syntax checker
  109. let g:syntastic_check_on_open=1
  110. let g:syntastic_aggregate_errors=1
  111. let g:syntastic_javascript_checkers = ['eslint']
  112. Plug 'ntpeters/vim-better-whitespace' " whitespace!!
  113. let g:strip_whitespace_on_save = 1
  114. Plug 'nathanaelkane/vim-indent-guides' " pretty indent guides with softtabs
  115. let g:indent_guides_enable_on_vim_startup = 1
  116. let g:indent_guides_guide_size = 1
  117. let g:indent_guides_auto_colors = 1
  118. Plug 'lilydjwg/colorizer' " colorize CSS inline
  119. Plug 'ctrlpvim/ctrlp.vim' " fuzzy file matcher / opener
  120. let ctrlp_user_cmd_fallback = 'find %s -type f'
  121. if executable('rg') | let ctrlp_user_cmd_fallback = 'rg --files %s' | endif
  122. let g:ctrlp_user_command = ['.git', 'cd %s && git ls-files . -co --exclude-standard', ctrlp_user_cmd_fallback]
  123. Plug 'tpope/vim-fugitive' " git commands and statusline
  124. Plug 'airblade/vim-gitgutter' " git changes in gutter
  125. "# COC SETUP
  126. if executable('node')
  127. Plug 'neoclide/coc.nvim', {'branch': 'release'} " amazing code completions
  128. let g:coc_global_extensions = [
  129. \ 'coc-git',
  130. \ 'coc-json',
  131. \ 'coc-tsserver',
  132. \ 'coc-eslint',
  133. \ 'coc-biome',
  134. \ 'coc-prettier',
  135. \ 'coc-java',
  136. \ ]
  137. if executable('zls') | call add(g:coc_global_extensions, 'coc-zig') | endif
  138. set encoding=utf-8
  139. set updatetime=300 " helps make some UI interactions more responsive
  140. set signcolumn=yes " always enable to avoid UI shifting on init
  141. " Use tab for trigger completion with characters ahead and navigate
  142. " NOTE: There's always complete item selected by default, you may want to enable
  143. " no select by `"suggest.noselect": true` in your configuration file
  144. " NOTE: Use command ':verbose imap <tab>' to make sure tab is not mapped by
  145. " other plugin before putting this into your config
  146. inoremap <silent><expr> <TAB>
  147. \ coc#pum#visible() ? coc#pum#next(1) :
  148. \ CheckBackspace() ? "\<Tab>" :
  149. \ coc#refresh()
  150. inoremap <expr><S-TAB> coc#pum#visible() ? coc#pum#prev(1) : "\<C-h>"
  151. " Make <CR> to accept selected completion item or notify coc.nvim to format
  152. " <C-g>u breaks current undo, please make your own choice
  153. inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm()
  154. \ : "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
  155. function! CheckBackspace() abort
  156. let col = col('.') - 1
  157. return !col || getline('.')[col - 1] =~# '\s'
  158. endfunction
  159. " Use <c-space> to trigger completion
  160. if has('nvim')
  161. inoremap <silent><expr> <c-space> coc#refresh()
  162. else
  163. inoremap <silent><expr> <c-@> coc#refresh()
  164. endif
  165. " Use `[g` and `]g` to navigate diagnostics
  166. " Use `:CocDiagnostics` to get all diagnostics of current buffer in location list
  167. nmap <silent> [g <Plug>(coc-diagnostic-prev)
  168. nmap <silent> ]g <Plug>(coc-diagnostic-next)
  169. " GoTo code navigation
  170. nmap <silent> gd <Plug>(coc-definition)
  171. nmap <silent> gy <Plug>(coc-type-definition)
  172. nmap <silent> gi <Plug>(coc-implementation)
  173. nmap <silent> gr <Plug>(coc-references)
  174. " Use K to show documentation in preview window
  175. nnoremap <silent> K :call ShowDocumentation()<CR>
  176. function! ShowDocumentation()
  177. if CocAction('hasProvider', 'hover')
  178. call CocActionAsync('doHover')
  179. else
  180. call feedkeys('K', 'in')
  181. endif
  182. endfunction
  183. " Highlight the symbol and its references when holding the cursor
  184. autocmd CursorHold * silent call CocActionAsync('highlight')
  185. " Symbol renaming
  186. nmap <leader>rn <Plug>(coc-rename)
  187. " Formatting selected code
  188. xmap <leader>f <Plug>(coc-format-selected)
  189. nmap <leader>f <Plug>(coc-format-selected)
  190. augroup mygroup
  191. autocmd!
  192. " Setup formatexpr specified filetype(s)
  193. autocmd FileType typescript,javascript,json setl formatexpr=CocAction('formatSelected')
  194. augroup end
  195. " Applying code actions to the selected code block
  196. " Example: `<leader>aap` for current paragraph
  197. xmap <leader>a <Plug>(coc-codeaction-selected)
  198. nmap <leader>a <Plug>(coc-codeaction-selected)
  199. " Remap keys for applying code actions at the cursor position
  200. nmap <leader>ac <Plug>(coc-codeaction-cursor)
  201. " Remap keys for apply code actions affect whole buffer
  202. nmap <leader>as <Plug>(coc-codeaction-source)
  203. " Apply the most preferred quickfix action to fix diagnostic on the current line
  204. nmap <leader>qf <Plug>(coc-fix-current)
  205. " Remap keys for applying refactor code actions
  206. nmap <silent> <leader>re <Plug>(coc-codeaction-refactor)
  207. xmap <silent> <leader>r <Plug>(coc-codeaction-refactor-selected)
  208. nmap <silent> <leader>r <Plug>(coc-codeaction-refactor-selected)
  209. " Run the Code Lens action on the current line
  210. nmap <leader>cl <Plug>(coc-codelens-action)
  211. " Map function and class text objects
  212. " NOTE: Requires 'textDocument.documentSymbol' support from the language server
  213. xmap if <Plug>(coc-funcobj-i)
  214. omap if <Plug>(coc-funcobj-i)
  215. xmap af <Plug>(coc-funcobj-a)
  216. omap af <Plug>(coc-funcobj-a)
  217. xmap ic <Plug>(coc-classobj-i)
  218. omap ic <Plug>(coc-classobj-i)
  219. xmap ac <Plug>(coc-classobj-a)
  220. omap ac <Plug>(coc-classobj-a)
  221. " Remap <C-f> and <C-b> to scroll float windows/popups
  222. if has('nvim-0.4.0') || has('patch-8.2.0750')
  223. nnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
  224. nnoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
  225. inoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(1)\<cr>" : "\<Right>"
  226. inoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(0)\<cr>" : "\<Left>"
  227. vnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
  228. vnoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
  229. endif
  230. " Use CTRL-S for selections ranges
  231. " Requires 'textDocument/selectionRange' support of language server
  232. nmap <silent> <C-s> <Plug>(coc-range-select)
  233. xmap <silent> <C-s> <Plug>(coc-range-select)
  234. " Add `:Format` command to format current buffer
  235. command! -nargs=0 Format :call CocActionAsync('format')
  236. " Add `:Fold` command to fold current buffer
  237. command! -nargs=? Fold :call CocAction('fold', <f-args>)
  238. " Add `:OR` command for organize imports of the current buffer
  239. command! -nargs=0 OR :call CocActionAsync('runCommand', 'editor.action.organizeImport')
  240. " statusline support
  241. let g:airline#extensions#coc#enabled = 1
  242. " Mappings for CoCList
  243. " Show all diagnostics
  244. nnoremap <silent><nowait> <space>a :<C-u>CocList diagnostics<cr>
  245. " Manage extensions
  246. nnoremap <silent><nowait> <space>e :<C-u>CocList extensions<cr>
  247. " Show commands
  248. nnoremap <silent><nowait> <space>c :<C-u>CocList commands<cr>
  249. " Find symbol of current document
  250. nnoremap <silent><nowait> <space>o :<C-u>CocList outline<cr>
  251. " Search workspace symbols
  252. nnoremap <silent><nowait> <space>s :<C-u>CocList -I symbols<cr>
  253. " Do default action for next item
  254. nnoremap <silent><nowait> <space>j :<C-u>CocNext<CR>
  255. " Do default action for previous item
  256. nnoremap <silent><nowait> <space>k :<C-u>CocPrev<CR>
  257. " Resume latest coc list
  258. nnoremap <silent><nowait> <space>p :<C-u>CocListResume<CR>
  259. "# COC SETUP END
  260. endif
  261. Plug 'rust-lang/rust.vim'
  262. call plug#end()
  263. colorscheme codedark