Cruft.
[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 " Vim 5 hardcodes the size of numbers column to 8.
72 let numberwidth=8
73
74 " Save sessions in UNIX format with / as file separator.  This is
75 " cross-platoform.
76 se ssop+=unix,slash
77
78 " Nuke any pre-existing autocommands.
79 autocmd!
80
81 " Save the current window width so we can restore it when we quit.
82 let oldcols=&columns
83
84 " More GUI options.  Add icon, tearoffs and toolbar.
85 se go+=itT
86
87 " Allow dynamic window resize even if we aren't in an xterm.
88 se t_WS=\e[8;%p1%d;%p2%dt
89
90 " Highlight search results.
91 se hlsearch
92
93 " Syntax highlighting.
94 syn on
95
96 " Catch typos.
97 command! W :w
98 command! Wq :wq
99 command! Wqa :wqa
100
101 " Set up our variables.
102 fun! Iain_Vars()
103   if ! exists("b:iainlist")
104     let b:iainlist = 0
105   endif
106   if ! exists("b:iainhex")
107     let b:iainhex = 0
108   endif
109   if ! exists("b:iainverbose")
110     let b:iainverbose = 0
111   endif
112 endfun
113
114 " Helper for status line.
115 " Show space, underscore or dollar sign depending on list mode.
116 fun! Show_List()
117   call Iain_Vars()
118   if b:iainlist == 0
119     " No list.
120     return " "
121   elseif b:iainlist == 1
122     " Just tabs.
123     return "_"
124   else
125     " Full list.
126     return "\$"
127   endif
128 endfun
129
130 " Helper for status line.
131 " Show c or C to denote case-sensitivity.
132 fun! Show_Case()
133   if &ic
134     return "c"
135   else
136     return "C"
137   endif
138 endfun
139
140 " Helper for status line.
141 " Show the size of the tabstop.
142 fun! Show_Tabstop()
143   return &ts
144 endfun
145
146 " Helper for status line.
147 " Show p when paste mode is on.
148 fun! Show_Paste()
149   if &paste
150     return "p"
151   else
152     return ""
153   endif
154 endfun
155
156 " Show the status line.
157 fun! Show_StatusLine()
158   call Iain_Vars()
159   let sl1='%2n\:\ %<%f\ [%{Show_List()}%{Show_Case()}%{Show_Tabstop()}%{Show_Paste()}%Y%M%R]\ %='
160   let sl3='L:%4.6l/%-4.6L\ C:%3.6c\ \|\ %P'
161   let hexformat='%b'
162   if b:iainhex
163     let hexformat='0\x%02B'
164   endif
165   if b:iainverbose
166     let sl2=hexformat . '\ \|\ P:%4.6o\ '
167   else
168     let sl2=''
169   endif
170   exec "set statusline=" . sl1 . sl2 . sl3
171 endfun
172
173 " Toggle case-sensitivity.
174 fun! Invert_Case()
175   let &ic = ! &ic
176 endfun
177
178 " Restore window size.
179 au VimLeave * if exists("oldcols") | let &columns=oldcols | endif
180
181 " Map Makefile mode.
182 au BufEnter * if &ft == "make" | call MakeMode_map() | endif
183 au BufLeave * if &ft == "make" | call MakeMode_unmap() | endif
184
185 " Entering Make mode.
186 fun! MakeMode_map()
187   set list
188   set noexpandtab
189 endfun
190
191 " Leaving Make mode.
192 fun! MakeMode_unmap()
193   set nolist
194   set expandtab
195 endfun
196
197 " Show the status line for the first time.
198 call Show_StatusLine()
199
200 " Change to ts=2 with Q2.
201 map Q2 :se ts=2<CR>:<CR>
202 " Change to ts=4 with Q4.
203 map Q4 :se ts=4<CR>:<CR>
204 " Change to ts=8 with Q8.
205 map Q8 :se ts=8<CR>:<CR>
206 " Change to ts=16 with Q6.
207 map Q6 :se ts=16<CR>:<CR>
208 " Change to ts=32 with Q3.
209 map Q3 :se ts=32<CR>:<CR>
210 " Toggle paste mode with Qp.
211 map Qp :se paste!<CR>:<CR>
212 " Swap case-sensitivity with Qc.
213 map Qc :call Invert_Case()<CR>:<CR>
214 " Change number mode with Qn.
215 map Qn :se number!<CR>:<CR>
216 " Expand or shrink window size with Q> and Q<.  For use after toggling number.
217 map Q> :exe 'se columns+=' . numberwidth<CR>:<CR>
218 map Q< :exe 'se columns-=' . numberwidth<CR>:<CR>
219 " Clear search pattern with Q/.
220 map Q/ :let @/=""<CR>:<CR>
221
222 endif
223
224 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
225 " Handle options only available in Vim 6 and above.
226 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
227 if version >= "600"
228 version 6.0
229
230 " Remember quickfix state.
231 let g:quickfixing=0
232
233 " Set indenting by filetype.
234 filetype indent on
235
236 " Less intrusive syntax highlighting.
237 syn enable
238
239 " Nice GUI colour.
240 if has("gui_running")
241   se guifont=Bitstream\ Vera\ Sans\ Mono\ 10
242   colo darkblue
243 endif
244 if has("win32")
245   se guifont=Bitstream_Vera_Sans_Mono:h10:cANSI
246 endif
247
248 " Ignore whitespace when diffing.
249 se diffopt=filler,iwhite
250
251 " Expand window when doing a vertical diff.
252 if &diff
253   let &columns = 164
254 endif
255
256 " Status bar matches the colour.
257 highlight StatusLine guifg=white guibg=blue ctermbg=white ctermfg=blue
258
259 " Numbers in blue.
260 highlight LineNr term=underline cterm=bold guifg=blue ctermfg=blue
261
262 " Remember that we are opening the quickfix window.
263 au BufWinEnter quickfix let g:quickfixing=1
264 au BufUnload * if &ft == "qf" | let g:quickfixing=0 | endif
265
266 " Make * and # work the way you expect in visual mode.
267 vnoremap * y/\V<C-R>=substitute(escape(@@,"/\\"),"\n","\\\\n","ge")<CR><CR>
268 vnoremap # y?\V<C-R>=substitute(escape(@@,"?\\"),"\n","\\\\n","ge")<CR><CR>
269
270 " Change list mode.
271 fun! Cycle_List()
272   let basic='tab:\\_,trail:_,extends:<,precedes:>'
273   call Iain_Vars()
274   let b:iainlist = b:iainlist + 1
275   if b:iainlist > 2
276     let b:iainlist = 0
277   endif
278   if b:iainlist == 0
279     set nolist
280   elseif b:iainlist == 1
281     exec "set lcs=" . basic
282     set list
283   else
284     exec "set lcs=" . basic . ",eol:$"
285     set list
286   endif
287 endfun
288
289 " Cycle between hex and decimal display of toolbar stuff.
290 fun! Cycle_HexStatusLine()
291   call Iain_Vars()
292   let b:iainhex = ! b:iainhex
293   call Show_StatusLine()
294 endfun
295
296 " Cycle verbose display of toolbar stuff.
297 fun! Cycle_VerboseStatusLine()
298   call Iain_Vars()
299   let b:iainverbose = ! b:iainverbose
300   call Show_StatusLine()
301 endfun
302
303 " Toggle quickfix window.
304 fun! Cycle_Quickfix()
305   if g:quickfixing == 1
306     cclose
307     let g:quickfixing=0
308   else
309     copen
310   endif
311 endfun
312
313 " We use Q for various commands.  Unmap it.
314 " Vim 5 won't let us unmap this as it treats Q as an ambiguous mapping (because 
315 " Qx also exists.  With Vim 5 you are rewarded with Ex mode if you don't type 
316 " the Qx sequence quickly enough.  Vim 6 allows us to forget the Ex mapping.
317 map Q <Nop>
318
319 " Swap hex/decimal statusline with Qx
320 map Qx :call Cycle_HexStatusLine()<CR>:<CR>
321 " Change statusline verbosity with Qv
322 map Qv :call Cycle_VerboseStatusLine()<CR>:<CR>
323 " Cycle list styles with Ql.
324 map Ql :call Cycle_List()<CR>:<CR>
325 " Toggle tags with Qt.
326 map Qt :Tlist<CR>
327 " Change foldmethod with Qf.
328 map Qf :se foldenable!<CR>:<CR>
329 " Toggle quickfix window with Qq.
330 map Qq :call Cycle_Quickfix()<CR>:<CR>
331
332 endif
333
334 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
335 " Handle options only available in Vim 7 and above.
336 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
337 if version >= "700"
338 version 7.0
339
340 " Make diffs vertical by default.
341 se diffopt+=vertical
342
343 " Set size of numbers column.
344 se numberwidth=5
345
346 " Add "previous tab" mapping as gb.
347 map gb :tabPrev<CR>
348 endif