working snapshot
[distro-setup] / .vimrc
1 " Note, this config is many years old and needs serious updating.
2
3 " }}}
4 " vim:sw=4 ts=4 sts=4 foldmethod=marker
5 " MAPPINGS: {{{1
6 set encoding=utf-8 "the world is big
7 "
8 "
9 " set tab stops to 4
10 set sw=4
11 set ts=4
12 set sts=4
13 " mapping that should change: -, +, _
14 " j and k should go to first non-whitespace.
15 " |i_CTRL-Y|
16 "
17 "
18 " paste unnamed register
19 inoremap <C-e> <C-r>"
20
21 " when pasting, don't yank the pasted over text
22 vnoremap p "_dP
23
24 " save
25 inoremap <C-s> <C-o>:update<CR>
26
27
28 "swap " and '
29 noremap " '
30 noremap ' "
31
32 " Make ctrl-n and ctrl-p cycle through buffers in cmd mode
33 nnoremap <C-N> :bn<Enter>
34 nnoremap <C-P> :bp<Enter>
35 " quick command line window.
36 noremap m q:k
37 " mark
38 noremap <A-m> m
39 noremap , q:i
40 noremap Q ,
41 noremap q: Q
42 " yank to end of line
43 map Y y$
44 " Insert a single char
45 nmap <Space> i<Space><Esc>r
46
47 " Previously unmammed keys.
48
49 " yank document to clipboard
50 noremap - mzgg"+yG<C-o>'zzz
51 " save
52 noremap <C-s> :w<CR>
53 " current_file:line_number
54 nmap \x :let @"=expand("%:t") . ':' . line(".")<cr>
55 " split common files
56 noremap <A-q> :tabe ~/.vimrc<CR>
57 noremap <A-k> :tabe ~/t<CR>
58 " paste clipboard
59 noremap <A-p> "+p
60 noremap <A-P> "+P
61 " alt-shift-letter has some terminal incompatibilities, so
62 " this needs to find a new key:
63 inoremap <A-p> <C-r>+
64
65 " yank clipboard
66 noremap <A-y> "+y:let@*=@+<CR>
67 " easier window, tab and quickfix cycling
68 noremap <A-w> <C-w>w
69 noremap <A-n> :cn<CR>
70 noremap <A-N> :cp<CR>
71 " needs new key
72
73 "nohl
74 noremap <silent> <C-l> :nohl<CR><C-l>
75 inoremap <C-l> <C-O>:nohl<CR>
76
77 " make x and X not affect registers
78 noremap x "_x
79 noremap X "_X
80 "stop # from unindenting newlines with smartcase
81 inoremap # X\b#
82
83 "make * and # in visual mode search for the selection, instead of word under cursor.
84 vnoremap * :<C-u>call <SID>VSearch()<CR>/<CR>
85 vnoremap # :<C-u>call <SID>VSearch()<CR>?<CR>
86 function! s:VSearch()
87 let old = @"
88 norm! gvy
89 let @/ = '\V' . substitute(escape(@", '\'), '\n', '\\n', 'g')
90 let @" = old
91 endfunction
92
93 " remove trailing whitespace
94 nnoremap <F3> :%s/\s\+$//<CR>:unlet! b:statusline_trailing_space_warning<CR>
95
96 " easy vim.. use ctrl-o and ctrl-l to use normal mode.
97 noremap <F4> :call <SID>easyVim()<CR>
98 inoremap <F4> <C-o>:call <SID>easyVim()<CR>
99 function! s:easyVim()
100 if !&insertmode
101 set insertmode
102 "menu and button stuff.
103 set go+=m
104 set go+=T
105 " right scrollbar
106 set go+=r
107 " left scrollbar
108 set go+=L
109 else
110 set noinsertmode
111 set go-=m
112 set go-=T
113 set go-=r
114 set go-=L
115 endif
116 endfunction
117
118
119 " found this to compile the current buffer, took a bit from it
120 "function Make()
121 "let curr_dir = expand('%:h')
122 "if curr_dir == ''
123 "let curr_dir = '.'
124 "endif
125 "echo curr_dir
126 "execute 'lcd ' . curr_dir
127 "execute 'make %:r.o'
128 "execute 'lcd -'
129 "endfun
130 "imap <A-b> <Esc>:wa<CR>:call Make()<CR>
131 "nmap <A-b> :wa<CR>:call Make()<CR>
132
133 " standard make
134 " haven't been using it so its getting kicked out
135 "inoremap <A-d> <Esc>:wa<CR>:make<CR>
136 "nnoremap <A-d> :wa<CR>:make<CR>
137 inoremap <A-d> <Esc>gT
138 nnoremap <A-d> gT
139 inoremap <A-e> <Esc>gt
140 nnoremap <A-e> gt
141
142 " used to have !silent, but if make fails without an error that vim can parse,
143 " it doesn't show any failure, which is extremely confusing. I'd like
144 " something that would be silent except for a failure.
145 command! -bar -complete=file -nargs=* Make lcd %:h | make <args> | cw
146 nnoremap <A-b> :wa<CR>:Make<CR>
147 inoremap <A-b> <Esc>:wa<CR>:Make<CR>
148
149 nnoremap <A-c> :!./run.sh<CR>
150 nnoremap <A-t> :silent! !ctags -R .<CR>:redr!<CR>
151
152 " Execute the current line.
153 nnoremap <A-v> "pyy:@p<CR>
154 vnoremap <A-v> :<C-u>call ExecRegion('vis')<CR>
155 function! ExecRegion(type, ...)
156 if a:type == "vis"
157 " Yank and execute the visual region.
158 :'<,'> y p
159 @p
160 endif
161 endfun
162
163
164 " EX COMMANDS: {{{1
165
166
167 " originally I was using a macro for this
168 " do yy@0 on the next line
169 " ggzn/^{{ nyyddnkk::r !date +"\%I:\%M \%p \%A \%D" <<zm'0pA
170 " now I have hte following command:
171 command! J call JournalEntry()
172 function JournalEntry()
173 normal ggzn/^{{ nyyddnO\e:call setline(".", strftime("%I:%M %p %A %d/%m/%y")) <<ddkPzm'0pzvjO
174 startinsert!
175 endfun
176
177
178
179 " insert date in the format I like
180 noremap <A-s> :call setline(".", strftime("%I:%M %p %A %d/%m/%y"))<CR>
181 command! T :call setline(".", strftime("%I:%M %p %A %d/%m/%y"))<CR>
182 " need to lookup the cross platform date for this one
183 command! D :r !date +"\%D"<CR>
184
185 " for my super simple version control.
186 command! -nargs=* I w|!ii % <args>
187 command! -nargs=* O w|!io % <args>
188
189
190
191 " ABREVIATIONS: {{{1
192
193 ab J) Ïåñ
194
195
196 " SETTINGS: {{{1
197 "taken or modified from gentoo
198 set wildignore+=.info,.aux,.log,.dvi,.bbl,.out,.o,.lo "ignore some files for filename completion
199 " Terminal fixes
200 " todo: make this work like == screen*
201 if &term ==? "xterm" || &term ==? "screen" || &term == "xterm-color" || &term == "screen-bce"
202 set t_Sb=^[4%dm
203 set t_Sf=^[3%dm
204 set ttymouse=xterm2
205 set term=xterm-color
206 set t_Co=256 "terminal colors
207 endif
208 set virtualedit=block "Allow visual block to move in empty space.
209 set showbreak=\ "show a single character on wrapped lines, uses the non-text highlight.
210
211
212 set ttimeoutlen=10 " 10 milliseconds for esc timeout
213 set nocompatible
214 " end of gentoo stuff
215
216 " Allow split windows to shrink to this size
217 set winminheight=0
218
219 " show partial wrapped lines at the bottom
220 set display=lastline
221
222 " show menu for completion
223 set wildmenu
224
225 " was auto, but this caused new files created when writting which messes up
226 " my aufs
227 set bkc=yes
228
229 " we can see what mode we are in from the cursor.
230 set nomodeline
231
232 "
233 if v:version >= 701
234 set diffopt=filler,context:1,vertical,foldcolumn:1
235 endif
236
237 " grep will sometimes skip displaying the file name if you
238 " search in a singe file. Set grep to always generate a file-name.
239 set grepprg=grep\ -nH\ $*
240 set report=0 " Show a 'N lines were changed' report always
241 set shiftround " < and > will hit indent levels instead of +-4 always
242 " note: for some reason, windows gvim didn't like ~/tmp to be windows
243 " directory link to ../m/tmp. I used a different dir.
244 set backupdir=~/.editor-backups " keep backup files in one directory.
245 set mouse=a " Enable the mouse in all terminals if possible
246 set laststatus=2
247 set statusline=\|%n\|\ \ %f%M%R%h%W:%l\ \ \|%v»\ %P\|%<\ \ %{getcwd()}
248 set modeline " use modelines when root. security be damned.
249 set listchars=tab:._ "show tabs.
250 set foldignore= " don't ignore # when using foldmethod=indent
251
252 filetype plugin on
253 filetype indent on
254 set autoindent
255 syntax enable
256 set hlsearch
257 "Tab settings, for reference.
258 "set shiftwidth=2
259 "set noexpandtab
260 "set softtabstop=2
261 "set tabstop=2
262 "End tab settings.
263 set number
264 "viminfo settings
265 " the viminfo file deletes symlinks, so i moved it to tmp/
266 set vi='100,<1000,s10,h,n~/.viminfo
267 set foldlevel=99
268 set ic "set ignorecase
269 set scs "set smartcase
270 " allow backspacing over everything in insert mode
271 set backspace=indent,eol,start
272 set backup " keep a backup file
273 set history=1000 " keep 1000 lines of command line history
274 set showcmd " display incomplete commands
275 set incsearch " do incremental searching
276 colorscheme nightsky
277 set foldmethod=indent
278
279 " Just need to define it. Picked single char 'y' cuz its the smallest
280 let c_no_comment_fold = 1
281 let c_space_errors = 1
282 "for folding C preprocessor if, from my syntax addition.
283 "the problem is that sometime
284 " let cpre
285
286 "for easy x11 cutting and pasting, but it gets in the way.
287 "note: exclude must be the last option.
288 "set clipboard=autoselect,unnamed,exclude:cons\|linux
289
290
291 " for syntax highlighting
292 let is_bash = 1
293
294
295
296 if $OS ==# 'Windows_NT'
297 set directory=p:\tmp,.
298 endif
299
300 " AUTOCOMMANDS: {{{1
301
302 if !exists("autocommands_loaded")
303 let autocommands_loaded = 1
304
305
306
307
308 " au BufNewFile,BufRead /usr/src/linux*/** set patchmode=.orig
309 if v:version >= 701
310 au BufReadPre * set numberwidth=3
311 endif
312 "au BufEnter * syntax sync fromstart
313 "
314 "my attempt to have foldlevel=0 but still show the cursor. gives errors
315 "when opening help files, so avoid that.
316 au BufWinEnter * if &ft != 'help' | exe "normal zv" | endif
317
318 " for highlighting c preprocessor conditionals
319 au BufNewFile,BufRead syn match prePConditional "^\s*#\s*\(ifndef\|else\|endif\|ifdef\)"
320 au BufNewFile,BufRead hi link prePConditional WarningMSG
321 " alternate version which highlights only certain things inside the main match
322 "syn match prePSub contained "[^# ]*\>"
323 "syn match prePConditional contains=prePSub "^\s*#\s*\(ifndef\|else\|endif\|ifdef\)"
324 "hi link prePSub DiffText
325
326 " make shell scripts executable. todo: Make this into an ex command.
327 " au BufWritePost *.sh !/bin/bash -c 'if [ -x % ]; then exit; else /bin/chmod +x %; fi'
328
329 " When editing a file, always jump to the last cursor position
330 " this is in the reference manual.
331 autocmd BufReadPost *
332 \ if ! exists("g:leave_my_cursor_position_alone") |
333 \ if line("'\"") > 0 && line ("'\"") <= line("$") |
334 \ exe "normal g'\"" |
335 \ endif |
336 \ endif
337 endif
338
339 autocmd FuncUndefined * exe 'runtime autoload/' . expand('<afile>') . '.vim'
340 nnoremap <F6> :call SyntaxAttr()<CR>
341
342 " HEX EDITING FUNCTION: {{{1
343
344 if has("autocmd")
345 " vim -b : edit binary using xxd-format!
346
347 augroup Binary
348 au!
349 au BufReadPre *.bin,*.hex setlocal binary
350 au BufReadPost *
351 \ if &binary | exe "Hexmode" | endif
352 au BufWritePre *
353 \ if exists("b:editHex") && b:editHex && &binary |
354 \ exe "%!xxd -r" |
355 \ endif
356 au BufWritePost *
357 \ if exists("b:editHex") && b:editHex && &binary |
358 \ exe "%!xxd" |
359 \ exe "set nomod" |
360 \ endif
361 augroup END
362 endif
363
364 command Hexmode call ToggleHex()
365 function ToggleHex()
366 " hex mode should be considered a read-only operation
367 " save values for modified and read-only for restoration later,
368 " and clear the read-only flag for now
369 let l:modified=&mod
370 let l:oldreadonly=&readonly
371 let &readonly=0
372 if !exists("b:editHex") || !b:editHex
373 " save old options
374 let b:oldft=&ft
375 let b:oldbin=&bin
376 " set new options
377 setlocal binary " make sure it overrides any textwidth, etc.
378 let &ft="xxd"
379 " set status
380 let b:editHex=1
381 " switch to hex editor
382 %!xxd
383 else
384 " restore old options
385 let &ft=b:oldft
386 if !b:oldbin
387 setlocal nobinary
388 endif
389 " set status
390 let b:editHex=0
391 " return to normal editing
392 %!xxd -r
393 endif
394 " restore values for modified and read only state
395 let &mod=l:modified
396 let &readonly=l:oldreadonly
397 endfunction
398
399 " Copy matches function {{{1
400 " Copy matches of the last search to a register (default is the clipboard).
401 " Accepts a range (default is the current line).
402 " Matches are appended to the register and each match is terminated by \n.
403 " Usage: [RANGE]CopyMatches [REGISTER]
404 command! -nargs=0 -range -register CopyMatches call s:CopyMatches(<line1>, <line2>, "<reg>")
405 function! s:CopyMatches(line1, line2, reg)
406 let reg = a:reg != '' ? a:reg : '+'
407 for line in range(a:line1, a:line2)
408 let txt = getline(line)
409 let idx = match(txt, @/)
410 while idx > -1
411 exec "let @".reg." .= matchstr(txt, @/, idx) . \"\n\""
412 let end = matchend(txt, @/, idx)
413 let idx = match(txt, @/, end)
414 endwhile
415 endfor
416 endfunction
417
418 " Miscelany {{{1
419
420 " trailing whitespace warning
421 set statusline+=\ \ %{StatuslineTrailingSpaceWarning()}
422 "recalculate the trailing whitespace warning when idle, and after saving
423 autocmd cursorhold,bufwritepost * unlet! b:statusline_trailing_space_warning
424 "return '[\s]' if trailing white space is detected
425 "return '' otherwise
426 function! StatuslineTrailingSpaceWarning()
427 if !exists("b:statusline_trailing_space_warning")
428 if search('\s\+$', 'nw') != 0
429 let b:statusline_trailing_space_warning = '[\s]'
430 else
431 let b:statusline_trailing_space_warning = ''
432 endif
433 endif
434 return b:statusline_trailing_space_warning
435 endfunction