.vimrc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. " vim: foldmethod=marker
  2. " Basic Settings {{{1
  3. filetype plugin indent on
  4. set encoding=utf-8
  5. set ff=unix
  6. syntax enable
  7. " Turn on relative line numbers except for current line and uses absolute line
  8. " numbers for insert mode.
  9. " set number
  10. " set relativenumber
  11. " au InsertEnter * :set number norelativenumber
  12. " au InsertLeave * :set number relativenumber
  13. " Enable mouse in all modes.
  14. set mouse=a
  15. set ruler
  16. " I normally have this, but it looks ugly using this without a proper
  17. " colorscheme or changing it's highlight. But I can't be bothered to do those
  18. " (yet), so I just don't turn it on. 🤷
  19. " set cursorline
  20. set showcmd
  21. set scrolloff=5
  22. " See `:help wildmode` for more.
  23. set wildmenu
  24. " To be able to remap as <down> in command mode.
  25. set wildcharm=<tab>
  26. set hidden
  27. set splitbelow
  28. set splitright
  29. set ignorecase
  30. set smartcase
  31. set incsearch
  32. set autoindent
  33. set backspace=2
  34. set expandtab
  35. set tabstop=2
  36. set shiftwidth=2
  37. set softtabstop=2
  38. set textwidth=80
  39. set colorcolumn=+1
  40. set nojoinspaces
  41. " Always show status line.
  42. set laststatus=2
  43. " Retains cursor (column) position across buffer switches.
  44. set nostartofline
  45. " As much as possible of the last line in a window will be displayed.
  46. set display+=lastline
  47. " See `:help fo-table`.
  48. " set formatoptions+=tc
  49. set formatoptions+=tcroqnj
  50. " Less delay when pressing escape.
  51. set timeoutlen=1000
  52. set ttimeoutlen=10
  53. " Vim info options:
  54. " '0 --> Marks will not be saved.
  55. " f0 -- Marks will not be saved.
  56. " @0 --> Input line history will not be saved.
  57. " No % --> Buffer list will not be saved.
  58. " No :0 --> Command line history will be saved.
  59. " No <0 --> Registers will be saved.
  60. " No / --> Search history will be saved.
  61. set viminfo='0,@0,f0
  62. " Main Mappings {{{1
  63. nnoremap <bslash> <c-^>
  64. let mapleader=" "
  65. " xnoremap means only map for visual mode. (vnoremap remaps for both visual and
  66. " select mode.)
  67. nnoremap ' :
  68. xnoremap ' :
  69. " Basically for allowing `vv` in normal mode to copy character under cursor.
  70. xnoremap v y<esc>
  71. nnoremap - +
  72. nnoremap + -
  73. xnoremap - +
  74. xnoremap + -
  75. nnoremap q; q:k
  76. xnoremap q; q:k
  77. nnoremap q/ q/k
  78. nnoremap gs :w<cr>
  79. nnoremap <silent> <leader>s :set<space>hlsearch!<cr>
  80. nnoremap <leader>k H
  81. nnoremap <leader>j L
  82. nnoremap H ^
  83. xnoremap H ^
  84. nnoremap L $
  85. xnoremap L $
  86. onoremap M %
  87. nnoremap M %
  88. xnoremap M %
  89. nnoremap Y y$
  90. nnoremap ZZ <nop>
  91. nnoremap ZQ <nop>
  92. nnoremap <c-n> :bn<cr>
  93. nnoremap <c-p> :bp<cr>
  94. nnoremap gl :ls<cr>:b<space>
  95. nnoremap <leader>f mmvipgq`m
  96. nnoremap <leader>wo :only<cr>
  97. nnoremap <leader>we <c-w>=
  98. nnoremap <leader>re <c-l>
  99. nnoremap <leader>ri <c-a>
  100. nnoremap <leader>rd <c-x>
  101. nnoremap <c-h> <c-w>h
  102. nnoremap <c-j> <c-w>j
  103. nnoremap <c-k> <c-w>k
  104. nnoremap <c-l> <c-w>l
  105. inoremap <c-l> <esc>l%%a
  106. " Allows opening files from the directory of the currently opened buffer
  107. " quickly.
  108. " From <http://vimcasts.org/episodes/the-edit-command/>.
  109. nnoremap <leader>ew :e <c-r>=fnameescape(expand('%:h')).'/'<cr>
  110. xnoremap <leader>cp "*y
  111. xnoremap <leader>co "+y
  112. nnoremap <leader>p "*p
  113. nnoremap <leader>o "+p
  114. nnoremap <leader>eh :edit $HISTFILE<cr>
  115. nnoremap <leader>vc :edit ~/.vimrc<cr>
  116. nnoremap <leader>vn :edit ~/dots/nixos/config.nix<cr>
  117. nnoremap <leader>vs :edit ~/dots/sxhkd/.config/sxhkd/sxhkdrc<cr>
  118. nnoremap <leader>vz :edit ~/.zshrc<cr>
  119. " Status Line {{{1
  120. set statusline=
  121. " Buffer number.
  122. set statusline+=%3.3n\ »\
  123. " Filename.
  124. set statusline+=%f\ «\
  125. " Status flags.
  126. set statusline+=%h%m%r%w
  127. " " File type.
  128. " set statusline+=\[%{strlen(&ft)?&ft:'none'}]
  129. " Right align the rest.
  130. set statusline+=%=
  131. " " Character value.
  132. " set statusline+=0x%-8B
  133. " Line, character.
  134. set statusline+=%-14(%l,%c%V%)
  135. " File position.
  136. set statusline+=%<%P
  137. " Folds {{{1
  138. set fillchars="fold: "
  139. set foldmethod=syntax
  140. set foldlevel=99
  141. set foldlevelstart=99
  142. function! FoldText()
  143. let line = ' ' . substitute(getline(v:foldstart),
  144. \ '^\s*"\?\s*\|\s*"\?\s*{{' . '{\d*\s*', '', 'g')
  145. let lines_count = v:foldend - v:foldstart + 1
  146. let lines_count_text = printf("%10s", lines_count)
  147. let foldtextstart = '+' . repeat('-', v:foldlevel*2) . line
  148. let rightalignby = winwidth(0) - 79
  149. if rightalignby < 0
  150. let rightalignby = 1
  151. endif
  152. let foldtextend = lines_count_text . repeat(' ', rightalignby)
  153. let foldtextlength = strlen(substitute(foldtextstart .
  154. \ foldtextend, '.', 'x', 'g'))
  155. \ + &foldcolumn
  156. return foldtextstart . repeat(' ', winwidth(0)-foldtextlength) . foldtextend
  157. endfunction
  158. set foldtext=FoldText()
  159. " Filetype {{{1
  160. " Gitcommit {{{2
  161. function GitcommitOptions()
  162. setl tw=72 cc=73
  163. endfunction
  164. au filetype gitcommit call GitcommitOptions()
  165. " JavaScript {{{2
  166. if filereadable('./node_modules/.bin/prettier-standard')
  167. au filetype javascript nnoremap <buffer> <leader><leader> :%!./node_modules/.bin/prettier-standard<cr>
  168. au filetype typescript nnoremap <buffer> <leader><leader> :%!./node_modules/.bin/prettier-standard --parser typescript<cr>
  169. endif
  170. " Python {{{2
  171. function PythonOptions()
  172. setl ts=4 sw=4 sts=4
  173. endfunction
  174. au filetype python call PythonOptions()
  175. " Extra {{{1
  176. " Change cursor shape based on vim mode:
  177. "
  178. " - Block for normal mode.
  179. " - Bar for insert mode.
  180. if &term =~ "xterm\\|rxvt\\|st\\|alacritty"
  181. " Insert mode shape.
  182. let &t_SI = "\x1b[\x36 q"
  183. " Normal mode shape.
  184. let &t_EI = "\x1b[\x32 q"
  185. endif
  186. " Allows to `:q` when opening multiple files via the command line without
  187. " needing to open all buffers.
  188. au VimEnter * nested call VisitLastBuffer()
  189. fun! VisitLastBuffer()
  190. startinsert
  191. call feedkeys('')
  192. if (argc() > 1)
  193. last
  194. rew
  195. endif
  196. endfun
  197. " When switching buffers, preserve window view.
  198. " From <http://vim.wikia.com/wiki/Avoid_scrolling_when_switch_buffers>.
  199. au BufLeave * if !&diff | let b:winview = winsaveview() | endif
  200. au BufEnter * if exists('b:winview') && !&diff | call winrestview(b:winview) |
  201. \ unlet! b:winview | endif
  202. " Highlight group under cursor {{{2
  203. command! SynName echo synIDattr(synID(line("."), col("."), 1), "name")
  204. " Colors {{{1
  205. " source ~/dl/repos/disco.vim/colors/disco.vim
  206. hi cursorline cterm=NONE
  207. hi comment cterm=NONE
  208. hi colorcolumn ctermbg=NONE ctermfg=red