various fixes
[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 " au BufNewFile,BufRead /usr/src/linux*/** set patchmode=.orig
308 if v:version >= 701
309 au BufReadPre * set numberwidth=3
310 endif
311 "au BufEnter * syntax sync fromstart
312 "
313 "my attempt to have foldlevel=0 but still show the cursor. gives errors
314 "when opening help files, so avoid that.
315 au BufWinEnter * if &ft != 'help' | exe "normal zv" | endif
316
317 " for highlighting c preprocessor conditionals
318 au BufNewFile,BufRead syn match prePConditional "^\s*#\s*\(ifndef\|else\|endif\|ifdef\)"
319 au BufNewFile,BufRead hi link prePConditional WarningMSG
320 " alternate version which highlights only certain things inside the main match
321 "syn match prePSub contained "[^# ]*\>"
322 "syn match prePConditional contains=prePSub "^\s*#\s*\(ifndef\|else\|endif\|ifdef\)"
323 "hi link prePSub DiffText
324
325 " make shell scripts executable. todo: Make this into an ex command.
326 " au BufWritePost *.sh !/bin/bash -c 'if [ -x % ]; then exit; else /bin/chmod +x %; fi'
327
328 " When editing a file, always jump to the last cursor position
329 " this is in the reference manual.
330 autocmd BufReadPost *
331 \ if ! exists("g:leave_my_cursor_position_alone") |
332 \ if line("'\"") > 0 && line ("'\"") <= line("$") |
333 \ exe "normal g'\"" |
334 \ endif |
335 \ endif
336 endif
337
338 autocmd FuncUndefined * exe 'runtime autoload/' . expand('<afile>') . '.vim'
339 nnoremap <F6> :call SyntaxAttr()<CR>
340
341 " HEX EDITING FUNCTION: {{{1
342
343 if has("autocmd")
344 " vim -b : edit binary using xxd-format!
345
346 augroup Binary
347 au!
348 au BufReadPre *.bin,*.hex setlocal binary
349 au BufReadPost *
350 \ if &binary | exe "Hexmode" | endif
351 au BufWritePre *
352 \ if exists("b:editHex") && b:editHex && &binary |
353 \ exe "%!xxd -r" |
354 \ endif
355 au BufWritePost *
356 \ if exists("b:editHex") && b:editHex && &binary |
357 \ exe "%!xxd" |
358 \ exe "set nomod" |
359 \ endif
360 augroup END
361 endif
362
363 command Hexmode call ToggleHex()
364 function ToggleHex()
365 " hex mode should be considered a read-only operation
366 " save values for modified and read-only for restoration later,
367 " and clear the read-only flag for now
368 let l:modified=&mod
369 let l:oldreadonly=&readonly
370 let &readonly=0
371 if !exists("b:editHex") || !b:editHex
372 " save old options
373 let b:oldft=&ft
374 let b:oldbin=&bin
375 " set new options
376 setlocal binary " make sure it overrides any textwidth, etc.
377 let &ft="xxd"
378 " set status
379 let b:editHex=1
380 " switch to hex editor
381 %!xxd
382 else
383 " restore old options
384 let &ft=b:oldft
385 if !b:oldbin
386 setlocal nobinary
387 endif
388 " set status
389 let b:editHex=0
390 " return to normal editing
391 %!xxd -r
392 endif
393 " restore values for modified and read only state
394 let &mod=l:modified
395 let &readonly=l:oldreadonly
396 endfunction
397
398 " Copy matches function {{{1
399 " Copy matches of the last search to a register (default is the clipboard).
400 " Accepts a range (default is the current line).
401 " Matches are appended to the register and each match is terminated by \n.
402 " Usage: [RANGE]CopyMatches [REGISTER]
403 command! -nargs=0 -range -register CopyMatches call s:CopyMatches(<line1>, <line2>, "<reg>")
404 function! s:CopyMatches(line1, line2, reg)
405 let reg = a:reg != '' ? a:reg : '+'
406 for line in range(a:line1, a:line2)
407 let txt = getline(line)
408 let idx = match(txt, @/)
409 while idx > -1
410 exec "let @".reg." .= matchstr(txt, @/, idx) . \"\n\""
411 let end = matchend(txt, @/, idx)
412 let idx = match(txt, @/, end)
413 endwhile
414 endfor
415 endfunction
416
417 " Miscelany {{{1
418
419 " trailing whitespace warning
420 set statusline+=\ \ %{StatuslineTrailingSpaceWarning()}
421 "recalculate the trailing whitespace warning when idle, and after saving
422 autocmd cursorhold,bufwritepost * unlet! b:statusline_trailing_space_warning
423 "return '[\s]' if trailing white space is detected
424 "return '' otherwise
425 function! StatuslineTrailingSpaceWarning()
426 if !exists("b:statusline_trailing_space_warning")
427 if search('\s\+$', 'nw') != 0
428 let b:statusline_trailing_space_warning = '[\s]'
429 else
430 let b:statusline_trailing_space_warning = ''
431 endif
432 endif
433 return b:statusline_trailing_space_warning
434 endfunction