vimrc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. " vim: set sw=2 ts=2 sts=2 et tw=80 fmr={{{,}}} fdl=0 fdm=marker:
  2. "
  3. " Main Vim Config File
  4. "
  5. " Loads plugins from `~/dotfiles/plugins.vim`.
  6. " Loads additional settings from `~/dotfiles/vim/settings/*.vim`.
  7. "
  8. " General Behaviours {{{
  9. set nocompatible " Use vim's full features (not vi-compatible)
  10. set backspace=indent,eol,start " Backspace for dummies - deletes most things
  11. set hidden " Change buffers without unsaved-warnings
  12. set modeline " Support vim modelines (e.g. start of this file)
  13. set mouse=a " Mouse support
  14. set splitbelow " Normal splits open below current
  15. set splitright " Vertical splits open right of current
  16. set virtualedit=block " Move cursor freely in visual block mode
  17. if has('autocmd')
  18. " Clear existing autocommands to avoid side effects
  19. autocmd!
  20. " Detect a file's type and use plugin/indent configs if available
  21. filetype plugin indent on
  22. " Restore cursor position in a file (:help last-position-jump)
  23. autocmd BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$")
  24. \| execute "normal! g`\""
  25. \| endif
  26. " Keep cursor at start of buffer for: Git commits
  27. autocmd FileType gitcommit call cursor(1, 1)
  28. endif
  29. " }}}
  30. " Persistent Undo {{{
  31. if has('persistent_undo')
  32. set undofile " Keep undo history even when files are closed
  33. set undolevels=1000 " Max changes that can be undone
  34. set undoreload=10000 " Max lines to save for undo on a buffer reload
  35. endif
  36. " }}}
  37. " Folders {{{
  38. " Store vim working files in `~/.vim[something]` folders (e.g. `~/.vimswap`)
  39. let folders = {}
  40. if &swapfile | let folders['swap'] = 'directory' | endif
  41. if &backup | let folders['backup'] = 'backupdir' | endif
  42. if has('persistent_undo')
  43. if &undofile | let folders['undo'] = 'undodir' | endif
  44. endif
  45. for [folder, setting] in items(folders)
  46. let path = expand('~/.vim'.folder.'/')
  47. " Create the folders
  48. if exists('*mkdir') && !isdirectory(path)
  49. call mkdir(path, 'p')
  50. endif
  51. " Use the folders
  52. if isdirectory(path)
  53. execute 'set' setting.'='.path
  54. else
  55. echo 'Warning! No folder:' path
  56. endif
  57. endfor
  58. " }}}
  59. " Tabs and Whitespace {{{
  60. set expandtab " Convert tabs to spaces
  61. set shiftround " Indent to nearest tabstops
  62. set shiftwidth=2 " Number of spaces for (auto)indent.
  63. set tabstop=2 " Number of spaces to a tab
  64. if has('autocmd')
  65. " Highlight trailing whitespace
  66. highlight ExtraWhitespace ctermbg=red guibg=red
  67. autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
  68. autocmd InsertLeave * match ExtraWhitespace /\s\+$/
  69. " Remove trailing whitespace on save
  70. autocmd BufWritePre * call RemoveTrailingWhitespace()
  71. function! RemoveTrailingWhitespace()
  72. let _s=@/ | let l = line(".") | let c = col(".") " Save search and cursor
  73. %s/\s\+$//e " Remove whitespace
  74. let @/=_s | call cursor(l, c) " Restore settings
  75. endfunction
  76. endif
  77. " }}}
  78. " Search {{{
  79. set hlsearch " Highlight search terms
  80. set ignorecase " Case-insensitive search
  81. set incsearch " Find-as-you-type search
  82. set smartcase " Case-sensitive when uppercase present
  83. " }}}
  84. " Visuals {{{
  85. set cursorline " Highlight current line
  86. set list " Display unprintables (like tabs)
  87. set listchars=tab:>\ ,extends:#,nbsp:# " Customise unprintables
  88. set matchpairs+=<:> " Match <> as a pair, not just ()[]{}
  89. set number " Show line numbers
  90. set scrolloff=5 " Lines to keep above/below cursor
  91. set shortmess+=filmnrxoOtT " Abbreviation of messages
  92. set showmatch " Show matching brackets/parenthesis
  93. set sidescroll=1 " Side-ways scroll speed
  94. set sidescrolloff=10 " Columns to keep left/right of cursor
  95. set wildmenu " Ex command tab-complete shows options
  96. if has('syntax')
  97. syntax enable " Syntax highlighting
  98. endif
  99. if v:version >= 703
  100. set colorcolumn=+1 " Highlight the textwidth limit
  101. set relativenumber " Show numbers relative to current line
  102. endif
  103. " }}}
  104. " Folding {{{
  105. if has('folding')
  106. set foldenable
  107. set foldmethod=indent " Fold lines based on indentation levels
  108. set foldlevel=10 " Files start mostly unfolded, start 10 indents deep
  109. endif
  110. " }}}}
  111. " Text Wrapping {{{
  112. set textwidth=80 " Try to keep text within 80 chars
  113. set nowrap " Start without text wrapping
  114. set whichwrap=b,s,h,l,<,>,[,] " Backspace and cursor keys wrap too
  115. set linebreak " Do not break lines mid-word, only on spaces
  116. " }}}
  117. " Join (J) Behaviour {{{
  118. set nojoinspaces " Prevents inserting two spaces after punctuation
  119. if v:version > 703 || v:version == 703 && has("patch541")
  120. set formatoptions+=j " Remove comment chars when joining comment lines
  121. endif
  122. " }}}
  123. " Status Line {{{
  124. set laststatus=2 " always show the status line
  125. set showcmd " display command keypresses on the last line
  126. " }}}
  127. " Mappings {{{
  128. " <Space> is a very convenient leader key
  129. let mapleader = ' '
  130. " Reload vimrc settings
  131. map <leader>s :source $MYVIMRC<CR>:echo "vimrc reloaded..."<CR>
  132. " Buffers {{{
  133. " Save changes to a buffer
  134. map <silent> <Leader>w :up<CR>
  135. " Close a buffer
  136. map <silent> <Leader>q :q<CR>
  137. " List (:ls) all buffers, <CR> to close, or [number]<CR> to go to [number]
  138. map <Leader>l :buffers<CR>:buffer<Space>
  139. " Go to buffer [number] with [number]<CR>
  140. nmap <silent> <expr> <CR> (v:count > 0) ? ':<C-U>b'.v:count.'<CR>' : '<CR>'
  141. " }}}
  142. " Movement {{{
  143. " Jump to start and end of line with shift
  144. map H ^
  145. map L $
  146. " Move by screen lines, not vim lines, when wrapped (g-movements)
  147. for key in ['j', 'k', '0', '^', '$']
  148. for mode in ['n', 'o', 'x']
  149. execute mode.'map <silent> <expr>' key '&wrap ? "g'.key.'" : "'.key.'"'
  150. endfor
  151. endfor
  152. " Move between splits easily
  153. map <C-j> <C-w>j
  154. map <C-k> <C-w>k
  155. map <C-h> <C-w>h
  156. map <C-l> <C-w>l
  157. " }}}
  158. " Visuals {{{
  159. " Equalise split sizes
  160. map <Leader>= <C-w>=
  161. " Half-screen horizontal scrolling instead of full-screen
  162. map zl zL
  163. map zh zH
  164. " Backspace to hide search highlight temporarily
  165. nmap <silent> <backspace> :nohlsearch<CR>
  166. if v:version >= 703
  167. " Toggle both types of line numbers together
  168. map <Leader>n :set number! relativenumber! number?<CR>
  169. endif
  170. " }}}
  171. " Folding {{{
  172. if has('folding')
  173. " Toggle the fold of the current line with double-space
  174. nmap <silent> <expr> <Leader><Space> foldlevel('.') ? 'za' : '<Space>'
  175. " Set fold level 0-9 with zf[number]
  176. for lvl in range(10)
  177. execute 'nmap <silent> zf'.lvl.' :set foldlevel='.lvl.'<CR>'
  178. endfor
  179. endif
  180. " }}}
  181. " Editing {{{
  182. " Yank to end of line. Consistent with `C` and `D`
  183. map Y y$
  184. " Convenient escape - mash `jk`
  185. imap kj <Esc>
  186. imap jk <Esc>
  187. " Increment numbers with `<C-c>`. For when `<C-a>` is used by tmux
  188. map <C-c> <C-a>
  189. " Convert tabs to spaces
  190. map <silent> <Leader><Tab> :retab<CR>
  191. " Toggle paste mode - no autoindenting of pasted material
  192. nmap <silent> <F2> :set paste! paste?<CR>
  193. set pastetoggle=<F2>
  194. " Search for version control conflict markers
  195. map <Leader>c /\v^[<\|=>]{7}( .*\|$)<CR>
  196. " Do math on the current line.
  197. map <silent> <Leader>m :exe "normal 0f=d$"<CR>0y$A=<C-r>=<C-r>"<CR><Esc>
  198. "map <silent> <Leader>m yyp:.!bc<CR>
  199. " }}}
  200. " }}}
  201. " Commands {{{
  202. if has('user_commands')
  203. " View unsaved changes in a file (:help DiffOrig)
  204. command! DiffOrig vert new | set buftype=nofile | read ++edit # | 0d_
  205. \| diffthis | wincmd p | diffthis
  206. " Insert a date/time on the next line
  207. command! Date :put = strftime('%d/%m/%Y %I:%M%p')
  208. endif
  209. " }}}
  210. " Load Plugins {{{
  211. if filereadable(expand("~/dotfiles/vim/plugins.vim"))
  212. source ~/dotfiles/vim/plugins.vim
  213. endif
  214. " }}}
  215. " Load Extra Settings {{{
  216. for filePath in split(globpath('~/dotfiles/vim/settings', '*.vim'), '\n')
  217. execute 'source' filePath
  218. endfor
  219. " }}}