.vimrc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 'ssh://git@gogs.viktorgrahn.com:2022/viktor/ale.git'
  18. Plug 'maximbaz/lightline-ale'
  19. Plug 'preservim/tagbar'
  20. Plug 'vim-php/tagbar-phpctags.vim'
  21. Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
  22. Plug 'junegunn/fzf.vim'
  23. Plug 'airblade/vim-gitgutter'
  24. Plug 'mxw/vim-jsx'
  25. Plug 'pangloss/vim-javascript'
  26. Plug 'natebosch/vim-lsc'
  27. Plug 'tpope/vim-surround'
  28. Plug 'ap/vim-css-color', { 'for': [ 'css', 'scss' ] }
  29. call plug#end()
  30. filetype plugin indent on
  31. syntax on
  32. set autoindent
  33. set backspace=2
  34. set background=dark
  35. set completeopt=menu,popup,noselect
  36. set cursorcolumn
  37. set cursorline
  38. set expandtab
  39. set history=1000
  40. set hlsearch
  41. set incsearch
  42. set nowrap
  43. set number
  44. set numberwidth=4
  45. set pastetoggle=<F12> "Press <F12> when paste-alot
  46. set preserveindent
  47. set ruler
  48. set shiftround
  49. set shiftwidth=2
  50. set shortmess=atI
  51. set showcmd
  52. set showmatch
  53. set smartindent
  54. set smarttab
  55. set splitright
  56. set switchbuf+=usetab,newtab
  57. set termguicolors
  58. set tabstop=2
  59. set whichwrap+=<,>,[,],h,l
  60. set undofile
  61. set undodir=~/.vim/undo
  62. " Colorscheme
  63. colorscheme minimalist
  64. highlight Comment cterm=italic
  65. highlight Pmenu ctermbg=233
  66. " Lightline
  67. set laststatus=2
  68. set noshowmode
  69. " Source lightline theme
  70. if empty(glob('~/.vim/source/minimalist.vim'))
  71. silent !curl -fLO --create-dirs --output-dir ~/.vim/source https://public.viktorgrahn.com/minimalist.vim
  72. endif
  73. source ~/.vim/source/minimalist.vim
  74. let g:lightline = {
  75. \ 'colorscheme': 'minimalist',
  76. \ 'tabline_subseparator': { 'left': '', 'right': '' },
  77. \ 'active': {
  78. \ 'left': [
  79. \ [ 'mode', 'paste' ],
  80. \ [ 'filestate' ],
  81. \ [ 'gitbranch', 'tagbar' ],
  82. \ ],
  83. \ 'right': [
  84. \ [ 'linter_checking', 'linter_errors', 'linter_warnings', 'linter_infos', 'linter_ok' ],
  85. \ [ 'fileformat', 'fileencoding', 'filetype', 'percent', 'lineinfo', 'offset' ],
  86. \ ]
  87. \ },
  88. \ 'tabline': {
  89. \ 'left': [ [ 'tabs' ] ],
  90. \ 'right': [ ],
  91. \ },
  92. \ 'component': {
  93. \ 'tagbar': '%{tagbar#currenttag("%s", "", "f", "nearest-stl")}',
  94. \ },
  95. \ 'component_function': {
  96. \ 'filestate': 'LightlineFileState',
  97. \ 'offset': 'LightlineFileOffset',
  98. \ },
  99. \ 'component_expand': {
  100. \ 'gitbranch': 'FugitiveHead',
  101. \ 'linter_checking': 'lightline#ale#checking',
  102. \ 'linter_infos': 'lightline#ale#infos',
  103. \ 'linter_warnings': 'lightline#ale#warnings',
  104. \ 'linter_errors': 'lightline#ale#errors',
  105. \ 'linter_ok': 'lightline#ale#ok',
  106. \ },
  107. \ 'component_type': {
  108. \ 'linter_warnings': 'warning',
  109. \ 'linter_errors': 'error',
  110. \ 'linter_ok': 'ok',
  111. \ 'linter_infos': 'info',
  112. \ }
  113. \ }
  114. " Lightline helper (concatenate readonly state, filename and modified state)
  115. function! LightlineFileState()
  116. if @% == "" | return "[No name]" | endif
  117. let s = expand('%:t')
  118. if &modified | let s = s . "+" | endif
  119. if &readonly | let s = "[RO] " . s | endif
  120. return s
  121. endfunction
  122. " Lightline helper (get cursor line and character position in file)
  123. function! LightlineFileOffset()
  124. return line2byte(line('.')) + col('.') - 1
  125. endfunction
  126. " NERDtree
  127. let NERDTreeQuitOnOpen=1
  128. let NERDTreeShowHidden=1
  129. " NERDTree helper (toggle in current buffer)
  130. function! NERDTreeToggleCustom()
  131. if (exists("t:NERDTreeBufName") && bufwinnr(t:NERDTreeBufName) != -1)
  132. exe ":NERDTreeClose"
  133. elseif bufname('%') != ""
  134. exe ":NERDTreeFind"
  135. else
  136. exe ":NERDTreeCWD"
  137. endif
  138. endfunction
  139. " NERDTREE Toggle NERDTree on <space>-o
  140. map <Space>o :call NERDTreeToggleCustom()<CR>
  141. " NERDTree fix for menu bug
  142. let g:NERDTreeMinimalMenu=1
  143. " ALE Configuration
  144. let g:ale_completion_enabled = 0
  145. let g:ale_sign_column_always = 1
  146. let g:ale_set_signs = 1
  147. let g:ale_set_highlights = 0
  148. " QD for intelephense stubs
  149. let s:intelephense_config = {
  150. \ 'stubs': [ 'apache', 'bcmath', 'bz2', 'calendar', 'com_dotnet', 'Core', 'ctype', 'curl', 'date', 'dba', 'dom', 'enchant', 'exif', 'FFI', 'fileinfo', 'filter', 'fpm', 'ftp', 'gd', 'gettext', 'gmp', 'hash', 'iconv', 'imap', 'intl', 'json', 'ldap', 'libxml', 'mbstring', 'meta', 'mongodb', 'mysqli', 'oci8', 'odbc', 'openssl', 'pcntl', 'pcre', 'PDO', 'pdo_ibm', 'pdo_mysql', 'pdo_pgsql', 'pdo_sqlite', 'pgsql', 'Phar', 'posix', 'pspell', 'readline', 'Reflection', 'session', 'shmop', 'SimpleXML', 'snmp', 'soap', 'sockets', 'sodium', 'SPL', 'sqlite3', 'standard', 'superglobals', 'sysvmsg', 'sysvsem', 'sysvshm', 'tidy', 'tokenizer', 'xml', 'xmlreader', 'xmlrpc', 'xmlwriter', 'xsl', 'ZendOPcache', 'zip', 'zlib' ],
  151. \ 'files': {
  152. \ 'maxSize': 2000000
  153. \ },
  154. \}
  155. if exists("*ale#linter#Define")
  156. " Custom intelephense
  157. call ale#linter#Define('php', {
  158. \ 'name': 'intelephense-debug',
  159. \ 'lsp': 'stdio',
  160. \ 'initialization_options': function('ale_linters#php#intelephense#GetInitializationOptions'),
  161. \ 'executable': {b -> ale#path#FindExecutable(b, 'php_intelephense', [])},
  162. \ 'command': '%e --stdio',
  163. \ 'project_root': function('ale_linters#php#intelephense#GetProjectRoot'),
  164. \ 'lsp_config': s:intelephense_config,
  165. \})
  166. endif
  167. " ALE linters
  168. let g:ale_use_global_executables = 1
  169. let g:ale_linters_explicit = 1
  170. let g:ale_linters = {}
  171. let g:ale_linters.javascript = [ 'eslint' ]
  172. let g:ale_linters.php = [ 'intelephense-debug', 'phpcs', 'phpmd' ]
  173. let g:ale_linters.go = [ 'gopls', 'gofmt', 'gobuild' ]
  174. let g:ale_linters.json = [ 'jsonlint', 'jq' ]
  175. let g:ale_history_log_output = 1
  176. let g:ale_virtualtext_cursor = 'current'
  177. let g:ale_phpcs_standard = "PSR2"
  178. let g:ale_php_phpmd_ruleset = 'cleancode'
  179. let g:ale_php_intelephense_executable = 'debugIntelephense.sh'
  180. let g:ale_php_intelephense_config = { 'storagePath': '/tmp/intelephense-ale' }
  181. " ALE fixers
  182. let g:ale_fixers = { '*': [ 'remove_trailing_lines', 'trim_whitespace' ] }
  183. let g:ale_fixers.javascript = [ 'prettier', 'eslint' ]
  184. let g:ale_fixers.json = [ 'prettier' ]
  185. let g:ale_fixers.go = [ 'gofmt', 'goimports' ]
  186. " ALE message should include responsible linter
  187. let g:ale_echo_msg_format = '[%linter%] %s'
  188. " Ale keymaps
  189. nnoremap <C-a>l :ALELint<CR>
  190. nnoremap <C-a>f :ALEFix<CR>
  191. nnoremap <C-a>i :ALEInfo<CR>
  192. nnoremap <C-a>n :ALENext<CR>
  193. nnoremap <C-a>p :ALEPrevious<CR>
  194. " ALE styling
  195. highlight ALEErrorSign ctermbg=237 ctermfg=167
  196. highlight ALEWarningSign ctermbg=237 ctermfg=215
  197. highlight ALEInfoign ctermbg=237 ctermfg=117
  198. " ALE Configuration end
  199. " Tagbar
  200. let g:tagbar_autoclose = 1
  201. let g:tagbar_autofocus = 1
  202. let g:tagbar_map_showproto = ''
  203. nmap <Space>t :TagbarToggle<CR>
  204. " Toggle transparent background
  205. let g:is_transparent = 0
  206. function! Toggle_transparent()
  207. echo g:is_transparent
  208. if g:is_transparent == 0
  209. hi Normal guibg=NONE ctermbg=NONE
  210. let g:is_transparent = 1
  211. else
  212. set background=dark
  213. let g:is_transparent = 0
  214. endif
  215. endfunction
  216. nnoremap <Space>T :call Toggle_transparent()<CR>
  217. " Location list toggle
  218. function! Toggle_location_list()
  219. if get(b:, 'location_list', 0) == 0
  220. silent! lopen
  221. if get(getloclist(0, { 'winid': 0 }), 'winid')
  222. let b:location_list = 1
  223. endif
  224. else
  225. lclose
  226. let b:location_list = 0
  227. endif
  228. endfunction
  229. nnoremap <Space>l :call Toggle_location_list()<CR>
  230. " Quickfix list toggle
  231. function! Toggle_quick_list()
  232. if get(b:, 'quick_list', 0) == 0
  233. silent! copen
  234. if get(getqfist(0, { 'winid': 0 }), 'winid')
  235. let b:quick_list = 1
  236. endif
  237. else
  238. cclose
  239. let b:quick_list = 0
  240. endif
  241. endfunction
  242. nnoremap <Space>c :call Toggle_quick_list()<CR>
  243. " Git blame
  244. autocmd BufEnter * if !exists("b:git_blame") | let b:git_blame = 0 | endif
  245. function! Toggle_git_blame()
  246. if b:git_blame == 0
  247. :silent! Git blame
  248. let b:git_blame = 1
  249. else
  250. let winIndex = 1
  251. let winCnt = winnr('$')
  252. while winIndex <= winCnt
  253. if expand('%:e') == "fugitiveblame"
  254. :close
  255. else
  256. :wincmd w
  257. endif
  258. let winIndex += 1
  259. endwhile
  260. let b:git_blame = 0
  261. endif
  262. endfunction
  263. nnoremap <Space>b :call Toggle_git_blame()<CR>
  264. " Git gutter
  265. nmap <C-g>n :GitGutterNextHunk<CR>
  266. nmap <C-g>p :GitGutterPrevHunk<CR>
  267. " Fuzzy search
  268. nmap <Space>f :Files<CR>
  269. nmap <Space>s :Rg<CR>
  270. nmap <Space>w :Windows<CR>
  271. " Setting title to enable better tmux titling
  272. if exists('$TMUX')
  273. autocmd BufReadPost,FileReadPost,BufNewFile,BufEnter * call UpdateTmuxWindow()
  274. autocmd VimLeave * call system("tmux setw automatic-rename")
  275. endif
  276. function UpdateTmuxWindow()
  277. let excludedFiletypes = [ 'help', 'qf', 'nerdtree' ]
  278. if index(excludedFiletypes, &filetype) < 0
  279. let title = @% == "" ? "vim" : "vim (" . expand("%:t") . ")"
  280. call system("tmux rename-window '" . title . "'")
  281. endif
  282. endfunction
  283. " Format XML pretty
  284. function! PrettyXML()
  285. set filetype=xml
  286. silent %!xmllint --format --encode UTF-8 --recover - 2>/dev/null
  287. endfunction
  288. " Format json pretty
  289. function! PrettyJSON()
  290. set filetype=json
  291. silent %!python3 -m json.tool
  292. endfunction
  293. nmap <Space>x :call PrettyXML()<CR>
  294. nmap <Space>j :call PrettyJSON()<CR>
  295. " Diff since save
  296. nmap <Space>d :w !diff -y --suppress-common-lines --color % -<CR>
  297. " Navigation keymaps
  298. inoremap <C-h> <Left>
  299. inoremap <C-l> <Right>
  300. inoremap <C-j> <Down>
  301. inoremap <C-k> <Up>
  302. " Disable bad default keybindings
  303. inoremap <C-w> <Nop>
  304. " LSP configuration
  305. let g:lsc_enable_diagnostics = v:false
  306. let g:lsc_auto_map = v:true
  307. let g:lsc_autocomplete_length=1
  308. set omnifunc=lsc#complete#complete
  309. " LSP keymappings
  310. nnoremap <C-l>f :LSClientFindReferences<CR>
  311. nnoremap <C-l>g :tab LSClientGoToDefinitionSplit<CR>
  312. nnoremap <C-l>G :LSClientGoToDefinition<CR>
  313. nnoremap <C-l>h :LSClientShowHover<CR>
  314. nnoremap <C-l>c :LSClientSignatureHelp<CR>
  315. " LSP servers
  316. let g:lsc_server_commands = {}
  317. let g:lsc_server_commands = {
  318. \ 'javascript': { 'command': 'typescript-language-server --stdio', 'log_level': -1, 'suppress_stderr': v:true },
  319. \ 'javascript.jsx': { 'command': 'typescript-language-server --stdio', 'log_level': -1, 'suppress_stderr': v:true },
  320. \ 'php': { 'command': 'intelephense --stdio', 'message_hooks': {'initialize': { 'initializationOptions': {'storagePath': '/tmp/intelephense'} } }, 'workspace_config': { 'intelephense': s:intelephense_config } },
  321. \ 'go': { 'command': 'gopls serve', 'log_level': -1, 'suppress_stderr': v:true },
  322. \}
  323. " LSP close preview after selecting completion
  324. autocmd CompleteDone * silent! pclose
  325. " LSP close quickfix list after selection
  326. autocmd BufLeave * cclose
  327. " Auto expand
  328. inoremap (; (<CR>);<C-c>O
  329. inoremap (, (<CR>),<C-c>O
  330. inoremap (<CR> (<CR>)<C-c>O
  331. inoremap {; {<CR>};<C-c>O
  332. inoremap {, {<CR>},<C-c>O
  333. inoremap {<CR> {<CR>}<C-c>O
  334. inoremap [; [<CR>];<C-c>O
  335. inoremap [, [<CR>],<C-c>O
  336. inoremap [<CR> [<CR>]<C-c>O