Use \ or <Leader> instead of Q for mappings.
[profile.git] / .vimrc
1 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2 " $Id$
3 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
4 " Multi-version vimrc compatible with version 4 and above.
5 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
6
7 " Note that "if <condition> | call Something() | endif" syntax is unsupported 
8 " in Vim 4 so we write all our functions out the long way.  It does work in 
9 " autocommand definitions, however.
10
11 " Vim 4 complains if version isn't set in the configuration file.
12 version 4.0
13
14 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
15 " Handle options safe to use in version 4.  Vim 4 parses but ignores the 
16 " "if version" syntax used later in this file so we don't use it.  No attempt 
17 " is made to make this configuration compatible with Vim 3.
18 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
19 " No compatibility mode.
20 se nocp
21
22 " Tabstop 2.
23 se ts=2
24 " And use spaces not tabs.
25 se expandtab
26 " And << and >> indent by 2.
27 se sw=2
28
29 " Allow backspace to delete before start of line.
30 se bs=2
31
32 " Show the ruler.
33 se ruler
34 " Show partial commands in the ruler.
35 se showcmd
36 " And always show the status line.
37 se laststatus=2
38
39 " Use C indent style.
40 se cindent
41 se cinkeys=0{,0},0),:,!^F,o,O,e
42 se cinoptions=b1,c2
43
44 " GUI options.
45 se go=aglmr
46
47 " Don't be bugged by messages at the bottom of the screen.
48 se shm=aot
49
50 " Find as you type.
51 se incsearch
52
53 " Case-insensitive search.
54 se ignorecase
55 " But override by typing capitals.
56 se smartcase
57
58 " Look for ctags in home directory first.
59 se tags=~/.tags,./tags,tags
60
61 " Don't timeout waiting to interpet, eg, <ESC>OA as an escape code.
62 se ttimeoutlen=100
63
64 " Function to create mappings with either a hardcoded \ or <Leader>.
65 fun! Mapping(keysequence,mapping)
66   if version >= "600"
67     exec "map \\" . a:keysequence . " " . a:mapping
68   else
69     exec "map <Leader>" . a:keysequence . " " . a:mapping
70   endif
71 endfun
72
73 " Use - and = to create underlines.
74 call Mapping("-", "yyp:s/./-/g<RETURN>:let @/=''<RETURN>:<RETURN>")
75 call Mapping("=", "yyp:s/./=/g<RETURN>:let @/=''<RETURN>:<RETURN>")
76
77 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
78 " Handle options only available in Vim 5 and above.
79 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
80 if version >= "500"
81 version 5.0
82
83 " Tell Vim we use dark backgrounds in our terminals.
84 if ! has("gui_running")
85   se bg=dark
86 endif
87
88 " Vim 5 hardcodes the size of numbers column to 8.
89 let numberwidth=8
90
91 " Save sessions in UNIX format with / as file separator.  This is
92 " cross-platoform.
93 se ssop+=unix,slash
94
95 " Nuke any pre-existing autocommands.
96 autocmd!
97
98 " Save the current window width so we can restore it when we quit.
99 let oldcols=&columns
100
101 " More GUI options.  Add icon, tearoffs and toolbar.
102 se go+=itT
103
104 " Allow dynamic window resize even if we aren't in an xterm.
105 se t_WS=\e[8;%p1%d;%p2%dt
106
107 " Highlight search results.
108 se hlsearch
109
110 " Syntax highlighting.
111 syn on
112
113 " Use a discernably different colour to highlight the cursor which shows 
114 " matching brackets.  Our regular cursor is green so use blue instead of cyan.
115 hi MatchParen ctermbg=blue
116
117 " Catch typos.
118 command! W :w
119 command! Wq :wq
120 command! Wqa :wqa
121
122 " Set up our variables.
123 fun! Iain_Vars()
124   if ! exists("b:iainlist")
125     let b:iainlist = 0
126   endif
127   if ! exists("b:iainhex")
128     let b:iainhex = 0
129   endif
130   if ! exists("b:iainverbose")
131     let b:iainverbose = 0
132   endif
133   if ! exists("b:iainstatus")
134     " Window Flags: (F)ocused, (I)nsert mode, Cursor (H)old.
135     let b:iainstatus = "Fih"
136   endif
137 endfun
138
139 " Helper for status line.
140 " Show space, underscore or dollar sign depending on list mode.
141 fun! Show_List()
142   call Iain_Vars()
143   if b:iainlist == 0
144     " No list.
145     return " "
146   elseif b:iainlist == 1
147     " Just tabs.
148     return "_"
149   else
150     " Full list.
151     return "\$"
152   endif
153 endfun
154
155 " Helper for status line.
156 " Show c or C to denote case-sensitivity.
157 fun! Show_Case()
158   if &ic
159     return "c"
160   else
161     return "C"
162   endif
163 endfun
164
165 " Helper for status line.
166 " Show the size of the tabstop.
167 fun! Show_Tabstop()
168   return &ts
169 endfun
170
171 " Helper for status line.
172 " Show p when paste mode is on.
173 fun! Show_Paste()
174   if &paste
175     return "p"
176   else
177     return ""
178   endif
179 endfun
180
181 " Show the status line.
182 fun! Show_StatusLine()
183   call Iain_Vars()
184   let sl1='%2n\:\ %<%f\ [%{Show_List()}%{Show_Case()}%{Show_Tabstop()}%{Show_Paste()}%Y%M%R]\ %='
185   let sl3='L:%4.6l/%-4.6L\ C:%3.6c\ \|\ %P'
186   let hexformat='%b'
187   if b:iainhex
188     let hexformat='0\x%02B'
189   endif
190   if b:iainverbose
191     let sl2=hexformat . '\ \|\ P:%4.6o\ '
192   else
193     let sl2=''
194   endif
195   exec "set statusline=" . sl1 . sl2 . sl3
196 endfun
197
198 " Toggle case-sensitivity.
199 fun! Invert_Case()
200   let &ic = ! &ic
201 endfun
202
203 " Restore window size.
204 au VimLeave * if exists("oldcols") | let &columns=oldcols | endif
205
206 " Map Makefile mode.
207 au BufEnter * if &ft == "make" | call MakeMode_map() | endif
208 au BufLeave * if &ft == "make" | call MakeMode_unmap() | endif
209
210 " Entering Make mode.
211 fun! MakeMode_map()
212   set list
213   set noexpandtab
214 endfun
215
216 " Leaving Make mode.
217 fun! MakeMode_unmap()
218   set nolist
219   set expandtab
220 endfun
221
222 " Show the status line for the first time.
223 call Show_StatusLine()
224
225 " Change to ts=2 with \2.
226 call Mapping("2", ":se ts=2<CR>:<CR>")
227 " Change to ts=4 with \4.
228 call Mapping("4", ":se ts=4<CR>:<CR>")
229 " Change to ts=8 with \8.
230 call Mapping("8", ":se ts=8<CR>:<CR>")
231 " Change to ts=16 with \6.
232 call Mapping("6", ":se ts=16<CR>:<CR>")
233 " Change to ts=32 with \3.
234 call Mapping("3", ":se ts=32<CR>:<CR>")
235 " Toggle paste mode with \p.
236 call Mapping("p", ":se paste!<CR>:<CR>")
237 " Swap case-sensitivity with \c.
238 call Mapping("c", ":call Invert_Case()<CR>:<CR>")
239 " Change number mode with \n.
240 call Mapping("n", ":se number!<CR>:<CR>")
241 " Expand or shrink window size with \> and \<.  For use after toggling number.
242 call Mapping(">", ":exe 'se columns+=' . numberwidth<CR>:<CR>")
243 call Mapping("<", ":exe 'se columns-=' . numberwidth<CR>:<CR>")
244 " Clear search pattern with \/.
245 call Mapping("/", ":let @/=\"\"<CR>:<CR>")
246
247 " Forget the Ex mode mapping.
248 map Q <NOP>
249
250 endif
251
252 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
253 " Handle options only available in Vim 6 and above.
254 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
255 if version >= "600"
256 version 6.0
257
258 " Remember quickfix state.
259 let g:quickfixing=0
260
261 " Set indenting by filetype.
262 filetype indent on
263
264 " Less intrusive syntax highlighting.
265 syn enable
266
267 " Nice GUI colour.
268 if has("gui_running")
269   se guifont=DejaVu\ Sans\ Mono\ 10
270   colo darkblue
271 endif
272 if has("win32")
273   se guifont=DejaVu_Sans_Mono:h10:cANSI
274 endif
275
276 " Ignore whitespace when diffing.
277 se diffopt=filler,iwhite
278
279 " Expand window when doing a vertical diff.
280 if &diff
281   let &columns = 164
282 endif
283
284 " Numbers in blue.
285 highlight LineNr term=underline cterm=bold guifg=blue ctermfg=blue
286
287 " Remember that we are opening the quickfix window.
288 au BufWinEnter quickfix let g:quickfixing=1
289 au BufUnload * if &ft == "qf" | let g:quickfixing=0 | endif
290
291 " Make * and # work the way you expect in visual mode.
292 vnoremap * y/\V<C-R>=substitute(escape(@@,"/\\"),"\n","\\\\n","ge")<CR><CR>
293 vnoremap # y?\V<C-R>=substitute(escape(@@,"?\\"),"\n","\\\\n","ge")<CR><CR>
294
295 " Change list mode.
296 fun! Cycle_List()
297   let basic='tab:\\_,trail:_,extends:<,precedes:>'
298   call Iain_Vars()
299   let b:iainlist = b:iainlist + 1
300   if b:iainlist > 2
301     let b:iainlist = 0
302   endif
303   if b:iainlist == 0
304     set nolist
305   elseif b:iainlist == 1
306     exec "set lcs=" . basic
307     set list
308   else
309     exec "set lcs=" . basic . ",eol:$"
310     set list
311   endif
312 endfun
313
314 " Cycle between hex and decimal display of toolbar stuff.
315 fun! Cycle_HexStatusLine()
316   call Iain_Vars()
317   let b:iainhex = ! b:iainhex
318   call Show_StatusLine()
319 endfun
320
321 " Cycle verbose display of toolbar stuff.
322 fun! Cycle_VerboseStatusLine()
323   call Iain_Vars()
324   let b:iainverbose = ! b:iainverbose
325   call Show_StatusLine()
326 endfun
327
328 " Toggle quickfix window.
329 fun! Cycle_Quickfix()
330   if g:quickfixing == 1
331     cclose
332     let g:quickfixing=0
333   else
334     copen
335   endif
336 endfun
337
338 " Swap hex/decimal statusline with \x.
339 call Mapping("x", ":call Cycle_HexStatusLine()<CR>:<CR>")
340 " Change statusline verbosity with \v.
341 call Mapping("v", ":call Cycle_VerboseStatusLine()<CR>:<CR>")
342 " Cycle list styles with \l.
343 call Mapping("l", ":call Cycle_List()<CR>:<CR>")
344 " Toggle tags with \t.
345 call Mapping("t", ":Tlist<CR>")
346 " Change foldmethod with \f.
347 call Mapping("f", ":se foldenable!<CR>:<CR>")
348 " Toggle quickfix window with \q.
349 call Mapping("q", ":call Cycle_Quickfix()<CR>:<CR>")
350 " Rerun filetype detection with \s.  The s is for syntax, as this will be
351 " updated as a side-effect.
352 call Mapping("s", ":filetype detect<CR>:<CR>")
353
354 " Change status bar colour when various things happen.
355 fun! Highlight_StatusLine(flag)
356   " Get current status.
357   call Iain_Vars()
358
359   " Change the status based on the flag.  XXX: Does Vim let us to do flags?
360   let re = "[" . tolower(a:flag) . toupper(a:flag) . "]"
361   let b:iainstatus = substitute(b:iainstatus, re, a:flag, "")
362
363   " Default colour.
364   let s:colour = "darkblue"
365   let s:termcolour = ""
366   let s:term88colour = "17"
367   let s:term256colour = "17"
368   " Maybe override depending on status.
369   if b:iainstatus =~# "H"
370     if b:iainstatus =~# "I"
371       " Held in insert mode.  Add extra highlight if we don't have focus.
372       if b:iainstatus =~# "f"
373         let s:colour = "darkmagenta"
374       else
375         let s:colour = "darkred"
376       endif
377       let s:term88colour = "32"
378       let s:term256colour = "88"
379     endif
380   else
381     if b:iainstatus =~# "I"
382       " Regular insert mode.
383       let s:colour = "darkred"
384       let s:term88colour = "32"
385       let s:term256colour = "88"
386     endif
387   endif
388
389   if &t_Co == 88
390     let s:termcolour = s:term88colour
391   elseif &t_Co == 256
392     let s:termcolour = s:term256colour
393   else
394     let s:termcolour = s:colour
395   endif
396
397   exec "highlight StatusLine guifg=white guibg=" . s:colour . " ctermbg=white ctermfg=" . s:termcolour
398 endfun
399
400 call Highlight_StatusLine("")
401 endif
402
403 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
404 " Handle options only available in Vim 7 and above.
405 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
406 if version >= "700"
407 version 7.0
408
409 au CursorHoldI * call Highlight_StatusLine("H")
410 au CursorMovedI * call Highlight_StatusLine("h")
411 au FocusGained * call Highlight_StatusLine("F")
412 au FocusLost * call Highlight_StatusLine("f")
413 au InsertEnter * call Highlight_StatusLine("I")
414 au InsertLeave * call Highlight_StatusLine("i")
415
416 " Make diffs vertical by default.
417 se diffopt+=vertical
418
419 " Set size of numbers column.
420 se numberwidth=5
421
422 " Add "previous tab" mapping as gb.
423 map gb :tabPrev<CR>
424 endif