vimrc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. set nocompatible " Don't have to try to be compatible with old vi
  2. set encoding=utf-8
  3. """"""""
  4. "" Plugin Loading with Pathogen
  5. """""""""""""""""""""""""""""""""""""""""""""""""""
  6. call pathogen#infect()
  7. call pathogen#helptags()
  8. """"""""
  9. "" Environment - What kind of machine are we on?
  10. """""""""""""""""""""""""""""""""""""""""""""""""""
  11. if has("unix")
  12. " remove the newline character from the uname command
  13. let s:uname = substitute(system("uname"), "\n", "", "")
  14. endif
  15. """"""""
  16. "" General Behaviours
  17. """""""""""""""""""""""""""""""""""""""""""""""""""
  18. set splitbelow " New splits appear below current window instead of above
  19. set splitright " New splits appear right of current window
  20. set ttyfast " Smooth movement
  21. set ttymouse=xterm2
  22. " Persistent undo
  23. if has("persistent_undo")
  24. set undofile
  25. set undodir=~/.vimundo
  26. endif
  27. if has("autocmd")
  28. " Enable filetype specific features
  29. filetype plugin indent on
  30. " Clear existing autocmd
  31. autocmd!
  32. " When editing a file, always jump to the last cursor position
  33. autocmd BufReadPost *
  34. \ if line("'\"") > 0 && line ("'\"") <= line("$") |
  35. \ exe "normal! g'\"" |
  36. \ endif
  37. " Maps for Quickfix and Location List windows
  38. autocmd FileType qf nnoremap <silent> <buffer> q :ccl<CR>:lcl<CR>
  39. autocmd FileType qf nnoremap <silent> <buffer> o <CR>
  40. autocmd FileType qf nnoremap <silent> <buffer> go <CR><C-W><C-W>
  41. " Any actions on startup
  42. autocmd VimEnter * call StartUp()
  43. autocmd WinEnter * setlocal cursorline
  44. autocmd WinLeave * setlocal nocursorline
  45. " Show trailing white space
  46. autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
  47. autocmd InsertLeave * match ExtraWhitespace /\s\+$/
  48. " Source the vimrc file after saving it
  49. autocmd bufwritepost .vimrc source $MYVIMRC
  50. autocmd bufwritepost .vimrc call Pl#Load()
  51. " Reload Powerline colours after source
  52. " https://github.com/Lokaltog/vim-powerline/issues/28
  53. " if the last window is NERDTree, then close Vim
  54. "autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif
  55. else
  56. set autoindent on
  57. endif
  58. """"""""
  59. "" Start Up
  60. """""""""""""""""""""""""""""""""""""""""""""""""""
  61. function! StartUp()
  62. " start NERDTree if vim called with no arguments
  63. if 0 == argc()
  64. NERDTree
  65. end
  66. endfunction
  67. """"""""
  68. "" Tabs and Text Formatting
  69. """"""""""""""""""""""""""""""""""""""""""""""""""""""
  70. set expandtab " change to single spaces
  71. set tabstop=2 " actual tab press distance
  72. set shiftround " indent to nearest tabstops
  73. set shiftwidth=2 " amount to indent with > and <
  74. set smarttab " backspace tabs where appropriate even if spaces
  75. set softtabstop=2 " let backspace delete by indents
  76. set wrap lbr " wrap long lines of text
  77. set backspace=eol,start,indent "backspace over everything
  78. set textwidth=80 " try to keep text within 80 characters
  79. set colorcolumn=+1 " mark out the limits of the textwidth
  80. set listchars=trail:_,tab:>.,eol:$
  81. """"""""
  82. "" UI - Colours
  83. """"""""""""""""""""""""""""""""""""""""""""""""""""""
  84. syntax enable
  85. colorscheme solarized
  86. set t_Co=16
  87. set background=dark
  88. " set colour for folded lines
  89. highlight Folded term=none cterm=none ctermfg=darkred ctermbg=none
  90. " show trailing white space
  91. highlight ExtraWhitespace ctermfg=red ctermbg=red guifg=red guibg=red
  92. """"""""
  93. "" UI - Numbering
  94. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  95. set number " show line numbers
  96. "set relativenumber " current line always 0 (requires 7.3 and up)
  97. """"""""
  98. "" UI - Statusline (now using powerline)
  99. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  100. set laststatus=2 " status line is second last line (not hidden by commands)
  101. """"""""
  102. "" UI - Code Folding
  103. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  104. set foldmethod=indent
  105. set foldlevel=10
  106. set foldtext=FoldText()
  107. """"""""
  108. "" UI - Search
  109. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  110. set hlsearch " make searches highlighted
  111. set incsearch " vim will search as you type!
  112. set ignorecase " ignore case for searches
  113. set smartcase " well, unless a user puts in uppercase search characters
  114. set magic " enables wildcard searching
  115. """"""""
  116. "" Key Remaps
  117. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  118. let mapleader = "," " easier to use than \
  119. " More convenient escape
  120. imap ii <Esc>
  121. imap II <Esc>
  122. " Yank to end of line, like D deletes to end of line
  123. nmap Y y$
  124. " Clear trailing white space
  125. nmap <silent> <leader>$ :%s/\s\+$//g<CR>
  126. " Add extra lines up and down
  127. nmap <leader>j o<Esc>k
  128. nmap <leader>k O<Esc>j
  129. " Edit .vimrc
  130. nmap <leader>v :e $MYVIMRC<CR>
  131. nmap <silent> <leader>n :set number!<CR>
  132. nmap <silent> <leader>w :set wrap!<CR>
  133. " Toggle paste mode - no autoindenting of pasted material
  134. nmap <silent> <leader>p :set paste! paste?<CR>
  135. " Toggle visible whitespace characters
  136. nmap <silent> <leader>l :set list!<CR>
  137. " Toggle scrollbind for moving multiple splits in sync together
  138. nmap <silent> <leader>s :set scrollbind! scrollbind?<CR>
  139. " Toggle mouse support.
  140. nmap <silent> <leader>m :call ToggleMouse()<CR>
  141. " Toggle NERDTree file browser
  142. nmap <silent> <leader>d :NERDTreeToggle<CR>
  143. " Toggle Commenting out lines with NERDCommenter
  144. nmap <silent> <leader>, :call NERDComment("n", "toggle")<CR>
  145. vmap <silent> <leader>, <ESC>:call NERDComment("x", "toggle")<CR>
  146. " Traverse undo tree with Gundo!
  147. nmap <silent> <leader>u :GundoToggle<CR>
  148. " Git blame with Fugitive!
  149. nmap <silent> <leader>b :Gblame<CR>
  150. " Code heirarchy with Tagbar!
  151. nmap <silent> <leader>t :TagbarToggle<CR>
  152. """"""""
  153. "" Key Remaps - Movement and Windows
  154. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  155. " jump to beginning and end of line easier
  156. nmap H ^
  157. nmap L $
  158. " Smart way to move between windows
  159. nmap <C-j> <C-W>j
  160. nmap <C-k> <C-W>k
  161. nmap <C-h> <C-W>h
  162. nmap <C-l> <C-W>l
  163. " mapping to make movements operate on 1 screen line in wrap mode
  164. onoremap <silent> <expr> j ScreenMovement("j")
  165. onoremap <silent> <expr> k ScreenMovement("k")
  166. onoremap <silent> <expr> 0 ScreenMovement("0")
  167. onoremap <silent> <expr> ^ ScreenMovement("^")
  168. onoremap <silent> <expr> $ ScreenMovement("$")
  169. nnoremap <silent> <expr> j ScreenMovement("j")
  170. nnoremap <silent> <expr> k ScreenMovement("k")
  171. nnoremap <silent> <expr> 0 ScreenMovement("0")
  172. nnoremap <silent> <expr> ^ ScreenMovement("^")
  173. nnoremap <silent> <expr> $ ScreenMovement("$")
  174. """"""""
  175. "" Plugin options
  176. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  177. """"""""" NERDTree
  178. let g:NERDTreeQuitOnOpen = 1 " close sidebar after we go to selection
  179. """"""""" Gundo
  180. let g:gundo_width = 30
  181. let g:gundo_preview_height = 12
  182. let g:gundo_preview_bottom = 1
  183. """"""""" Powerline
  184. let g:Powerline_symbols = 'fancy' " alternatives: fancy, unicode
  185. """"""""" Tagbar
  186. let g:tagbar_autoclose = 1 " close sidebar after we go to selection
  187. """"""""" EasyGrep
  188. let g:EasyGrepJumpToMatch = 0
  189. let g:EasyGrepHighlightQfMatches = 1
  190. """"""""
  191. "" Functions
  192. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  193. function! FoldText()
  194. let line = getline(v:foldstart)
  195. let indent = indent(v:foldstart)
  196. let indentOnly = strpart(line, 0, indent-1)
  197. let linecount = v:foldend+1 - v:foldstart
  198. let foldtext = '+'.indentOnly.'... ('.linecount.' More lines)'
  199. return foldtext
  200. endfunction
  201. function! ToggleMouse()
  202. if &mouse == 'a'
  203. set mouse=
  204. echo "Mouse usage disabled"
  205. else
  206. set mouse=a
  207. echo "Mouse usage enabled"
  208. endif
  209. endfunction
  210. function! ScreenMovement(movement)
  211. if &wrap
  212. return "g" . a:movement
  213. else
  214. return a:movement
  215. endif
  216. endfunction