.vimrc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. " -*- coding: utf-8; mode: conf-space; comment-start: "\" "; -*-
  2. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  3. " Maintainer:
  4. " Amir Salihefendic — @amix3k
  5. "
  6. " Awesome_version:
  7. " Get this config, nice color schemes and lots of plugins!
  8. "
  9. " Install the awesome version from:
  10. "
  11. " https://github.com/amix/vimrc
  12. "
  13. " Sections:
  14. " -> General
  15. " -> VIM user interface
  16. " -> Colors and Fonts
  17. " -> Files and backups
  18. " -> Text, tab and indent related
  19. " -> Visual mode related
  20. " -> Moving around, tabs and buffers
  21. " -> Status line
  22. " -> Editing mappings
  23. " -> vimgrep searching and cope displaying
  24. " -> Spell checking
  25. " -> Misc
  26. " -> Helper functions
  27. "
  28. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  29. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  30. " => General
  31. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  32. " mouse scroll
  33. set mouse=a
  34. " Sets how many lines of history VIM has to remember
  35. set history=500
  36. " Enable filetype plugins
  37. filetype plugin on
  38. filetype indent on
  39. " Set to auto read when a file is changed from the outside
  40. set autoread
  41. au FocusGained,BufEnter * checktime
  42. " With a map leader it's possible to do extra key combinations
  43. " like <leader>w saves the current file
  44. let mapleader = ","
  45. " Fast saving
  46. nmap <leader>w :w!<cr>
  47. " :W sudo saves the file
  48. " (useful for handling the permission-denied error)
  49. command! W execute 'w !sudo tee % > /dev/null' <bar> edit!
  50. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  51. " => VIM user interface
  52. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  53. " Set 7 lines to the cursor - when moving vertically using j/k
  54. set so=7
  55. " Avoid garbled characters in Chinese language windows OS
  56. let $LANG='en'
  57. set langmenu=en
  58. source $VIMRUNTIME/delmenu.vim
  59. source $VIMRUNTIME/menu.vim
  60. " Turn on the Wild menu
  61. set wildmenu
  62. " Ignore compiled files
  63. set wildignore=*.o,*~,*.pyc
  64. if has("win16") || has("win32")
  65. set wildignore+=.git\*,.hg\*,.svn\*
  66. else
  67. set wildignore+=*/.git/*,*/.hg/*,*/.svn/*,*/.DS_Store
  68. endif
  69. "Always show current position
  70. set ruler
  71. " Height of the command bar
  72. set cmdheight=1
  73. " A buffer becomes hidden when it is abandoned
  74. set hid
  75. " Configure backspace so it acts as it should act
  76. set backspace=eol,start,indent
  77. set whichwrap+=<,>,h,l
  78. " Ignore case when searching
  79. set ignorecase
  80. " When searching try to be smart about cases
  81. set smartcase
  82. " Highlight search results
  83. set hlsearch
  84. " Makes search act like search in modern browsers
  85. set incsearch
  86. " Don't redraw while executing macros (good performance config)
  87. set lazyredraw
  88. " For regular expressions turn magic on
  89. set magic
  90. " Show matching brackets when text indicator is over them
  91. set showmatch
  92. " How many tenths of a second to blink when matching brackets
  93. set mat=2
  94. " No annoying sound on errors
  95. set noerrorbells
  96. set novisualbell
  97. set t_vb=
  98. set tm=500
  99. " Properly disable sound on errors on MacVim
  100. if has("gui_macvim")
  101. autocmd GUIEnter * set vb t_vb=
  102. endif
  103. " Add a bit extra margin to the left
  104. set foldcolumn=1
  105. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  106. " => Colors and Fonts
  107. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  108. " Enable syntax highlighting
  109. syntax enable
  110. " Enable 256 colors palette in Gnome Terminal
  111. if $COLORTERM == 'gnome-terminal'
  112. set t_Co=256
  113. endif
  114. " try
  115. " colorscheme desert
  116. " catch
  117. " endtry
  118. set background=dark
  119. " Set extra options when running in GUI mode
  120. if has("gui_running")
  121. set guioptions-=T
  122. set guioptions-=e
  123. set t_Co=256
  124. set guitablabel=%M\ %t
  125. endif
  126. " Set utf8 as standard encoding and en_US as the standard language
  127. set encoding=utf8
  128. " Use Unix as the standard file type
  129. set ffs=unix,dos,mac
  130. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  131. " => Files, backups and undo
  132. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  133. " Turn backup off, since most stuff is in SVN, git etc. anyway...
  134. set nobackup
  135. set nowb
  136. set noswapfile
  137. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  138. " => Text, tab and indent related
  139. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  140. " Use spaces instead of tabs
  141. set expandtab
  142. " Be smart when using tabs ;)
  143. set smarttab
  144. " 1 tab == 4 spaces
  145. set shiftwidth=4
  146. set tabstop=4
  147. " Linebreak on 500 characters
  148. set lbr
  149. set tw=500
  150. set ai "Auto indent
  151. set si "Smart indent
  152. set wrap "Wrap lines
  153. """"""""""""""""""""""""""""""
  154. " => Visual mode related
  155. """"""""""""""""""""""""""""""
  156. " Visual mode pressing * or # searches for the current selection
  157. " Super useful! From an idea by Michael Naumann
  158. vnoremap <silent> * :<C-u>call VisualSelection('', '')<CR>/<C-R>=@/<CR><CR>
  159. vnoremap <silent> # :<C-u>call VisualSelection('', '')<CR>?<C-R>=@/<CR><CR>
  160. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  161. " => Moving around, tabs, windows and buffers
  162. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  163. " Map <Space> to / (search) and Ctrl-<Space> to ? (backwards search)
  164. map <space> /
  165. map <C-space> ?
  166. " Disable highlight when <leader><cr> is pressed
  167. map <silent> <leader><cr> :noh<cr>
  168. " Smart way to move between windows
  169. map <C-j> <C-W>j
  170. map <C-k> <C-W>k
  171. map <C-h> <C-W>h
  172. map <C-l> <C-W>l
  173. " Close the current buffer
  174. map <leader>bd :Bclose<cr>:tabclose<cr>gT
  175. " Close all the buffers
  176. map <leader>ba :bufdo bd<cr>
  177. map <leader>l :bnext<cr>
  178. map <leader>h :bprevious<cr>
  179. " Useful mappings for managing tabs
  180. map <leader>tn :tabnew<cr>
  181. map <leader>to :tabonly<cr>
  182. map <leader>tc :tabclose<cr>
  183. map <leader>tm :tabmove
  184. map <leader>t<leader> :tabnext
  185. " Let 'tl' toggle between this and the last accessed tab
  186. let g:lasttab = 1
  187. nmap <Leader>tl :exe "tabn ".g:lasttab<CR>
  188. au TabLeave * let g:lasttab = tabpagenr()
  189. " Opens a new tab with the current buffer's path
  190. " Super useful when editing files in the same directory
  191. map <leader>te :tabedit <C-r>=expand("%:p:h")<cr>/
  192. " Switch CWD to the directory of the open buffer
  193. map <leader>cd :cd %:p:h<cr>:pwd<cr>
  194. " Specify the behavior when switching between buffers
  195. try
  196. set switchbuf=useopen,usetab,newtab
  197. set stal=2
  198. catch
  199. endtry
  200. " Return to last edit position when opening files (You want this!)
  201. au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
  202. """"""""""""""""""""""""""""""
  203. " => Status line
  204. """"""""""""""""""""""""""""""
  205. " Always show the status line
  206. set laststatus=2
  207. " Format the status line
  208. set statusline=\ %{HasPaste()}%F%m%r%h\ %w\ \ CWD:\ %r%{getcwd()}%h\ \ \ Line:\ %l\ \ Column:\ %c
  209. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  210. " => Editing mappings
  211. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  212. " Remap VIM 0 to first non-blank character
  213. map 0 ^
  214. " Move a line of text using ALT+[jk] or Command+[jk] on mac
  215. nmap <M-j> mz:m+<cr>`z
  216. nmap <M-k> mz:m-2<cr>`z
  217. vmap <M-j> :m'>+<cr>`<my`>mzgv`yo`z
  218. vmap <M-k> :m'<-2<cr>`>my`<mzgv`yo`z
  219. if has("mac") || has("macunix")
  220. nmap <D-j> <M-j>
  221. nmap <D-k> <M-k>
  222. vmap <D-j> <M-j>
  223. vmap <D-k> <M-k>
  224. endif
  225. " Delete trailing white space on save, useful for some filetypes ;)
  226. fun! CleanExtraSpaces()
  227. let save_cursor = getpos(".")
  228. let old_query = getreg('/')
  229. silent! %s/\s\+$//e
  230. call setpos('.', save_cursor)
  231. call setreg('/', old_query)
  232. endfun
  233. if has("autocmd")
  234. autocmd BufWritePre *.txt,*.js,*.py,*.wiki,*.sh,*.coffee :call CleanExtraSpaces()
  235. endif
  236. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  237. " => Spell checking
  238. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  239. " Pressing ,ss will toggle and untoggle spell checking
  240. map <leader>ss :setlocal spell!<cr>
  241. " Shortcuts using <leader>
  242. map <leader>sn ]s
  243. map <leader>sp [s
  244. map <leader>sa zg
  245. map <leader>s? z=
  246. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  247. " => Misc
  248. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  249. " Remove the Windows ^M - when the encodings gets messed up
  250. noremap <Leader>m mmHmt:%s/<C-V><cr>//ge<cr>'tzt'm
  251. " Quickly open a buffer for scribble
  252. map <leader>q :e ~/buffer<cr>
  253. " Quickly open a markdown buffer for scribble
  254. map <leader>x :e ~/buffer.md<cr>
  255. " Toggle paste mode on and off
  256. map <leader>pp :setlocal paste!<cr>
  257. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  258. " => Helper functions
  259. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  260. " Returns true if paste mode is enabled
  261. function! HasPaste()
  262. if &paste
  263. return 'PASTE MODE '
  264. endif
  265. return ''
  266. endfunction
  267. " Don't close window, when deleting a buffer
  268. command! Bclose call <SID>BufcloseCloseIt()
  269. function! <SID>BufcloseCloseIt()
  270. let l:currentBufNum = bufnr("%")
  271. let l:alternateBufNum = bufnr("#")
  272. if buflisted(l:alternateBufNum)
  273. buffer #
  274. else
  275. bnext
  276. endif
  277. if bufnr("%") == l:currentBufNum
  278. new
  279. endif
  280. if buflisted(l:currentBufNum)
  281. execute("bdelete! ".l:currentBufNum)
  282. endif
  283. endfunction
  284. function! CmdLine(str)
  285. call feedkeys(":" . a:str)
  286. endfunction
  287. function! VisualSelection(direction, extra_filter) range
  288. let l:saved_reg = @"
  289. execute "normal! vgvy"
  290. let l:pattern = escape(@", "\\/.*'$^~[]")
  291. let l:pattern = substitute(l:pattern, "\n$", "", "")
  292. if a:direction == 'gv'
  293. call CmdLine("Ack '" . l:pattern . "' " )
  294. elseif a:direction == 'replace'
  295. call CmdLine("%s" . '/'. l:pattern . '/')
  296. endif
  297. let @/ = l:pattern
  298. let @" = l:saved_reg
  299. endfunction