vimrc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. " vim: set sw=2 ts=2 sts=2 et tw=78 foldmarker={{{,}}} foldlevel=0 foldmethod=marker:
  2. "
  3. " The Main VIMRC
  4. "
  5. " Configures have-anywhere, plugin-independent settings and mappings.
  6. " Loads plugins from `~/dotfiles/plugins.vim`.
  7. " Loads additional settings from `~/dotfiles/vim/settings/*.vim`.
  8. "
  9. " General Behaviours {{{
  10. set nocompatible " Use vim's full features (not vi-compatible)
  11. set mouse=a " Mouse for scrolling and resizing splits
  12. set modeline " Support vim modelines at the top of files
  13. set splitbelow " Normal splits open below current
  14. set splitright " Vertical splits open right of current
  15. set hidden " Change buffers without unsaved-warnings
  16. set backspace=indent,eol,start " Backspace for dummies - deletes most things
  17. set virtualedit=block " Move cursor freely in visual block mode
  18. if has('autocmd')
  19. " Clear existing autocommands to avoid side effects
  20. autocmd!
  21. " Detect a file's type and use plugin/indent configs if available
  22. filetype plugin indent on
  23. " Restore cursor position in a file (:help last-position-jump)
  24. autocmd BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$")
  25. \| execute "normal! g`\""
  26. \| endif
  27. " Git commit exception - keep cursor at start of file
  28. autocmd FileType gitcommit call cursor(1, 1)
  29. endif
  30. " }}}
  31. " Persistent Undo {{{
  32. if has('persistent_undo')
  33. set undofile " Keep undo history even when files are closed
  34. set undolevels=1000 " Max changes that can be undone
  35. set undoreload=10000 " Max lines to save for undo on a buffer reload
  36. endif
  37. " }}}
  38. " Vim Folders {{{
  39. " Store vim-related files in `~/.vim*` folders (e.g. `~/.vimswap`) instead of
  40. " at the currently-editted file's location
  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 tabstop=2 " Number of spaces to a tab
  64. set softtabstop=2 " Let backspace delete by indents
  65. set shiftround " Indent to nearest tabstops
  66. set shiftwidth=2 " Amount to indent with > and <
  67. set smarttab " Backspace tabs where appropriate even if spaces
  68. if has('autocmd')
  69. " Highlight trailing whitespace
  70. autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
  71. autocmd InsertLeave * match ExtraWhitespace /\s\+$/
  72. " Remove trailing whitespace on save
  73. autocmd BufWritePre * call RemoveTrailingWhitespace()
  74. function! RemoveTrailingWhitespace()
  75. let _s=@/ | let l = line(".") | let c = col(".") " Save search and cursor
  76. %s/\s\+$//e " Remove whitespace
  77. let @/=_s | call cursor(l, c) " Restore settings
  78. endfunction
  79. endif
  80. " }}}
  81. " Search {{{
  82. set hlsearch " Highlight search terms
  83. set ignorecase " Case insensitive search
  84. set incsearch " Find as you type search
  85. set smartcase " Case sensitive when uc present
  86. " }}}
  87. " Visuals {{{
  88. set colorcolumn=+1 " Highlight the textwidth limit
  89. set cursorline " Highlight current line
  90. set list " Display unprintables (like tabs)
  91. set listchars=tab:>\ ,extends:#,nbsp:# " Customise unprintables
  92. set scrolloff=5 " Lines to keep above/below cursor
  93. set shortmess+=filmnrxoOtT " Abbrev. of messages
  94. set showmatch " Show matching brackets/parenthesis
  95. set matchpairs+=<:> " Match <> as a pair, not just ()[]{}
  96. set sidescroll=1 " Side-ways scroll speed
  97. set sidescrolloff=10 " Columns to keep left/right of cursor
  98. set wildmenu " Ex command tab-completion shows list
  99. if has('syntax')
  100. syntax enable " Syntax highlighting
  101. endif
  102. set number " Show line numbers
  103. if v:version >= 703
  104. set relativenumber " Numbers relative to current line
  105. endif
  106. " }}}
  107. " Folding {{{
  108. if has('folding')
  109. set foldenable
  110. set foldmethod=indent " Fold lines based on indentation levels
  111. set foldlevel=10 " Files start mostly unfolded, start 10 indents deep
  112. endif
  113. " }}}}
  114. " Text Wrapping {{{
  115. set textwidth=80 " Try to keep text within 80 chars
  116. set nowrap " Start without text wrapping
  117. set whichwrap=b,s,h,l,<,>,[,] " Backspace and cursor keys wrap too
  118. set linebreak " Break on words when wrapping
  119. " }}}
  120. " Join (J) Behaviour {{{
  121. set nojoinspaces " Prevents inserting two spaces after punctuation
  122. if v:version > 703 || v:version == 703 && has("patch541")
  123. set formatoptions+=j " Remove comment chars when joining comment lines
  124. endif
  125. " }}}
  126. " Status Line {{{
  127. set laststatus=2 " always show the status line
  128. set showcmd " display command keypresses on the last line
  129. " }}}
  130. " Mappings {{{
  131. " <Space> is a very convenient leader key
  132. let mapleader = ' '
  133. " Reload vimrc settings
  134. map <leader>s :source $MYVIMRC<CR>:echo "vimrc reloaded..."<CR>
  135. " Buffers {{{
  136. " Save changes to a buffer
  137. map <silent> <Leader>w :up<CR>
  138. " Close a buffer
  139. map <silent> <Leader>q :q<CR>
  140. " List (:ls) all buffers, <CR> to close, or [number]<CR> to go to one
  141. map <Leader>l :buffers<CR>:buffer<Space>
  142. " Go to a buffer with [number]<CR>
  143. nmap <silent> <expr> <CR> (v:count > 0) ? ':<C-U>b'.v:count.'<CR>' : '<CR>'
  144. " }}}
  145. " Movement {{{
  146. " Jump to start and end of line with shift
  147. map H ^
  148. map L $
  149. " Move by screen lines when wrapped (g-movements)
  150. for key in ['j', 'k', '0', '^', '$']
  151. for mode in ['n', 'o', 'x']
  152. execute mode.'map <silent> <expr>' key '&wrap ? "g'.key.'" : "'.key.'"'
  153. endfor
  154. endfor
  155. " Move between splits easily
  156. map <C-j> <C-w>j
  157. map <C-k> <C-w>k
  158. map <C-h> <C-w>h
  159. map <C-l> <C-w>l
  160. " }}}
  161. " Visuals {{{
  162. " Adjust viewports to the same size
  163. map <Leader>= <C-w>=
  164. " Half-screen horizontal scrolling instead of full-screen
  165. map zl zL
  166. map zh zH
  167. " Backspace to hide search highlight temporarily
  168. nmap <silent> <backspace> :nohlsearch<CR>
  169. if v:version >= 703
  170. " Toggle both types of line numbers together
  171. map <Leader>n :set number! relativenumber! number?<CR>
  172. endif
  173. " }}}
  174. " Folding {{{
  175. if has('folding')
  176. " Toggle the fold of the current line with double-space
  177. nmap <silent> <expr> <Leader><Space> foldlevel('.') ? 'za' : '<Space>'
  178. " Set fold level 0-9 with zf[number]
  179. for lvl in range(10)
  180. execute 'nmap <silent> zf'.lvl.' :set foldlevel='.lvl.'<CR>'
  181. endfor
  182. endif
  183. " }}}
  184. " Editing {{{
  185. " Yank to end of line. Consistent with `C` and `D`
  186. map Y y$
  187. " More convenient escape
  188. imap kj <Esc>
  189. imap jk <Esc>
  190. " Remap number incrementing to `<C-c>`. For when `<C-a>` is used by tmux
  191. map <C-c> <C-a>
  192. " Convert tabs to spaces
  193. map <silent> <Leader><Tab> :retab<CR>
  194. " Toggle paste mode - no autoindenting of pasted material
  195. nmap <silent> <F2> :set paste! paste?<CR>
  196. set pastetoggle=<F2>
  197. " Search for version control conflict markers
  198. map <Leader>c /\v^[<\|=>]{7}( .*\|$)<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. " }}}