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