init.vim 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. " ## Installation ##
  2. " 1. Install neovim, xclip
  3. " 2. Then this config:
  4. " mkdir -p ~/.config/nvim && cp init.vim ~/.config/nvim/
  5. " 3. Install vim-plug:
  6. " For *nix:
  7. " curl -fLo ~/.local/share/nvim/site/autoload/plug.vim --create-dirs \
  8. " https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  9. " For Windows: https://github.com/junegunn/vim-plug
  10. " 4. Then run `nvim`, ignore any errors and then :PlugInstall
  11. " 5. Ctrl+w twice to exit and then use as normal
  12. " ######## Prerequisites ######## "
  13. " ---- OS Detection ---- "
  14. if !exists('g:os_platform')
  15. if has('win64') || has('win32') || has('win16')
  16. let g:os_platform = 'WINDOWS'
  17. else
  18. let g:os_platform = toupper(substitute(system('uname'), '\n', '', ''))
  19. endif
  20. endif
  21. " ref: https://stackoverflow.com/a/51551652
  22. " ---- Plugins ---- "
  23. " Specify a directory for plugins
  24. " - For Neovim: ~/.local/share/nvim/plugged
  25. " - For Vim: ~/.vim/plugged
  26. " - Avoid using standard Vim directory names like 'plugin'
  27. call plug#begin('~/.local/share/nvim/plugged')
  28. " Run :PlugInstall after adding a plugin
  29. Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' }
  30. Plug 'crusoexia/vim-monokai'
  31. Plug 'scrooloose/nerdcommenter'
  32. " End of custom plugins
  33. call plug#end()
  34. " ######## General Configs (without plugins) ######## "
  35. " ---- Basic stuff for display ---- "
  36. syntax on
  37. filetype plugin indent on
  38. "colorscheme monokai
  39. set cursorline " Highlight cursor line
  40. " highlight long lines
  41. " more helpful than "set textwidth=80"
  42. highlight ColorColumn ctermbg=magenta
  43. call matchadd('ColorColumn', '\%81v', 100)
  44. " for invisible characters
  45. set listchars=eol:$,tab:>-,trail:~,extends:>,precedes:<,space:␣
  46. "highlight SpecialKey ctermfg=8 ctermbg=8
  47. "highlight link VisibleTab Error
  48. "match VisibleTab /\t$/
  49. " -- show row and column in footer + improved statusbar -- "
  50. set ruler
  51. set statusline=%F%m%r%h%w\ [%l,%c]\ [%L,%p%%]
  52. " -- use mouse cursor (when needed badly) -- "
  53. set mouse=a
  54. " -- better line number -- "
  55. " on normal mode, sets current line as current line and others as
  56. " relative
  57. set number relativenumber
  58. " on insert mode, this is not needed, so restores normal line numbers
  59. augroup numbertoggle
  60. autocmd!
  61. autocmd BufEnter,FocusGained,InsertLeave * set relativenumber
  62. autocmd BufLeave,FocusLost,InsertEnter * set norelativenumber
  63. augroup END
  64. " -- Tab width -- "
  65. " for using tabs for indenting
  66. set sw=4
  67. set tabstop=4
  68. " -- Cursor style change depending on mode -- "
  69. let &t_ti.="\e[1 q"
  70. let &t_SI.="\e[5 q"
  71. let &t_EI.="\e[1 q"
  72. let &t_te.="\e[0 q"
  73. " -- highlight the status bar when in insert mode -- "
  74. if version >= 700
  75. au InsertEnter * hi StatusLine ctermfg=235 ctermbg=2
  76. au InsertLeave * hi StatusLine ctermbg=230 ctermfg=12
  77. endif
  78. " -- highlight trailing spaces in annoying red -- "
  79. highlight ExtraWhitespace ctermbg=1 guibg=red
  80. match ExtraWhitespace /\s\+$/
  81. autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
  82. autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
  83. autocmd InsertLeave * match ExtraWhitespace /\s\+$/
  84. autocmd BufWinLeave * call clearmatches()
  85. " ---- General Keyboard Shortcuts ---- "
  86. " -- so that : commands can be typed quick! -- "
  87. nnoremap ; :
  88. " -- move cursor up and down even on insert mode -- "
  89. inoremap <C-S-j> <ESC>ji
  90. inoremap <C-S-k> <ESC>ki
  91. " -- set leader key to comma -- "
  92. let mapleader = ","
  93. " -- Rename current file, via Gary Bernhardt -- "
  94. function! RenameFile()
  95. let old_name = expand('%')
  96. let new_name = input('New file name: ', expand('%'))
  97. if new_name != '' && new_name != old_name
  98. exec ':saveas ' . new_name
  99. exec ':silent !rm ' . old_name
  100. redraw!
  101. endif
  102. endfunction
  103. map <leader>r :call RenameFile()<cr>
  104. " -- exit insert with one hand! alternative to ESC or CAPS LOCK mod and faster -- "
  105. inoremap jk <ESC>
  106. " -- window/tab related keys -- "
  107. " tab related
  108. map <C-t> :tabnew<CR>
  109. map <C-h> :tabp<CR>
  110. map <C-l> :tabn<CR>
  111. " -- save related -- "
  112. function! SaveFile()
  113. let old_name = expand('%')
  114. if old_name != ''
  115. exec ':w'
  116. else
  117. let filename_input = input('Enter filename to save as: ')
  118. exec ':saveas ' . filename_input
  119. endif
  120. endfunction
  121. nnoremap <C-s> :call SaveFile()<cr>
  122. inoremap <C-s> <Esc>:call SaveFile()<cr>i
  123. vnoremap <C-s> <Esc>:call SaveFile()<cr>v
  124. " -- close buffer related -- "
  125. function! CloseFile()
  126. let old_name = expand('%')
  127. let unsaved_changes = &mod
  128. if unsaved_changes == 1
  129. " there are unsaved changes, so we offer to save
  130. let savechanges_input = input('Do you want to save changes? (y/n) ')
  131. if savechanges_input == 'y'
  132. :call SaveFile()
  133. endif
  134. endif
  135. exec ':q!'
  136. redraw!
  137. endfunction
  138. nnoremap <C-w> :call CloseFile()<cr>
  139. inoremap <C-w> <Esc>:call CloseFile()<cr>
  140. vnoremap <C-w> <Esc>:call CloseFile()<cr>
  141. " -- Duplicate line -- "
  142. vnoremap <C-S-D> y'>p<CR>
  143. nnoremap <C-S-D> yypk<CR>
  144. " -- Cut line -- "
  145. vnoremap <C-k> d
  146. nnoremap <C-k> dd
  147. " -- move lines up and down -- "
  148. nnoremap <C-M-j> :m .+1<CR>==
  149. nnoremap <C-M-k> :m .-2<CR>==
  150. inoremap <C-M-j> <Esc>:m .+1<CR>==gi
  151. inoremap <C-M-k> <Esc>:m .-2<CR>==gi
  152. vnoremap <C-M-j> :m '>+1<CR>gv=gv
  153. vnoremap <C-M-k> :m '<-2<CR>gv=gv
  154. " -- copy-paste systemwide (linux) -- "
  155. " dependency: xclip
  156. if g:os_platform =~ 'LINUX'
  157. vmap <C-c> y:call system("xclip -i -selection clipboard", getreg("\""))<CR>:call system("xclip -i", getreg("\""))<CR>
  158. vmap <C-x> d:call system("xclip -i -selection clipboard", getreg("\""))<CR>:call system("xclip -i", getreg("\""))<CR>
  159. nmap <C-v> :call setreg("\"",system("xclip -o -selection clipboard"))<CR>p
  160. imap <C-v> <Esc>:call setreg("\"",system("xclip -o -selection clipboard"))<CR>pi
  161. " to replace the selected text with text in clipboard
  162. vmap <C-v> x<Esc>h:call setreg("\"",system("xclip -o -selection clipboard"))<CR>p
  163. endif
  164. " ref: https://stackoverflow.com/a/20902777
  165. " -- Select All -- "
  166. nnoremap <C-a> ggVG
  167. inoremap <C-a> <ESC>ggVG
  168. " ######## Plugin Related ######## "
  169. " ---- NerdTree related ---- "
  170. map <C-o> :NERDTreeToggle<CR>
  171. " ---- monokai related ---- "
  172. colorscheme monokai
  173. " ---- NerdCommenter related ---- "
  174. nmap <C-_> ,c<space>
  175. imap <C-_> <Esc>,c<space>i
  176. vmap <buffer> <C-_> ,c<space>
  177. " Add spaces after comment delimiters by default
  178. let g:NERDSpaceDelims = 1
  179. " Use compact syntax for prettified multi-line comments
  180. "let g:NERDCompactSexyComs = 1
  181. " Add your own custom formats or override the defaults
  182. let g:NERDCustomDelimiters = { 'c': { 'left': '/**','right': '*/' } }
  183. " Allow commenting and inverting empty lines (useful when commenting a region)
  184. let g:NERDCommentEmptyLines = 1
  185. " Enable trimming of trailing whitespace when uncommenting
  186. let g:NERDTrimTrailingWhitespace = 1