Thedro's Dotfiles https://www.thedroneely.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

11 months ago
11 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. "------------Setup-------------"
  2. mapclear " Clear all mappings.
  3. packadd! matchit " Add match tag pairs package.
  4. let skip_defaults_vim=1 " Do not load defaults if ~/.vimrc is missing.
  5. so ~/.vim/plugins.vim " Source plugins.
  6. "------------General------------"
  7. let mapleader = "\<space>" " Set default map leader.
  8. syntax enable " Enable syntax highlighting.
  9. set ruler " Show the cursor position all the time.
  10. set nowrap " Do not wrap lines.
  11. set nonumber " Set line numbers.
  12. set hlsearch " Enable search.
  13. set incsearch " Set incremental search.
  14. set showmatch " Briefly show matching tags.
  15. set nocompatible " Use Vim defaults.
  16. set splitbelow " Open horizontal splits below.
  17. set splitright " Open vertical splits to the right.
  18. set cursorline " Set cursor line highlight.
  19. set notimeout " Wait indefinitely for complete key combinations.
  20. set ttimeout " Prevent pressing <Esc> twice.
  21. set showcmd " Show key presses in status line.
  22. set wildmenu " Show tab completions menu.
  23. set nojoinspaces " Insert one space after a '.', '?' and '!' with a join command.
  24. set path+=** " Search subfolders.
  25. set display=lastline " @@@ which indicates remaining line is not displayed.
  26. set hidden " Set hidden buffers.
  27. set nobackup " No write backups.
  28. set noswapfile " Disable swap files.
  29. set nowritebackup " No overwrite backups.
  30. set autoread " Auto read file on external change.
  31. set autowrite " Write if modified.
  32. set autowriteall " Write if modified on buffer change.
  33. set autoindent " Copy indent from current line to new line.
  34. set ignorecase " Ignore case on search.
  35. set smartcase " Search by case smartly.
  36. set lazyredraw " Do not redraw screen when using macros.
  37. set undofile " Enable undofile.
  38. set viminfo+=n~/.vim/viminfo " Set viminfo file path.
  39. set undodir=~/.vim/undodir " Set undofile directory.
  40. set mouse=a " Enable mouse.
  41. set shortmess+=I " Disable startup message.
  42. set showtabline=0 " Disable tab bar.
  43. set updatetime=1500 " Set duration for cursor hold event.
  44. set fillchars= " Set split window margin fill to none.
  45. set laststatus=0 " Disable status bar.
  46. set linespace=3 " Set line spaces.
  47. set guiheadroom=0 " Disable headroom on window maximize.
  48. set history=50 " Keep 50 lines of command line history.
  49. set clipboard=unnamed " Use clipboard as default register.
  50. set backspace=indent,eol,start " Enable backspacing.
  51. set formatoptions-=t " Do not auto wrap text when typing.
  52. set complete=.,w,b,u " Set our desired autocompletion match pattern.
  53. set encoding=utf-8 " Set UTF-8 encoding.
  54. set scrolloff=3 " Set vertical scroll headroom.
  55. set sidescroll=3 " Set horizontal column scroll.
  56. set sidescrolloff=10 " Set horizontal scroll headroom.
  57. set foldcolumn=2 " Set fold column width to 2.
  58. " Convert tab to spaces.
  59. set tabstop=2 softtabstop=0 shiftwidth=2 smarttab expandtab
  60. let php_sql_query = 1 " Highlight SQL in PHP.
  61. let php_htmlInStrings = 1 " Highlight HTML in PHP.
  62. let g:javascript_plugin_jsdoc = 1 " Highlight JavaScript.
  63. "----------------Visuals----------------"
  64. colorscheme fluid " Set color scheme.
  65. set guioptions-=l " Remove left scrollbar in GUI.
  66. set guioptions-=L " Remove left scrollbar in GUI vertical split.
  67. set guioptions-=r " Remove right scrollbar in GUI.
  68. set guioptions-=R " Remove right scrollbar in GUI vertical split.
  69. set guioptions-=m " Remove menu in GUI.
  70. set guioptions-=T " Remove toolbar in GUI.
  71. set guioptions-=e " Remove tab bar in GUI.
  72. "---------------Functions---------------"
  73. " PHP Fixer
  74. function! PHPFix()
  75. :silent !notify-send -t 10000 "$(phpcbf % 2>&1)" > /dev/null 2>&1
  76. :redraw!
  77. endfunction
  78. " ESLint Fix
  79. function! ESLintFix()
  80. :silent !notify-send -t 10000 "$(eslint -c $HOME/.config/eslintrc.yml --fix % 2>&1)" > /dev/null 2>&1
  81. endfunction
  82. " Ansible Check
  83. function! AnsibleCheck()
  84. :silent !notify-send -t 10000 "$(ansible-playbook --syntax-check % 2>&1)" > /dev/null 2>&1 &
  85. :redraw!
  86. endfunction
  87. " Shell Check
  88. function! ShellCheck()
  89. :silent !notify-send -t 10000 "$(shellcheck -x --exclude=SC1090,SC1091 % 2>&1 && echo 'Shellcheck OK: %')" > /dev/null 2>&1 &
  90. endfunction
  91. " Nix Check
  92. function! NixCheck()
  93. :silent !notify-send -t 10000 "$(nix-linter % 2>&1 && echo 'Nix Lint OK: %' && nixfmt % 2>&1)" > /dev/null 2>&1
  94. :redraw!
  95. endfunction
  96. " Elixir Format
  97. function! ElixirFormat()
  98. :silent !notify-send -t 10000 "$(mix format % 2>&1 && echo 'Elixir Format OK: %')" > /dev/null 2>&1
  99. :redraw!
  100. endfunction
  101. " Typography Format
  102. function! TypographyFormat()
  103. :silent! %s/\(^\|\s\|\w\)\zs--\ze\($\|\s\|\w\)/–/g | silent! %s/\(^\|\s\|\w\)\zs---\ze\($\|\s\|\w\)/—/g
  104. :silent! %s/ '/ ‘/g | silent! %s/' /’ /g | :silent! %s/^'/‘/g | silent! %s/'$/’/g
  105. :silent! %s/ "/ “/g | silent! %s/" /” /g | :silent! %s/^"/“/g | silent! %s/"$/”/g
  106. :silent! %s/\S\@='\S@!/’/g | :silent! %s/\S\@<='\S\@=/’/g
  107. :silent! %s/\S\@="\S@!/”/g | :silent! %s/\S\@<="\S\@=/”/g
  108. endfunction
  109. "---------------Shortcuts---------------"
  110. " Edit shortcuts
  111. nmap <leader>ev :tabedit ~/.vimrc<cr>
  112. nmap <leader>ep :tabedit ~/.vim/plugins.vim<cr>
  113. nmap <leader>eh :tabedit ~/.vim/post-save-hook<cr>
  114. " Git, tags, and help commands
  115. nmap <leader>fgf :GFiles<cr>
  116. nmap <leader>fgs :GFiles?<cr>
  117. nmap <leader>fgc :Commits<cr>
  118. nmap <leader>ftb :BTags<cr>
  119. nmap <leader>flh :Helptags<cr>
  120. nmap <leader>flc :Commands<cr>
  121. " Show key mappings
  122. nmap <leader>mm :Maps<cr>
  123. nmap <leader>ma :Marks<cr>
  124. " Search documentation under cursor
  125. nmap <leader>dm :exe ':term ++close fzf-man ' . expand('<cword>')<cr>
  126. nmap <leader>di :exe ':term ++close fzf-doc ' . expand('<cword>')<cr>
  127. nmap <leader>dt :exe ':term dict -h localhost -d dict-moby-thesaurus-latest ' . expand('<cword>')<cr>
  128. " View function documentation
  129. nmap <leader>dp :call fzf#run({'options': ['--preview', 'echo doc {} \| psysh \| fold -s -w 80'], 'source': "psysh-doc", 'sink': ':term psysh-doc', 'down': '50%'})<cr>
  130. " Jump to line
  131. nmap <leader>jl :norm yaW<cr> \| :Jump<cr>
  132. " Load and save sessions
  133. nmap <leader>sl :source ~/.vim/sessions/session.vim \| :source ~/.vimrc<cr>
  134. nmap <leader>ss :silent! exe "!~/.vim/hooks/pre-session-save && notify-send 'Vim session saved.'" \| :mksession! ~/.vim/sessions/session.vim \| :redraw!<cr>
  135. " Toggle Spell Check
  136. nmap <leader>sp :set spell!<cr>
  137. " Sort lines by length
  138. vnoremap <leader>sn !perl -e 'print sort { length($a) <=> length($b) } <>'<cr>
  139. " Switch between tabs and spaces
  140. nmap <leader>ses :set tabstop=2 softtabstop=0 shiftwidth=2 smarttab expandtab \| :%retab!<cr>
  141. nmap <leader>set :set tabstop=2 softtabstop=0 shiftwidth=2 smarttab noexpandtab \| :%retab!<cr>
  142. " Close buffer and window
  143. nmap <leader>q :bd<cr>
  144. nmap <leader>w <C-w>c<cr>
  145. " Re-indent entire file
  146. nmap <leader>re gg=G<C-o><C-o>
  147. " Reset all settings and source configuration.
  148. nmap <leader>ra :set all& \| :source ~/.vimrc \| :e<cr>
  149. " Remove duplicate lines
  150. vnoremap <leader>rd !awk '\!visited[$0]++'<cr>
  151. " Toggle color column
  152. nmap <leader>cv :exe "set colorcolumn=" . (&colorcolumn == "" ? "80" : "")<cr>
  153. " Toggle cursor column
  154. nmap <leader>cx :set cursorcolumn!<cr>
  155. " Transliterate special characters to ascii
  156. nmap <leader>ce :silent! %s/–/--/g<cr> \| :silent! %s/—/---/g<cr> \| my \| :%!iconv -f utf-8 -t ascii//translit<cr>'y
  157. " Use typographic characters
  158. nmap <leader>cl :call TypographyFormat()<cr>
  159. " Toggle line numbers
  160. nmap <leader>cn :set number! \| set relativenumber!<cr>
  161. " Justify columns
  162. vnoremap <leader>ct !column -t -o' '<cr>
  163. " Fold text
  164. vnoremap <leader>cf <S-J><S-v> \| !fold -s -w<space>
  165. " Go to scratchpad of specified filetype
  166. noremap <leader>cs :w! $HOME/.cache/vim-scratchpad \| :e +setf\ <space>$HOME/.cache/vim-scratchpad<C-left><left>
  167. " File open mappings
  168. nmap <Tab> :Buffers<cr>
  169. nmap <leader>ov :Lines<cr>
  170. nmap <leader>ob :BLines<cr>
  171. nmap <leader>op :History<cr>
  172. nmap <leader>oc :History:<cr>
  173. nmap <leader>ol :Locate<space>
  174. nmap <leader>ot :exe '!$TERMINAL -cd ' . expand('%:p:h') . ' &'<cr><cr>
  175. nmap <leader>of :call fzf#run({'options': [], 'source': "cat $FZF_FILE_MARKS", 'sink': 'e', 'down': '20%'})<cr>:pwd<cr>
  176. nmap <leader>od :call fzf#run({'options': ['--preview', 'ls {}'], 'source': "cut -d' ' -f3 $FZF_DIRECTORY_MARKS", 'sink': 'cd', 'down': '20%'})<cr>:pwd<cr>
  177. nmap <leader>oo :call fzf#run({'options': ['--preview', 'highlight -O ansi --force {}'], 'source': 'rg --files --hidden \|\| find . -type f -printf "%P\n"', 'sink': 'e', 'down': '20%'})<cr>
  178. " Mappings for nnn
  179. nmap <leader>nm :NnnPicker<cr>
  180. nmap <leader>nn :NnnPicker '%:p:h'<cr>
  181. " Exit incremental search
  182. nmap <Esc><Esc> :nohl<cr>
  183. " Disable Ex Mode
  184. nnoremap Q <Nop>
  185. " Split window mappings
  186. nmap <Bslash> :vsplit<cr>
  187. nmap <C-Bslash> :split<cr>
  188. nmap <C-j> :resize +5<cr>
  189. nmap <C-k> :resize -5<cr>
  190. nmap <C-h> :vertical resize +5<cr>
  191. nmap <C-l> :vertical resize -5<cr>
  192. " Inverse Tabs
  193. inoremap <S-Tab> <C-d>
  194. " Retain visual selection when tabbing
  195. vnoremap < <gv
  196. vnoremap > >gv
  197. " Prevent cursor from jumping in visual select context https://ddrscott.github.io/blog/2016/yank-without-jank/
  198. vnoremap <expr>y "my\"" . v:register . "y`y"
  199. "---------------Plugin Settings---------------"
  200. let g:netrw_banner=0 " Disable netrw banner.
  201. let g:nnn#command = 'nnn -H' " Override default command.
  202. let g:nnn#set_default_mappings = 0 " Disable default mappings.
  203. let g:nnn#layout = 'new' " Opens the nnn window in a split.
  204. let g:nnn#layout = { 'left': '~20%' } " Left 20% of the window.
  205. "-------------------Scripts-------------------"
  206. " https://vi.stackexchange.com/questions/14300/vim-how-to-send-entire-line-to-a-buffer-of-type-terminal
  207. function s:repl(start, end, language)
  208. let g:terminal_buffer = get(g:, 'terminal_buffer', -1)
  209. if g:terminal_buffer == -1 || !bufexists(g:terminal_buffer)
  210. terminal ++rows=10
  211. let g:terminal_buffer = bufnr('')
  212. call term_sendkeys(g:terminal_buffer, a:language . "; exit" . "\<cr>")
  213. wincmd p
  214. elseif bufwinnr(g:terminal_buffer) == -1
  215. exe 'sbuffer ' . g:terminal_buffer
  216. wincmd p
  217. endif
  218. call term_sendkeys(g:terminal_buffer, join(getline(a:start, a:end), "\<cr>") . "\<cr>")
  219. endfunction
  220. " Jump to line and column in the format 123:13
  221. function s:cursor(selection)
  222. wincmd p
  223. let g:cursor_request = split(a:selection, ":")
  224. call cursor(g:cursor_request[0], g:cursor_request[1])
  225. endfunction
  226. command! -nargs=? -range Jump call s:cursor(@*)
  227. command! -nargs=? -range REPL call s:repl(<line1>, <line2>, <f-args>)
  228. "----------------Autorun----------------"
  229. augroup AutoCommands
  230. " Clear auto commands.
  231. autocmd!
  232. " Reload vimrc on vimrc file save.
  233. autocmd BufWritePost .vimrc source % | silent !notify-send 'Sourcing vimrc...'
  234. " Reload plugins.vim on file save.
  235. autocmd BufWritePost plugins.vim source % | silent !notify-send 'Sourcing plugins...'
  236. " Linting extension post write commands.
  237. autocmd BufWritePost *.php :call PHPFix()
  238. autocmd BufWritePost *.js :call ESLintFix()
  239. autocmd BufWritePost *.txt,*.md :only | :term ++rows=10 vale-wrapper %
  240. " Linting file type post write commands.
  241. autocmd FileType bash,sh autocmd! BufWritePost <buffer> :call ShellCheck()
  242. autocmd FileType nix autocmd! BufWritePost <buffer> silent call NixCheck()
  243. autocmd FileType elixir autocmd! BufWritePost <buffer> :call ElixirFormat()
  244. autocmd FileType css autocmd! BufWritePost <buffer> silent !notify-send "$(prettier --write --parser css % 2>&1)"
  245. autocmd FileType rust autocmd! BufWritePost <buffer> silent !notify-send "$(rustfmt % 2>&1 && echo 'rustfmt OK: %')"
  246. autocmd FileType c autocmd! BufWritePost <buffer> silent !notify-send "$(clang-format -i % 2>&1 && echo 'clang-format OK: %')"
  247. autocmd FileType go autocmd! BufWritePost <buffer> silent !notify-send "$(gofmt -w -s -e % 2>&1 && go vet % 2>&1 && echo 'gofmt OK: %')"
  248. autocmd FileType awk autocmd! BufWritePost <buffer> silent !notify-send "$(awk -g -f % 2>&1 && awk -o- -f % | sponge % && echo 'awk OK: %')"
  249. autocmd FileType yaml autocmd! BufWritePost <buffer> silent !notify-send "$(yaml round-trip --indent 2 --save % 2>&1 && yamllint -s % 2>&1 && echo 'yaml OK: %')"
  250. " File type function under cursor lookups.
  251. autocmd FileType go noremap <buffer> <leader>df :exe ':term ++rows=10 go doc ' . expand('<cexpr>')<cr>
  252. autocmd FileType nix noremap <buffer> <leader>df :exe ':term ++rows=10 nixos-option ' . expand('<cexpr>')<cr>
  253. autocmd FileType elixir noremap <buffer> <leader>df :exe ':term ++rows=10 sh -c "echo ''h(' . expand('<cexpr>') . ')'' \| iex"'<cr>
  254. " REPL commands.
  255. autocmd FileType go noremap <buffer> <leader>cc :REPL gore<cr>
  256. autocmd FileType lua noremap <buffer> <leader>cc :REPL lua<cr>
  257. autocmd FileType php noremap <buffer> <leader>cc :REPL psysh<cr>
  258. autocmd FileType elixir noremap <buffer> <leader>cc :REPL iex<cr>
  259. autocmd FileType sh noremap <buffer> <leader>cc :REPL dash -x<cr>
  260. autocmd FileType rust noremap <buffer> <leader>cc :REPL evcxr<cr>
  261. autocmd FileType nix noremap <buffer> <leader>cc :REPL nix repl<cr>
  262. autocmd FileType bash noremap <buffer> <leader>cc :REPL bash -x<cr>
  263. autocmd FileType python noremap <buffer> <leader>cc :REPL python<cr>
  264. autocmd FileType perl noremap <buffer> <leader>cc :REPL perl -de0<cr>
  265. autocmd FileType javascript noremap <buffer> <leader>cc :REPL node<cr>
  266. autocmd FileType awk noremap <buffer> <leader>cc :term ++rows=10 ++close awk -f %<cr>
  267. " General auto commands.
  268. autocmd BufWritePost *.tex :term ++close ++rows=10 latex-compile %
  269. autocmd BufWritePost rc.lua silent !notify-send "$(awesome -k 2>&1)"
  270. autocmd BufWritePost quotes,*.fortune silent !notify-send "$(strfile %)"
  271. autocmd BufWritePost $HOME/.config/chromexup/config.ini silent !notify-send "$(chromexup 2>&1)"
  272. autocmd BufWritePost *.desktop silent !notify-send "$(desktop-file-validate % 2>&1 && echo 'OK: %')"
  273. autocmd BufWritePost Xresources silent !xrdb ~/.config/X11/Xresources && notify-send 'Reloading Xresources...'
  274. " Automatically remove trailing white space on save.
  275. autocmd InsertLeave,BufWritePre * %s/\s\+$//e
  276. " Automatically save file on insert and idle.
  277. autocmd InsertLeave,CursorHold * silent! write
  278. \| silent! exe "!~/.vim/hooks/post-save > /dev/null 2>&1 &"
  279. \| :echo expand('%:t') '[filetype=' . &filetype . ']'
  280. augroup END
  281. set cmdheight=999 " Set command height temporarily to avoid prompt.
  282. set cmdheight=1 " Set command height back to the default.