48a2cf2530cfd0f2dd8d3b56d1071c6337067af2
[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 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
65 " Handle options only available in Vim 5 and above.
66 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
67 if version >= "500"
68 version 5.0
69
70 " Tell Vim we use dark backgrounds in our terminals.
71 if ! has("gui_running")
72   se bg=dark
73 endif
74
75 " Enable tab-completion prompting for commands.
76 se wildmenu
77 " Don't list object files when globbing files to load.
78 se wildignore+=*.o,*.obj
79 " So there's no need to assign them low priority.
80 se suffixes-=*.o,*.obj
81
82 " Vim 5 hardcodes the size of numbers column to 8.
83 let numberwidth=8
84
85 " Save sessions in UNIX format with / as file separator.  This is
86 " cross-platform.
87 se ssop+=unix,slash
88
89 " Nuke any pre-existing autocommands.
90 autocmd!
91
92 " Save the current window width so we can restore it when we quit.
93 let oldcols=&columns
94
95 " More GUI options.  Add icon, tearoffs and toolbar.
96 se go+=itT
97
98 " Allow dynamic window resize even if we aren't in an xterm.
99 se t_WS=\e[8;%p1%d;%p2%dt
100
101 " Highlight search results.
102 se hlsearch
103
104 " Syntax highlighting.  New versions will use syn enable instead.
105 if version < 600
106   syn on
107 endif
108
109 " Use a discernably different colour to highlight the cursor which shows 
110 " matching brackets.  Our regular cursor is green so use blue instead of cyan.
111 hi MatchParen ctermbg=blue
112
113 " Catch typos.
114 command! W :w
115 command! Wq :wq
116 command! Wqa :wqa
117
118 " Set up our variables.
119 fun! Iain_Vars()
120   if ! exists("b:iainlist")
121     let b:iainlist = 0
122   endif
123   if ! exists("b:iainhex")
124     let b:iainhex = 0
125   endif
126   if ! exists("b:iainverbose")
127     let b:iainverbose = 0
128   endif
129   if ! exists("b:iainstatus")
130     " Window Flags: (F)ocused, (I)nsert mode, Cursor (H)old.
131     let b:iainstatus = "Fih"
132   endif
133 endfun
134
135 " Helper for status line.
136 " Show space, underscore or dollar sign depending on list mode.
137 fun! Show_List()
138   call Iain_Vars()
139   if b:iainlist == 0
140     " No list.
141     return " "
142   elseif b:iainlist == 1
143     " Just tabs.
144     return "_"
145   else
146     " Full list.
147     return "\$"
148   endif
149 endfun
150
151 " Helper for status line.
152 " Show c or C to denote case-sensitivity.
153 fun! Show_Case()
154   if &ic
155     return "c"
156   else
157     return "C"
158   endif
159 endfun
160
161 " Helper for status line.
162 " Show the size of the tabstop.
163 fun! Show_Tabstop()
164   return &ts
165 endfun
166
167 " Helper for status line.
168 " Show p when paste mode is on.
169 fun! Show_Paste()
170   if &paste
171     return "p"
172   else
173     return ""
174   endif
175 endfun
176
177 " Show the status line.
178 fun! Show_StatusLine()
179   call Iain_Vars()
180   let sl1='%2n\:\ %<%f\ [%{Show_List()}%{Show_Case()}%{Show_Tabstop()}%{Show_Paste()}%Y%M%R]\ %='
181   let sl3='L:%4.6l/%-4.6L\ C:%3.6c\ \|\ %P'
182   let hexformat='%b'
183   if b:iainhex
184     let hexformat='0\x%02B'
185   endif
186   if b:iainverbose
187     let sl2=hexformat . '\ \|\ P:%4.6o\ '
188   else
189     let sl2=''
190   endif
191   exec "set statusline=" . sl1 . sl2 . sl3
192 endfun
193
194 " Toggle case-sensitivity.
195 fun! Invert_Case()
196   let &ic = ! &ic
197 endfun
198
199 " Restore window size.
200 au VimLeave * if exists("oldcols") | let &columns=oldcols | endif
201
202 " Map Makefile mode.
203 au BufEnter * if &ft == "make" | call MakeMode_map() | endif
204 au BufLeave * if &ft == "make" | call MakeMode_unmap() | endif
205
206 " Entering Make mode.
207 fun! MakeMode_map()
208         call Iain_Vars()
209   let b:iainlist=1
210   call Cycle_List()
211   set ts=8
212   set noexpandtab
213 endfun
214
215 " Leaving Make mode.
216 fun! MakeMode_unmap()
217   call Cycle_List()
218   set ts=2
219   set expandtab
220 endfun
221
222 " Show the status line for the first time.
223 call Show_StatusLine()
224
225 " Function to create mappings with either a hardcoded \ or <Leader>.
226 fun! Mapping(keysequence,mapping)
227   if version >= "600"
228     exec "map \\" . a:keysequence . " " . a:mapping
229   else
230     exec "map <Leader>" . a:keysequence . " " . a:mapping
231   endif
232 endfun
233
234 " Use - and = to create underlines.
235 call Mapping("-", "yyp:s/./-/g<RETURN>:let @/=''<RETURN>:<RETURN>")
236 call Mapping("=", "yyp:s/./=/g<RETURN>:let @/=''<RETURN>:<RETURN>")
237
238 " Change to ts=2 with \2.
239 call Mapping("2", ":se ts=2<CR>:<CR>")
240 " Change to ts=4 with \4.
241 call Mapping("4", ":se ts=4<CR>:<CR>")
242 " Change to ts=8 with \8.
243 call Mapping("8", ":se ts=8<CR>:<CR>")
244 " Change to ts=16 with \6.
245 call Mapping("6", ":se ts=16<CR>:<CR>")
246 " Change to ts=32 with \3.
247 call Mapping("3", ":se ts=32<CR>:<CR>")
248 " Toggle paste mode with \p.
249 call Mapping("p", ":se paste!<CR>:<CR>")
250 " Swap case-sensitivity with \c.
251 call Mapping("c", ":call Invert_Case()<CR>:<CR>")
252 " Change number mode with \n.
253 call Mapping("n", ":se number!<CR>:<CR>")
254 " Expand or shrink window size with \> and \<.  For use after toggling number.
255 call Mapping(">", ":exe 'se columns+=' . numberwidth<CR>:<CR>")
256 call Mapping("<", ":exe 'se columns-=' . numberwidth<CR>:<CR>")
257 " Clear search pattern with \/.
258 call Mapping("/", ":let @/=\"\"<CR>:<CR>")
259
260 " Forget the Ex mode mapping.
261 map Q <NOP>
262
263 " Vim tip 99: What's the highlighting group under the cursor?
264 call Mapping("h", ":echo \"hi<\" . synIDattr(synID(line(\".\"),col(\".\"),1),\"name\") . '> trans<' . synIDattr(synID(line(\".\"),col(\".\"),0),\"name\") . \"> lo<\" . synIDattr(synIDtrans(synID(line(\".\"),col(\".\"),1)),\"name\") . \">\"<CR>")
265
266 endif
267
268 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
269 " Handle options only available in Vim 6 and above.
270 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
271 if version >= "600"
272 version 6.0
273
274 " Remember quickfix state.
275 let g:quickfixing=0
276
277 " Set indenting by filetype.
278 filetype indent on
279
280 " Less intrusive syntax highlighting.
281 syn enable
282
283 " Nice GUI colour.
284 if has("gui_running")
285   se guifont=DejaVu\ Sans\ Mono\ 10
286   colo darkblue
287 elseif &t_Co > 16
288   try
289     colo iain
290   catch
291   endtry
292 endif
293 if has("win32")
294   se guifont=DejaVu_Sans_Mono:h10:cANSI
295 endif
296
297 " Ignore whitespace when diffing.
298 se diffopt=filler,iwhite
299
300 " Expand window when doing a vertical diff.
301 if &diff
302   let &columns = 164
303 endif
304
305 " Remember that we are opening the quickfix window.
306 au BufWinEnter quickfix let g:quickfixing=1
307 au BufUnload * if &ft == "qf" | let g:quickfixing=0 | endif
308
309 " Allow in-place editing of crontabs.
310 au FileType crontab set backupcopy=yes
311
312 " Make * and # work the way you expect in visual mode.
313 vnoremap * y/\V<C-R>=substitute(escape(@@,"/\\"),"\n","\\\\n","ge")<CR><CR>
314 vnoremap # y?\V<C-R>=substitute(escape(@@,"?\\"),"\n","\\\\n","ge")<CR><CR>
315
316 " Change list mode.
317 fun! Cycle_List()
318   let basic='tab:\\_,trail:_,extends:<,precedes:>'
319   call Iain_Vars()
320   let b:iainlist = b:iainlist + 1
321   if b:iainlist > 2
322     let b:iainlist = 0
323   endif
324   if b:iainlist == 0
325     set nolist
326   elseif b:iainlist == 1
327     exec "set lcs=" . basic
328     set list
329   else
330     exec "set lcs=" . basic . ",eol:$"
331     set list
332   endif
333 endfun
334
335 " Cycle between hex and decimal display of toolbar stuff.
336 fun! Cycle_HexStatusLine()
337   call Iain_Vars()
338   let b:iainhex = ! b:iainhex
339   call Show_StatusLine()
340 endfun
341
342 " Cycle verbose display of toolbar stuff.
343 fun! Cycle_VerboseStatusLine()
344   call Iain_Vars()
345   let b:iainverbose = ! b:iainverbose
346   call Show_StatusLine()
347 endfun
348
349 " Toggle quickfix window.
350 fun! Cycle_Quickfix()
351   if g:quickfixing == 1
352     cclose
353     let g:quickfixing=0
354   else
355     copen
356   endif
357 endfun
358
359 " Swap hex/decimal statusline with \x.
360 call Mapping("x", ":call Cycle_HexStatusLine()<CR>:<CR>")
361 " Change statusline verbosity with \v.
362 call Mapping("v", ":call Cycle_VerboseStatusLine()<CR>:<CR>")
363 " Cycle list styles with \l.
364 call Mapping("l", ":call Cycle_List()<CR>:<CR>")
365 " Toggle tags with \t.
366 call Mapping("t", ":Tlist<CR>")
367 " Change foldmethod with \f.
368 call Mapping("f", ":se foldenable!<CR>:<CR>")
369 " Toggle quickfix window with \q.
370 call Mapping("q", ":call Cycle_Quickfix()<CR>:<CR>")
371 " Rerun filetype detection with \s.  The s is for syntax, as this will be
372 " updated as a side-effect.
373 call Mapping("s", ":filetype detect<CR>:<CR>")
374
375 " Change status bar colour when various things happen.
376 fun! Highlight_StatusLine(flag)
377   " Get current status.
378   call Iain_Vars()
379
380   " Change the status based on the flag.  XXX: Does Vim let us to do flags?
381   let re = "[" . tolower(a:flag) . toupper(a:flag) . "]"
382   let b:iainstatus = substitute(b:iainstatus, re, a:flag, "")
383
384   " Default colour.
385   let s:colour = "darkblue"
386   let s:termcolour = ""
387   let s:term88colour = "17"
388   let s:term256colour = "17"
389   " Maybe override depending on status.
390   if b:iainstatus =~# "H"
391     if b:iainstatus =~# "I"
392       " Held in insert mode.  Add extra highlight if we don't have focus.
393       if b:iainstatus =~# "f"
394         let s:colour = "darkmagenta"
395       else
396         let s:colour = "darkred"
397       endif
398       let s:term88colour = "32"
399       let s:term256colour = "88"
400     endif
401   else
402     if b:iainstatus =~# "I"
403       " Regular insert mode.
404       let s:colour = "darkred"
405       let s:term88colour = "32"
406       let s:term256colour = "88"
407     endif
408   endif
409
410   if &t_Co == 88
411     let s:termcolour = s:term88colour
412   elseif &t_Co == 256
413     let s:termcolour = s:term256colour
414   else
415     let s:termcolour = s:colour
416   endif
417
418   exec "highlight StatusLine guifg=white guibg=" . s:colour . " ctermbg=white ctermfg=" . s:termcolour
419 endfun
420
421 call Highlight_StatusLine("")
422 endif
423
424 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
425 " Handle options only available in Vim 7 and above.
426 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
427 if version >= "700"
428 version 7.0
429
430 au CursorHoldI * call Highlight_StatusLine("H")
431 au CursorMovedI * call Highlight_StatusLine("h")
432 au FocusGained * call Highlight_StatusLine("F")
433 au FocusLost * call Highlight_StatusLine("f")
434 au InsertEnter * call Highlight_StatusLine("I")
435 au InsertLeave * call Highlight_StatusLine("i")
436
437 " Make diffs vertical by default.
438 se diffopt+=vertical
439
440 " Set size of numbers column.
441 se numberwidth=5
442
443 " Add "previous tab" mapping as gb.
444 map gb :tabPrev<CR>
445 endif