Use vim for manpages and infopages.
[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 " Use - and = to create underlines.
62 map - yyp:s/./-/g<RETURN>:let @/=''<RETURN>:<RETURN>
63 map = yyp:s/./=/g<RETURN>:let @/=''<RETURN>:<RETURN>
64
65 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
66 " Handle options only available in Vim 5 and above.
67 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
68 if version >= "500"
69 version 5.0
70
71 " Nuke any pre-existing autocommands.
72 autocmd!
73
74 " Save the current window width so we can restore it when we quit.
75 let oldcols=&columns
76
77 " More GUI options.  Add icon, tearoffs and toolbar.
78 se go=agilmrtT
79
80 " Allow dynamic window resize even if we aren't in an xterm.
81 se t_WS=\e[8;%p1%d;%p2%dt
82
83 " Highlight search results.
84 se hlsearch
85
86 " Syntax highlighting.
87 syn on
88
89 " Catch typos.
90 command! W :w
91 command! Wq :wq
92 command! Wqa :wqa
93
94 " Set up our variables.
95 fun! Iain_Vars()
96   if ! exists("b:iainlist")
97     let b:iainlist = 0
98   endif
99   if ! exists("b:iainhex")
100     let b:iainhex = 0
101   endif
102   if ! exists("b:iainverbose")
103     let b:iainverbose = 0
104   endif
105 endfun
106
107 " Helper for status line.
108 " Show space, underscore or dollar sign depending on list mode.
109 fun! Show_List()
110   call Iain_Vars()
111   if b:iainlist == 0
112     " No list.
113     return " "
114   elseif b:iainlist == 1
115     " Just tabs.
116     return "_"
117   else
118     " Full list.
119     return "\$"
120   endif
121 endfun
122
123 " Helper for status line.
124 " Show c or C to denote case-sensitivity.
125 fun! Show_Case()
126   if &ic
127     return "c"
128   else
129     return "C"
130   endif
131 endfun
132
133 " Helper for status line.
134 " Show the size of the tabstop.
135 fun! Show_Tabstop()
136   return &ts
137 endfun
138
139 " Helper for status line.
140 " Show p when paste mode is on.
141 fun! Show_Paste()
142   if &paste
143     return "p"
144   else
145     return ""
146   endif
147 endfun
148
149 " Show the status line.
150 fun! Show_StatusLine()
151   call Iain_Vars()
152   let sl1='%2n\:\ %<%f\ [%{Show_List()}%{Show_Case()}%{Show_Tabstop()}%{Show_Paste()}%Y%M%R]\ %='
153   let sl3='L:%4.6l/%-4.6L\ C:%3.6c\ \|\ %P'
154   let hexformat='%b'
155   if b:iainhex
156     let hexformat='0\x%02B'
157   endif
158   if b:iainverbose
159     let sl2=hexformat . '\ \|\ P:%4.6o\ '
160   else
161     let sl2=''
162   endif
163   exec "set statusline=" . sl1 . sl2 . sl3
164 endfun
165
166 " Restore window size.
167 au VimLeave * if exists("oldcols") | let &columns=oldcols | endif
168
169 " Map C mode.
170 au BufEnter * if &ft == "c" || &ft == "cpp" | call CMode_map() | endif
171 au BufLeave * if &ft == "c" || &ft == "cpp" | call CMode_unmap() | endif
172
173 " Map Perl mode.
174 au BufEnter * if &ft == "perl" | call PerlMode_map() | endif
175 au BufLeave * if &ft == "perl" | call PerlMode_unmap() | endif
176
177 " Map Makefile mode.
178 au BufEnter * if &ft == "make" | call MakeMode_map() | endif
179 au BufLeave * if &ft == "make" | call MakeMode_unmap() | endif
180
181 " Entering C mode.
182 fun! CMode_map()
183   let oldcinkeys=&cinkeys
184   let oldcinwords=&cinwords
185   set cinkeys=0{,0},:,0#,!^F,o,O,e
186   set cinwords=if,else,while,do,for,switch
187 endfun
188
189 " Leaving C mode.
190 fun! CMode_unmap()
191   set cinkeys=oldcinkeys
192   set cinwords=oldcinwords
193 endfun
194
195 " Entering Perl mode.
196 fun! PerlMode_map()
197   let oldcinkeys=&cinkeys
198   let oldcinwords=&cinwords
199   set cinkeys=0{,0},:,!^F,o,O,e
200   set cinwords=if,else,while,do,for,eval
201 endfun
202
203 " Leaving Perl mode.
204 fun! PerlMode_unmap()
205   set cinkeys=oldcinkeys
206   set cinwords=oldcinwords
207 endfun
208
209 " Entering Make mode.
210 fun! MakeMode_map()
211   set list
212   set noexpandtab
213 endfun
214
215 " Leaving Make mode.
216 fun! MakeMode_unmap()
217   set nolist
218   set expandtab
219 endfun
220
221 " Show the status line for the first time.
222 call Show_StatusLine()
223 endif
224
225 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
226 " Handle options only available in Vim 6 and above.
227 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
228 if version >= "600"
229 version 6.0
230
231 " Track changing number mode.
232 let g:numbercols=&columns
233 let g:numberchanges=0
234
235 " Less intrusive syntax highlighting.
236 syn enable
237
238 " Nice GUI colour.
239 if has("gui_running")
240   se guifont=Bitstream\ Vera\ Sans\ Mono\ 12
241   colo darkblue
242 endif
243 if has("win32")
244   se guifont=Bitstream_Vera_Sans_Mono:h10:cANSI
245 endif
246
247 " Expand window when doing a vertical diff.
248 if &diff
249   let &columns = 164
250 endif
251
252 " Status bar matches the colour.
253 highlight StatusLine guifg=white guibg=blue ctermbg=white ctermfg=blue
254
255 " Numbers in blue.
256 highlight LineNr term=underline cterm=bold guifg=blue ctermfg=blue
257
258 " Make * and # work the way you expect in visual mode.
259 vnoremap * y/\V<C-R>=substitute(escape(@@,"/\\"),"\n","\\\\n","ge")<CR><CR>
260 vnoremap # y?\V<C-R>=substitute(escape(@@,"?\\"),"\n","\\\\n","ge")<CR><CR>
261
262 " Change list mode.
263 fun! Cycle_List()
264   let basic='tab:\\_,trail:_,extends:<,precedes:>'
265   call Iain_Vars()
266   let b:iainlist = b:iainlist + 1
267   if b:iainlist > 2
268     let b:iainlist = 0
269   endif
270   if b:iainlist == 0
271     set nolist
272   elseif b:iainlist == 1
273     exec "set lcs=" . basic
274     set list
275   else
276     exec "set lcs=" . basic . ",eol:$"
277     set list
278   endif
279 endfun
280
281 " Cycle between hex and decimal display of toolbar stuff.
282 fun! Cycle_HexStatusLine()
283   call Iain_Vars()
284   let b:iainhex = ! b:iainhex
285   call Show_StatusLine()
286 endfun
287
288 " Cycle verbose display of toolbar stuff.
289 fun! Cycle_VerboseStatusLine()
290   call Iain_Vars()
291   let b:iainverbose = ! b:iainverbose
292   call Show_StatusLine()
293 endfun
294
295 " Cycle between number mode.
296 " FIXME: Toggling in a split window doesn't work properly.  We need to track 
297 " the number of windows and number modes.  Something for later...
298 " Perhaps have a redraw callback that checks width and original column number.
299 fun! Cycle_Number()
300   if &number
301     " Restore width.
302     if &t_WS =~ '^\e.'
303       " Track changes.
304       let g:numberchanges=g:numberchanges-1
305       if g:numberchanges<0
306         g:numberchanges=0
307       endif
308
309       " Change size back if this was the last window.
310       if g:numberchanges == 0
311         let &columns=g:numbercols
312       endif
313     endif
314     set nonumber
315   else
316     " Save width between number toggling.
317     if &t_WS =~ '^\e'
318       " Expand if this was the first change.
319       if g:numberchanges == 0
320         let g:numbercols=&columns
321         if version >= 700
322           " Expand column by our preferred width.
323           let &columns=&columns+&numberwidth
324         else
325           " Vim 6 hardcodes width to 8.
326           let &columns=&columns+8
327         endif
328       endif
329
330       " Track changes.
331       let g:numberchanges=g:numberchanges+1
332     endif
333     set number
334   endif
335 endfun
336
337 " Toggle case-sensitivity.
338 fun! Invert_Case()
339   let &ic = ! &ic
340 endfun
341
342 " We'll use Q for various commands.  Unmap it.
343 map Q <Nop>
344
345 " Change to ts=2 with Q2.
346 map Q2 :se ts=2<CR>:<CR>
347 " Change to ts=4 with Q4.
348 map Q4 :se ts=4<CR>:<CR>
349 " Change to ts=8 with Q8.
350 map Q8 :se ts=8<CR>:<CR>
351 " Change to ts=16 with Q6.
352 map Q6 :se ts=16<CR>:<CR>
353 " Change to ts=32 with Q3.
354 map Q3 :se ts=32<CR>:<CR>
355 " Change foldmethod with Qf.
356 map Qf :se foldenable!<CR>:<CR>
357 " Toggle paste mode with Qp.
358 map Qp :se paste!<CR>:<CR>
359 " Swap hex/decimal statusline with Qx
360 map Qx :call Cycle_HexStatusLine()<CR>:<CR>
361 " Change statusline verbosity with Qv
362 map Qv :call Cycle_VerboseStatusLine()<CR>:<CR>
363 " Swap case-sensitivity with Qc.
364 map Qc :call Invert_Case()<CR>:<CR>
365 " Cycle list styles with Ql.
366 map Ql :call Cycle_List()<CR>:<CR>
367 " Change number mode with Qn.
368 map Qn :call Cycle_Number()<CR>:<CR>
369 " Toggle tags with Qt.
370 map Qt :Tlist<CR>
371 " Clear search pattern with Q/.
372 map Q/ :let @/=""<CR>:<CR>
373
374 " Leaving Perl mode.
375 fun! PerlMode_unmap()
376   set cinkeys=oldcinkeys
377   set cinwords=oldcinwords
378   set foldmethod=manual
379 endfun
380 endif
381
382 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
383 " Handle options only available in Vim 7 and above.
384 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
385 if version >= "700"
386 version 7.0
387
388 " Set size of numbers column.
389 se numberwidth=5
390
391 " Add "previous tab" mapping as gb.
392 map gb :tabPrev<CR>
393 endif