vimrc 7.9 KB

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