Vim 6 hardcodes numberwidth to 8.
[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 " Track changing number mode.
223 let g:numbercols=&columns
224 let g:numberchanges=0
225
226 " Less intrusive syntax highlighting.
227 syn enable
228
229 " Nice GUI colour.
230 if has("gui_running")
231   se guifont=Bitstream\ Vera\ Sans\ Mono\ 12
232   colo darkblue
233 endif
234 if has("win32")
235   se guifont=Bitstream_Vera_Sans_Mono:h10:cANSI
236 endif
237
238 " Expand window when doing a vertical diff.
239 if &diff
240   let &columns = 164
241 endif
242
243 " Status bar matches the colour.
244 highlight StatusLine guifg=white guibg=blue ctermbg=white ctermfg=blue
245
246 " Numbers in blue.
247 highlight LineNr term=underline cterm=bold guifg=blue ctermfg=blue
248
249 " Make * and # work the way you expect in visual mode.
250 vnoremap * y/\V<C-R>=substitute(escape(@@,"/\\"),"\n","\\\\n","ge")<CR><CR>
251 vnoremap # y?\V<C-R>=substitute(escape(@@,"?\\"),"\n","\\\\n","ge")<CR><CR>
252
253 " Change list mode.
254 fun! Cycle_List()
255   let basic='tab:\\_,trail:_,extends:<,precedes:>'
256   call Iain_Vars()
257   let b:iainlist = b:iainlist + 1
258   if b:iainlist > 2
259     let b:iainlist = 0
260   endif
261   if b:iainlist == 0
262     set nolist
263   elseif b:iainlist == 1
264     exec "set lcs=" . basic
265     set list
266   else
267     exec "set lcs=" . basic . ",eol:$"
268     set list
269   endif
270 endfun
271
272 " Cycle between hex and decimal display of toolbar stuff.
273 fun! Cycle_HexStatusLine()
274   call Iain_Vars()
275   let b:iainhex = ! b:iainhex
276   call Show_StatusLine()
277 endfun
278
279 " Cycle between number mode.
280 " FIXME: Toggling in a split window doesn't work properly.  We need to track 
281 " the number of windows and number modes.  Something for later...
282 " Perhaps have a redraw callback that checks width and original column number.
283 fun! Cycle_Number()
284   if &number
285     " Restore width.
286     if &t_WS =~ '^\e.'
287       " Track changes.
288       let g:numberchanges=g:numberchanges-1
289       if g:numberchanges<0
290         g:numberchanges=0
291       endif
292
293       " Change size back if this was the last window.
294       if g:numberchanges == 0
295         let &columns=g:numbercols
296       endif
297     endif
298     set nonumber
299   else
300     " Save width between number toggling.
301     if &t_WS =~ '^\e'
302       " Expand if this was the first change.
303       if g:numberchanges == 0
304         let g:numbercols=&columns
305         if version >= 700
306           " Expand column by our preferred width.
307           let &columns=&columns+&numberwidth
308         else
309           " Vim 6 hardcodes width to 8.
310           let &columns=&columns+8
311         endif
312       endif
313
314       " Track changes.
315       let g:numberchanges=g:numberchanges+1
316     endif
317     set number
318   endif
319 endfun
320
321 " Toggle case-sensitivity.
322 fun! Invert_Case()
323   let &ic = ! &ic
324 endfun
325
326 " We'll use Q for various commands.  Unmap it.
327 map Q <Nop>
328
329 " Change to ts=2 with Q2.
330 map Q2 :se ts=2<CR>:<CR>
331 " Change to ts=4 with Q4.
332 map Q4 :se ts=4<CR>:<CR>
333 " Change to ts=8 with Q8.
334 map Q8 :se ts=8<CR>:<CR>
335 " Change to ts=16 with Q6.
336 map Q6 :se ts=16<CR>:<CR>
337 " Change to ts=32 with Q3.
338 map Q3 :se ts=32<CR>:<CR>
339 " Change foldmethod with Qf.
340 map Qf :se foldenable!<CR>:<CR>
341 " Toggle paste mode with Qp.
342 map Qp :se paste!<CR>:<CR>
343 " Swap hex/decimal statusline with Qx
344 map Qx :call Cycle_HexStatusLine()<CR>:<CR>
345 " Swap case-sensitivity with Qc.
346 map Qc :call Invert_Case()<CR>:<CR>
347 " Cycle list styles with Ql.
348 map Ql :call Cycle_List()<CR>:<CR>
349 " Change number mode with Qn.
350 map Qn :call Cycle_Number()<CR>:<CR>
351 " Toggle tags with Qt.
352 map Qt :Tlist<CR>
353
354 " Leaving Perl mode.
355 fun! PerlMode_unmap()
356   set cinkeys=oldcinkeys
357   set cinwords=oldcinwords
358   set foldmethod=manual
359 endfun
360 endif
361
362 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
363 " Handle options only available in Vim 7 and above.
364 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
365 if version >= "700"
366 version 7.0
367
368 " Set size of numbers column.
369 se numberwidth=5
370
371 " Add "previous tab" mapping as gb.
372 map gb :tabPrev<CR>
373 endif