Advertisement
Guest User

vimrc

a guest
Sep 17th, 2019
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 13.23 KB | None | 0 0
  1. " vim: set foldmethod=marker:
  2. "----------------------------------------------------------
  3. " ENV setttings {{{1
  4. "----------------------------------------------------------
  5. "
  6. " The function needs to be near the top since it has to be known for later use
  7. " Sets only once the value of g:env to the running environment
  8. function! Config_setEnv() abort
  9.     if exists('g:env')
  10.         return
  11.     endif
  12.     if has('win64') || has('win32') || has('win16')
  13.         let g:env = 'WINDOWS'
  14.     else
  15.        let g:env = toupper(substitute(system('uname'), '\n', '', ''))
  16.     endif
  17. endfunction
  18.  
  19. call Config_setEnv()
  20.  
  21. "----------------------------------------------------------
  22. " General {{{1
  23. "----------------------------------------------------------
  24. if !(has('nvim'))
  25.     set nocompatible  " be iMproved, required
  26. endif
  27.  
  28. filetype off  " required
  29.  
  30. "----------------------------------------------------------
  31. " Plugins {{{1
  32. "----------------------------------------------------------
  33. " plug vim-plug if needed
  34. if empty(glob('~/.vim/autoload/plug.vim'))
  35.   silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
  36.     \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  37.   autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
  38. endif
  39.  
  40. " Specify a directory for plugins
  41. " - For Neovim: ~/.local/share/nvim/plugged
  42. " - Avoid using standard Vim directory names like 'plugin'
  43. " - Make sure you use single quotes
  44. call plug#begin('~/.vim/plugged')
  45.  
  46. " themes
  47. Plug 'morhetz/gruvbox'
  48.  
  49. " text objects
  50. Plug 'vim-scripts/argtextobj.vim'
  51. Plug 'michaeljsmith/vim-indent-object'
  52.  
  53. " git plugins
  54. Plug 'tpope/vim-fugitive'
  55. Plug 'airblade/vim-gitgutter'
  56.  
  57. " vim-vinegar
  58. Plug 'tpope/vim-vinegar'
  59.  
  60. " fzf
  61. " PlugInstall and PlugUpdate will clone fzf in ~/.fzf and run the install script
  62. Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --bin' }
  63.   " Both options are optional. You don't have to install fzf in ~/.fzf
  64.   " and you don't have to run the install script if you use fzf only in Vim.
  65. Plug 'junegunn/fzf.vim'
  66.  
  67. " filetypes
  68. Plug 'sheerun/vim-polyglot'
  69.  
  70. " lsp
  71. Plug 'neoclide/coc.nvim', {'branch': 'release'}
  72.  
  73. " tagbar
  74. Plug 'majutsushi/tagbar'
  75.  
  76. " statusline plugins
  77. Plug 'itchyny/lightline.vim'
  78.  
  79. " python plugins
  80. Plug 'Vimjas/vim-python-pep8-indent'
  81.  
  82. " Dockerfile syntax
  83. Plug 'moby/moby' , {'rtp': '/contrib/syntax/vim/'}
  84.  
  85. " tmux compatibility
  86. Plug 'tmux-plugins/vim-tmux-focus-events'
  87.  
  88. " Initialize plugin system
  89. call plug#end()
  90.  
  91. "----------------------------------------------------------
  92. " vim-plug helpers
  93. "----------------------------------------------------------
  94. function! PlugLoaded(name)
  95.     return (
  96.         \ has_key(g:plugs, a:name) &&
  97.         \ isdirectory(g:plugs[a:name].dir) &&
  98.         \ stridx(&rtp, g:plugs[a:name].dir) >= 0)
  99. endfunction
  100.  
  101. "----------------------------------------------------------
  102. "----------------------------------------------------------
  103. " Customizations {{{1
  104. "----------------------------------------------------------
  105. "----------------------------------------------------------
  106.  
  107. "----------------------------------------------------------
  108. " Feels
  109. "----------------------------------------------------------
  110. set nowrap
  111.  
  112. " more natural splits
  113. set splitbelow
  114. set splitright
  115.  
  116. "----------------------------------------------------------
  117. " Looks
  118. "----------------------------------------------------------
  119. if (has("termguicolors"))
  120.     set termguicolors
  121. endif
  122.  
  123. if !(has('nvim'))
  124.     set background=dark
  125. endif
  126.  
  127. let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
  128. let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
  129.  
  130.  
  131. " highlisht current line
  132. highlight LineNr ctermfg=grey
  133.  
  134. " font
  135. set guifont=Menlo\ Regular:h13
  136.  
  137. colorscheme gruvbox
  138.  
  139. "----------------------------------------------------------
  140. " General
  141. "----------------------------------------------------------
  142. " vim specific (nvim defaults)
  143. if !(has('nvim'))
  144.     " When opening a new line and no filetype-specific indenting is enabled, keep
  145.     " the same indent as the line you're currently on. Useful for READMEs, etc.
  146.     set autoindent
  147.  
  148.     " Highlight searches (use <C-L> to temporarily turn off highlighting)
  149.     set hlsearch
  150.  
  151.     " Dynaimc search
  152.     set incsearch
  153.  
  154.     " Display the cursor position on the last line of the screen or in the status
  155.     " line of a window
  156.     set ruler
  157. endif
  158.  
  159. " Enable syntax highlighting
  160. syntax enable
  161.  
  162. " filetype options
  163. filetype indent plugin on
  164.  
  165. " hide windows (instead of close when they lose focus)
  166. set hidden
  167.  
  168. " hide diff when winbdow lose focus
  169. set diffopt+=hiddenoff
  170.  
  171. " Better command-line completion
  172. set wildmenu
  173.  
  174. " Show partial commands in the last line of the screen
  175. set showcmd
  176.  
  177. " Use case insensitive search, except when using capital letters
  178. set ignorecase
  179. set smartcase
  180.  
  181. " Allow backspacing over autoindent, line breaks and start of insert action
  182. set backspace=indent,eol,start
  183.  
  184. " Stop certain movements from always going to the first character of a line.
  185. " While this behaviour deviates from that of Vi, it does what most users
  186. " coming from other editors would expect.
  187. set nostartofline
  188.  
  189. " Always display the status line, even if only one window is displayed
  190. set laststatus=2
  191.  
  192. " Instead of failing a command because of unsaved changes, instead raise a
  193. " dialogue asking if you wish to save changed files.
  194. set confirm
  195.  
  196. " Use visual bell instead of beeping when doing something wrong
  197. set visualbell
  198.  
  199. " And reset the terminal code for the visual bell. If visualbell is set, and
  200. " this line is also included, vim will neither flash nor beep. If visualbell
  201. " is unset, this does nothing.
  202. set t_vb=
  203.  
  204. " Enable use of the mouse for all modes
  205. set mouse=a
  206.  
  207. " Set the command window height to 2 lines, to avoid many cases of having to
  208. " "press <Enter> to continue"
  209. set cmdheight=2
  210.  
  211. " Display line numbers on the left
  212. set number
  213.  
  214. " Quickly time out on keycodes, but never time out on mappings
  215. set notimeout ttimeout ttimeoutlen=200
  216.  
  217. " Use <F11> to toggle between 'paste' and 'nopaste'
  218. set pastetoggle=<F11>
  219.  
  220. " vim complete lookup order
  221. set complete=".,t,w,b,u,i"
  222.  
  223. "----------------------------------------------------------
  224. " global indentation options
  225. "----------------------------------------------------------
  226. set shiftwidth=4
  227. set softtabstop=4
  228. set expandtab
  229.  
  230. "----------------------------------------------------------
  231. " General custom mappings
  232. "----------------------------------------------------------
  233. " Map Y to act like D and C, i.e. to yank until EOL, rather than act as yy,
  234. " which is the default
  235. map Y y$
  236.  
  237. " Map <C-L> (redraw screen) to also turn off search highlighting until the
  238. " next search
  239. nnoremap <C-L> :nohl<CR><C-L>
  240.  
  241. " multi paste form buffer in visual mode
  242. xnoremap <silent> p p:if v:register == '"'<Bar>let @@=@0<Bar>endif<cr>
  243.  
  244. " copy current buffer path to clipboard
  245. nnoremap <leader>c :let @+ = expand("%:p")<cr>
  246.  
  247. " save/load session
  248. map <F2> :mksession! ~/.vim_session <cr> " Quick write session with F2
  249. map <F3> :source ~/.vim_session <cr>     " And load session with F3
  250.  
  251. " Toggle number line from off -> absolute -> hybrid relative num -> off
  252. nnoremap <F1> :call ToggleNumLine()<CR>
  253.  
  254. function! ToggleNumLine()
  255.   if !&l:number && !&l:relativenumber
  256.     set number!
  257.   elseif &l:number && &l:relativenumber
  258.     set number!
  259.     set relativenumber!
  260.   elseif &l:number && !&l:relativenumber
  261.     set relativenumber!
  262.   endif
  263. endfunc
  264.  
  265. " count match of last search
  266. map ,* *<C-O>:%s///gn<CR>
  267.  
  268. "----------------------------------------------------------
  269. " General custom commands
  270. "----------------------------------------------------------
  271. " Tag current dir
  272. command! CTags !ctags -R -I EXTERN -I INIT
  273.   \ --exclude='build*' --exclude='venv/**' --exclude='**/site-packages/**'
  274.   \ --exclude='dist/**' --exclude='*.json' *
  275.  
  276. " Hnadle trailing white spaces
  277. function ShowSpaces(...)
  278.   let @/='\v(\s+$)|( +\ze\t)'
  279.   let oldhlsearch=&hlsearch
  280.   if !a:0
  281.     let &hlsearch=!&hlsearch
  282.   else
  283.     let &hlsearch=a:1
  284.   end
  285.   return oldhlsearch
  286. endfunction
  287.  
  288. function TrimSpaces() range
  289.   let oldhlsearch=ShowSpaces(1)
  290.   execute a:firstline.",".a:lastline."substitute ///gec"
  291.   let &hlsearch=oldhlsearch
  292. endfunction
  293.  
  294. command -bar -nargs=? ShowSpaces call ShowSpaces(<args>)
  295. command -bar -nargs=0 -range=% TrimSpaces <line1>,<line2>call TrimSpaces()
  296.  
  297. "----------------------------------------------------------
  298. " clipborad settings
  299. "----------------------------------------------------------
  300. if (g:env =~# 'DARWIN')
  301.     set clipboard=unnamed
  302. endif
  303.  
  304. "-----------------------------------------------------------
  305. " Ruler
  306. "-----------------------------------------------------------
  307. set colorcolumn=120
  308. highlight ColorColumn ctermbg=green
  309.  
  310. "-----------------------------------------------------------
  311. " autoreload files
  312. "-----------------------------------------------------------
  313. set autoread
  314. " Triger `autoread` when files changes on disk
  315. autocmd FileChangedShell,FocusGained,BufEnter,CursorHold,CursorHoldI *
  316.   \ if mode() != 'c' | checktime | endif
  317. " Notification after file change
  318. autocmd FileChangedShellPost *
  319.   \ echohl WarningMsg | echo "File changed on disk. Buffer reloaded." | echohl None
  320.  
  321. "----------------------------------------------------------
  322. " netrw
  323. "----------------------------------------------------------
  324. let g:netrw_banner = 0
  325. let g:netrw_liststyle = 3
  326. let g:netrw_browse_split = 4
  327. let g:netrw_altv = 1
  328. let g:netrw_winsize = 20
  329.  
  330. " Toggle Vexplore with Ctrl-E
  331. function! ToggleVExplorer()
  332.   if exists("t:expl_buf_num")
  333.       let expl_win_num = bufwinnr(t:expl_buf_num)
  334.       if expl_win_num != -1
  335.           let cur_win_nr = winnr()
  336.           exec expl_win_num . 'wincmd w'
  337.           close
  338.           exec cur_win_nr . 'wincmd w'
  339.           unlet t:expl_buf_num
  340.       else
  341.           unlet t:expl_buf_num
  342.       endif
  343.   else
  344.       exec '1wincmd w'
  345.       Vexplore
  346.       let t:expl_buf_num = bufnr("%")
  347.   endif
  348. endfunction
  349.  
  350. nmap <silent> <F9> :call ToggleVExplorer()<CR>
  351.  
  352. "----------------------------------------------------------
  353. " Dockerfile
  354. "----------------------------------------------------------
  355. augroup filetypedetect
  356. au! BufRead,BufNewFile *Dockerfile* setfiletype dockerfile
  357. augroup END
  358.  
  359. "----------------------------------------------------------
  360. " FZF
  361. "----------------------------------------------------------
  362. if executable('fzf') && PlugLoaded('fzf')
  363.     command! -bang -nargs=* GGrep
  364.       \ call fzf#vim#grep(
  365.       \   'git grep --line-number '.shellescape(<q-args>), 0,
  366.       \   { 'dir': systemlist('git rev-parse --show-toplevel')[0] }, <bang>0)
  367.  
  368.     nmap <silent> <Leader>g :GGrep<CR>
  369.     nmap <silent> <Leader>F :GFiles<CR>
  370.     nmap <silent> <Leader>f :Files<CR>
  371.     nmap <silent> <Leader>t :Tags<CR>
  372.     nmap <silent> <Leader>b :Buffers<CR>
  373.     nmap <silent> <leader>/ :BLines<CR>
  374.     nmap <silent> <leader>C :Commands<cr>
  375. endif
  376.  
  377. "----------------------------------------------------------
  378. " Tagbar
  379. "----------------------------------------------------------
  380. nmap <silent> <F8> :TagbarToggle<CR>
  381.  
  382. "----------------------------------------------------------
  383. " lightline
  384. "----------------------------------------------------------
  385. let g:lightline = {
  386.     \ 'active': {
  387.     \   'left': [[ 'mode', 'paste' ], [ 'gitbranch', 'readonly', 'filename', 'modified' ]]
  388.     \ },
  389.     \ 'component_function': {
  390.     \   'filename': 'LightlineFilename',
  391.     \   'gitbranch': 'fugitive#head'
  392.     \ }
  393.     \ }
  394.  
  395. function! LightlineFilename()
  396.   let root = fnamemodify(get(b:, 'git_dir'), ':h')
  397.   let path = expand('%:p')
  398.   if path[:len(root)-1] ==# root
  399.     return path[len(root)+1:]
  400.   endif
  401.   return expand('%')
  402. endfunction
  403.  
  404.  
  405. "----------------------------------------------------------
  406. " vim-coc
  407. "----------------------------------------------------------
  408. " less annoying highlights
  409. hi default link CocErrorHighlight   Normal
  410. hi default link CocWarningHighlight Normal
  411. hi default link CocInfoHighlight    Noraml
  412. hi default link CocHintHighlight    Noraml
  413.  
  414. hi default CocErrorSign    ctermfg=Red      guifg=#ff0000
  415. hi default CocWarningSign  ctermfg=Yellow   guifg=#ffff00
  416. hi default CocInfoSign     ctermfg=White    guifg=#ffffff
  417. hi default CocHintSign     ctermfg=Blue     guifg=#15aabf
  418.  
  419. " Remap keys for gotos
  420. nmap <silent> <leader>ld <Plug>(coc-definition)
  421. nmap <silent> <leader>lt <Plug>(coc-type-definition)
  422. nmap <silent> <leader>lc <Plug>(coc-declaration)
  423. nmap <silent> <leader>li <Plug>(coc-implementation)
  424. nmap <silent> <leader>lf <Plug>(coc-references)
  425.  
  426. " Remap for rename current word
  427. nmap <leader>lr <Plug>(coc-rename)
  428.  
  429. " Use K for show documentation in preview window
  430. nnoremap <silent> K :call <SID>show_documentation()<CR>
  431.  
  432. function! s:show_documentation()
  433.   if &filetype == 'vim'
  434.     execute 'h '.expand('<cword>')
  435.   else
  436.     call CocAction('doHover')
  437.   endif
  438. endfunction
  439.  
  440. " Highlight symbol under cursor on CursorHold
  441. autocmd CursorHold * silent call CocActionAsync('highlight')
  442.  
  443. " Use <Tab> and <S-Tab> to navigate the completion list
  444. inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
  445. inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
  446.  
  447. " Use <cr> to confirm completion, `<C-g>u` means break undo chain at current position.
  448. " Coc only does snippet and additional edit on confirm.
  449. inoremap <expr> <cr> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement