vimrc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. highlight ExtraWhitespace ctermbg=red guibg=red
  71. autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
  72. autocmd InsertLeave * match ExtraWhitespace /\s\+$/
  73. " Remove trailing whitespace on save
  74. autocmd BufWritePre * call RemoveTrailingWhitespace()
  75. function! RemoveTrailingWhitespace()
  76. let _s=@/ | let l = line(".") | let c = col(".") " Save search and cursor
  77. %s/\s\+$//e " Remove whitespace
  78. let @/=_s | call cursor(l, c) " Restore settings
  79. endfunction
  80. endif
  81. " }}}
  82. " Search {{{
  83. set hlsearch " Highlight search terms
  84. set ignorecase " Case insensitive search
  85. set incsearch " Find as you type search
  86. set smartcase " Case sensitive when uc present
  87. " }}}
  88. " Visuals {{{
  89. set colorcolumn=+1 " Highlight the textwidth limit
  90. set cursorline " Highlight current line
  91. set list " Display unprintables (like tabs)
  92. set listchars=tab:>\ ,extends:#,nbsp:# " Customise unprintables
  93. set scrolloff=5 " Lines to keep above/below cursor
  94. set shortmess+=filmnrxoOtT " Abbrev. of messages
  95. set showmatch " Show matching brackets/parenthesis
  96. set matchpairs+=<:> " Match <> as a pair, not just ()[]{}
  97. set sidescroll=1 " Side-ways scroll speed
  98. set sidescrolloff=10 " Columns to keep left/right of cursor
  99. set wildmenu " Ex command tab-completion shows list
  100. if has('syntax')
  101. syntax enable " Syntax highlighting
  102. endif
  103. set number " Show line numbers
  104. if v:version >= 703
  105. set relativenumber " Numbers relative to current line
  106. endif
  107. " }}}
  108. " Folding {{{
  109. if has('folding')
  110. set foldenable
  111. set foldmethod=indent " Fold lines based on indentation levels
  112. set foldlevel=10 " Files start mostly unfolded, start 10 indents deep
  113. endif
  114. " }}}}
  115. " Text Wrapping {{{
  116. set textwidth=80 " Try to keep text within 80 chars
  117. set nowrap " Start without text wrapping
  118. set whichwrap=b,s,h,l,<,>,[,] " Backspace and cursor keys wrap too
  119. set linebreak " Break on words when wrapping
  120. " }}}
  121. " Join (J) Behaviour {{{
  122. set nojoinspaces " Prevents inserting two spaces after punctuation
  123. if v:version > 703 || v:version == 703 && has("patch541")
  124. set formatoptions+=j " Remove comment chars when joining comment lines
  125. endif
  126. " }}}
  127. " Status Line {{{
  128. set laststatus=2 " always show the status line
  129. set showcmd " display command keypresses on the last line
  130. " }}}
  131. " Mappings {{{
  132. " <Space> is a very convenient leader key
  133. let mapleader = ' '
  134. " Reload vimrc settings
  135. map <leader>s :source $MYVIMRC<CR>:echo "vimrc reloaded..."<CR>
  136. " Buffers {{{
  137. " Save changes to a buffer
  138. map <silent> <Leader>w :up<CR>
  139. " Close a buffer
  140. map <silent> <Leader>q :q<CR>
  141. " List (:ls) all buffers, <CR> to close, or [number]<CR> to go to one
  142. map <Leader>l :buffers<CR>:buffer<Space>
  143. " Go to a buffer with [number]<CR>
  144. nmap <silent> <expr> <CR> (v:count > 0) ? ':<C-U>b'.v:count.'<CR>' : '<CR>'
  145. " }}}
  146. " Movement {{{
  147. " Jump to start and end of line with shift
  148. map H ^
  149. map L $
  150. " Move by screen lines when wrapped (g-movements)
  151. for key in ['j', 'k', '0', '^', '$']
  152. for mode in ['n', 'o', 'x']
  153. execute mode.'map <silent> <expr>' key '&wrap ? "g'.key.'" : "'.key.'"'
  154. endfor
  155. endfor
  156. " Move between splits easily
  157. map <C-j> <C-w>j
  158. map <C-k> <C-w>k
  159. map <C-h> <C-w>h
  160. map <C-l> <C-w>l
  161. " }}}
  162. " Visuals {{{
  163. " Adjust viewports to the same size
  164. map <Leader>= <C-w>=
  165. " Half-screen horizontal scrolling instead of full-screen
  166. map zl zL
  167. map zh zH
  168. " Backspace to hide search highlight temporarily
  169. nmap <silent> <backspace> :nohlsearch<CR>
  170. if v:version >= 703
  171. " Toggle both types of line numbers together
  172. map <Leader>n :set number! relativenumber! number?<CR>
  173. endif
  174. " }}}
  175. " Folding {{{
  176. if has('folding')
  177. " Toggle the fold of the current line with double-space
  178. nmap <silent> <expr> <Leader><Space> foldlevel('.') ? 'za' : '<Space>'
  179. " Set fold level 0-9 with zf[number]
  180. for lvl in range(10)
  181. execute 'nmap <silent> zf'.lvl.' :set foldlevel='.lvl.'<CR>'
  182. endfor
  183. endif
  184. " }}}
  185. " Editing {{{
  186. " Yank to end of line. Consistent with `C` and `D`
  187. map Y y$
  188. " More convenient escape
  189. imap kj <Esc>
  190. imap jk <Esc>
  191. " Remap number incrementing to `<C-c>`. For when `<C-a>` is used by tmux
  192. map <C-c> <C-a>
  193. " Convert tabs to spaces
  194. map <silent> <Leader><Tab> :retab<CR>
  195. " Toggle paste mode - no autoindenting of pasted material
  196. nmap <silent> <F2> :set paste! paste?<CR>
  197. set pastetoggle=<F2>
  198. " Search for version control conflict markers
  199. map <Leader>c /\v^[<\|=>]{7}( .*\|$)<CR>
  200. " }}}
  201. " }}}
  202. " Commands {{{
  203. if has('user_commands')
  204. " View unsaved changes in a file (:help DiffOrig)
  205. command! DiffOrig vert new | set buftype=nofile | read ++edit # | 0d_
  206. \| diffthis | wincmd p | diffthis
  207. " Insert a date/time on the next line
  208. command! Date :put = strftime('%d/%m/%Y %I:%M%p')
  209. endif
  210. " }}}
  211. " Load Plugins {{{
  212. if filereadable(expand("~/dotfiles/vim/plugins.vim"))
  213. source ~/dotfiles/vim/plugins.vim
  214. endif
  215. " }}}
  216. " Load Extra Settings {{{
  217. for filePath in split(globpath('~/dotfiles/vim/settings', '*.vim'), '\n')
  218. execute 'source' filePath
  219. endfor
  220. " }}}