.vimrc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. " Plugin manager
  2. if empty(glob('~/.vim/autoload/plug.vim'))
  3. silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
  4. \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  5. autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
  6. endif
  7. " Plugins
  8. call plug#begin('~/.vim/plugged')
  9. Plug 'preservim/nerdtree'
  10. Plug 'tpope/vim-fugitive'
  11. Plug 'tpope/vim-commentary'
  12. Plug 'junegunn/vim-peekaboo'
  13. Plug 'itchyny/lightline.vim'
  14. Plug 'dikiaap/minimalist'
  15. Plug 'kaicataldo/material.vim'
  16. Plug 'dense-analysis/ale'
  17. Plug 'maximbaz/lightline-ale'
  18. Plug 'preservim/tagbar'
  19. Plug 'vim-php/tagbar-phpctags.vim'
  20. Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
  21. Plug 'junegunn/fzf.vim'
  22. Plug 'airblade/vim-gitgutter'
  23. Plug 'mxw/vim-jsx'
  24. Plug 'pangloss/vim-javascript'
  25. Plug 'natebosch/vim-lsc'
  26. Plug 'tpope/vim-surround'
  27. call plug#end()
  28. filetype plugin indent on
  29. syntax on
  30. set autoindent
  31. set backspace=2
  32. set background=dark
  33. set completeopt=menu,popup,noselect
  34. set cursorcolumn
  35. set cursorline
  36. set expandtab
  37. set history=1000
  38. set hlsearch
  39. set incsearch
  40. set nowrap
  41. set number
  42. set numberwidth=4
  43. set pastetoggle=<F12> "Press <F12> when paste-alot
  44. set preserveindent
  45. set ruler
  46. set shiftround
  47. set shiftwidth=2
  48. set shortmess=atI
  49. set showcmd
  50. set showmatch
  51. set smartindent
  52. set smarttab
  53. set splitright
  54. set switchbuf+=usetab,newtab
  55. set termguicolors
  56. set tabstop=2
  57. set whichwrap+=<,>,[,],h,l
  58. set undofile
  59. set undodir=~/.vim/undo
  60. " Colorscheme
  61. colorscheme minimalist
  62. highlight Comment cterm=italic
  63. " Lightline
  64. set laststatus=2
  65. set noshowmode
  66. " Source lightline theme
  67. if empty(glob('~/.vim/source/minimalist.vim'))
  68. silent !curl -fLO --create-dirs --output-dir ~/.vim/source https://public.viktorgrahn.com/minimalist.vim
  69. endif
  70. source ~/.vim/source/minimalist.vim
  71. let g:lightline = {
  72. \ 'colorscheme': 'minimalist',
  73. \ 'tabline_subseparator': { 'left': '', 'right': '' },
  74. \ 'active': {
  75. \ 'left': [
  76. \ [ 'mode', 'paste' ],
  77. \ [ 'filestate' ],
  78. \ [ 'gitbranch', 'tagbar' ],
  79. \ ],
  80. \ 'right': [
  81. \ [ 'linter_checking', 'linter_errors', 'linter_warnings', 'linter_infos', 'linter_ok' ],
  82. \ [ 'fileformat', 'fileencoding', 'filetype', 'percent', 'lineinfo', 'offset' ],
  83. \ ]
  84. \ },
  85. \ 'tabline': {
  86. \ 'left': [ [ 'tabs' ] ],
  87. \ 'right': [ ],
  88. \ },
  89. \ 'component': {
  90. \ 'tagbar': '%{tagbar#currenttag("%s", "", "f", "nearest-stl")}',
  91. \ },
  92. \ 'component_function': {
  93. \ 'filestate': 'LightlineFileState',
  94. \ 'offset': 'LightlineFileOffset',
  95. \ },
  96. \ 'component_expand': {
  97. \ 'gitbranch': 'FugitiveHead',
  98. \ 'linter_checking': 'lightline#ale#checking',
  99. \ 'linter_infos': 'lightline#ale#infos',
  100. \ 'linter_warnings': 'lightline#ale#warnings',
  101. \ 'linter_errors': 'lightline#ale#errors',
  102. \ 'linter_ok': 'lightline#ale#ok',
  103. \ },
  104. \ 'component_type': {
  105. \ 'linter_warnings': 'warning',
  106. \ 'linter_errors': 'error',
  107. \ 'linter_ok': 'ok',
  108. \ 'linter_infos': 'info',
  109. \ }
  110. \ }
  111. " Lightline helper (concatenate readonly state, filename and modified state)
  112. function! LightlineFileState()
  113. if @% == "" | return "[No name]" | endif
  114. let s = expand('%:t')
  115. if &modified | let s = s . "+" | endif
  116. if &readonly | let s = "[RO] " . s | endif
  117. return s
  118. endfunction
  119. " Lightline helper (get cursor line and character position in file)
  120. function! LightlineFileOffset()
  121. return line2byte(line('.')) + col('.') - 1
  122. endfunction
  123. " NERDtree
  124. let NERDTreeQuitOnOpen=1
  125. let NERDTreeShowHidden=1
  126. " NERDTree helper (toggle in current buffer)
  127. function! NERDTreeToggleCustom()
  128. if (exists("t:NERDTreeBufName") && bufwinnr(t:NERDTreeBufName) != -1)
  129. exe ":NERDTreeClose"
  130. elseif bufname('%') != ""
  131. exe ":NERDTreeFind"
  132. else
  133. exe ":NERDTreeCWD"
  134. endif
  135. endfunction
  136. " NERDTREE Toggle NERDTree on <space>-o
  137. map <Space>o :call NERDTreeToggleCustom()<CR>
  138. " ALE Configuration
  139. let g:ale_completion_enabled = 0
  140. let g:ale_sign_column_always = 1
  141. let g:ale_set_signs = 1
  142. let g:ale_set_highlights = 0
  143. let g:ale_disable_lsp = 1
  144. " ALE linters
  145. let g:ale_use_global_executables = 1
  146. let g:ale_linters_explicit = 1
  147. let g:ale_linters = {}
  148. let g:ale_linters.javascript = [ 'eslint' ]
  149. let g:ale_linters.php = [ 'intelephense', 'phpcs' ]
  150. let g:ale_linters.go = [ 'gopls', 'gofmt', 'gobuild' ]
  151. let g:ale_phpcs_standard = "PSR2"
  152. " ALE fixers
  153. let g:ale_fixers = { '*': [ 'remove_trailing_lines', 'trim_whitespace' ] }
  154. let g:ale_fixers.javascript = [ 'prettier', 'eslint' ]
  155. let g:ale_fixers.go = [ 'gofmt' ]
  156. " ALE message should include responsible linter
  157. let g:ale_echo_msg_format = '[%linter%] %s'
  158. " Ale keymaps
  159. nnoremap <C-a>l :ALELint<CR>
  160. nnoremap <C-a>f :ALEFix<CR>
  161. nnoremap <C-a>i :ALEInfo<CR>
  162. " ALE styling
  163. highlight ALEErrorSign ctermbg=237 ctermfg=167
  164. highlight ALEWarningSign ctermbg=237 ctermfg=215
  165. highlight ALEInfoign ctermbg=237 ctermfg=117
  166. " ALE Configuration end
  167. " Tagbar
  168. let g:tagbar_autoclose = 1
  169. let g:tagbar_autofocus = 1
  170. let g:tagbar_map_showproto = ''
  171. nmap <Space>t :TagbarToggle<CR>
  172. " Toggle transparent background
  173. let g:is_transparent = 0
  174. function! Toggle_transparent()
  175. echo g:is_transparent
  176. if g:is_transparent == 0
  177. hi Normal guibg=NONE ctermbg=NONE
  178. let g:is_transparent = 1
  179. else
  180. set background=dark
  181. let g:is_transparent = 0
  182. endif
  183. endfunction
  184. nnoremap <Space>T :call Toggle_transparent()<CR>
  185. " Location list toggle
  186. function! Toggle_location_list()
  187. if get(b:, 'location_list', 0) == 0
  188. silent! lopen
  189. if get(getloclist(0, { 'winid': 0 }), 'winid')
  190. let b:location_list = 1
  191. endif
  192. else
  193. lclose
  194. let b:location_list = 0
  195. endif
  196. endfunction
  197. nnoremap <Space>l :call Toggle_location_list()<CR>
  198. " Quickfix list toggle
  199. function! Toggle_quick_list()
  200. if get(b:, 'quick_list', 0) == 0
  201. silent! copen
  202. if get(getqfist(0, { 'winid': 0 }), 'winid')
  203. let b:quick_list = 1
  204. endif
  205. else
  206. cclose
  207. let b:quick_list = 0
  208. endif
  209. endfunction
  210. nnoremap <Space>c :call Toggle_quick_list()<CR>
  211. " Git blame
  212. autocmd BufEnter * if !exists("b:git_blame") | let b:git_blame = 0 | endif
  213. function! Toggle_git_blame()
  214. if b:git_blame == 0
  215. :silent! Git blame
  216. let b:git_blame = 1
  217. else
  218. let winIndex = 1
  219. let winCnt = winnr('$')
  220. while winIndex <= winCnt
  221. if expand('%:e') == "fugitiveblame"
  222. :close
  223. else
  224. :wincmd w
  225. endif
  226. let winIndex += 1
  227. endwhile
  228. let b:git_blame = 0
  229. endif
  230. endfunction
  231. nnoremap <Space>b :call Toggle_git_blame()<CR>
  232. " Fuzzy search
  233. nmap <Space>f :Files<CR>
  234. nmap <Space>s :Rg<CR>
  235. " Git gutter
  236. nmap <Space>n :GitGutterNextHunk<CR>
  237. nmap <Space>p :GitGutterPrevHunk<CR>
  238. " Setting title to enable better tmux titling
  239. if exists('$TMUX')
  240. autocmd BufReadPost,FileReadPost,BufNewFile,BufEnter * call UpdateTmuxWindow()
  241. autocmd VimLeave * call system("tmux setw automatic-rename")
  242. endif
  243. function UpdateTmuxWindow()
  244. let title = @% == "" ? "vim" : "vim | " . expand("%:t")
  245. call system("tmux rename-window '" . title . "'")
  246. endfunction
  247. " Format XML pretty
  248. function! PrettyXML()
  249. set filetype=xml
  250. silent %!xmllint --format --encode UTF-8 --recover - 2>/dev/null
  251. endfunction
  252. " Format json pretty
  253. function! PrettyJSON()
  254. set filetype=json
  255. silent %!python3 -m json.tool
  256. endfunction
  257. nmap <Space>x :call PrettyXML()<CR>
  258. nmap <Space>j :call PrettyJSON()<CR>
  259. " Disable bad default keybindings
  260. inoremap <C-w> <Nop>
  261. " LSP configuration
  262. let g:lsc_enable_diagnostics = v:false
  263. let g:lsc_auto_map = v:true
  264. let g:lsc_autocomplete_length=1
  265. set omnifunc=lsc#complete#complete
  266. " LSP keymappings
  267. nnoremap <C-l>f :LSClientFindReferences<CR>
  268. nnoremap <C-l>g :tab LSClientGoToDefinitionSplit<CR>
  269. nnoremap <C-l>G :LSClientGoToDefinition<CR>
  270. nnoremap <C-l>h :LSClientShowHover<CR>
  271. " LSP servers
  272. let g:lsc_server_commands = {}
  273. let g:lsc_server_commands = {
  274. \ 'javascript': { 'command': 'typescript-language-server --stdio', 'log_level': -1, 'suppress_stderr': v:true },
  275. \ 'javascript.jsx': { 'command': 'typescript-language-server --stdio', 'log_level': -1, 'suppress_stderr': v:true },
  276. \ 'go': { 'command': 'gopls serve', 'log_level': -1, 'suppress_stderr': v:true },
  277. \ 'php': { 'command': 'intelephense --stdio', 'message_hooks': {'initialize': { 'initializationOptions': {'storagePath': '/tmp/intelephense'} } } },
  278. \}
  279. " LSP close preview after selecting completion
  280. autocmd CompleteDone * silent! pclose
  281. " LSP close quickfix list after selection
  282. autocmd BufLeave * cclose