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