]> git.phdru.name Git - dotfiles.git/blob - .vimrc
.vimrc: keep undo history across sessions by storing it in a file
[dotfiles.git] / .vimrc
1 " User configuration file for Vi IMproved.
2 "
3 " vim 6.0+ required, 7.0+ recommended.
4
5 " 1 important
6
7 " Remove ALL autocommands in case the file is sourced for the second time
8 autocmd!
9
10 language messages C " Print messages in English
11
12 if exists("b:wrap") " Only do this on the second and subsequent :source's
13    let fenc = &fileencoding
14    let ftype = &filetype
15    let mod = &modified
16    set all& " Reset all options, except terminal options, to their default value.
17    execute 'set fileencoding=' . fenc
18    execute 'set filetype=' . ftype
19    if mod
20       set modified
21    else
22       set nomodified
23    endif
24    unlet fenc ftype mod
25 endif
26
27 " No, it is not VI, it is VIM! It is important to set this first, because this
28 " command resets many other options.
29 set nocompatible
30
31 " Set behavior to xterm, not mswin
32 behave xterm
33
34 " 2 moving around, searching and patterns
35
36 set ignorecase    " Ignore case in search patterns
37 set smartcase     " Match 'word' case-insensitive and 'Word' case-sensitive
38
39 set nostartofline " Keep cursor's column
40 set whichwrap=b,s,h,l,<,>,[,],~ " Wrap to the previous/next line on all keys and ~ command
41
42 " 4 displaying text
43
44 set display=lastline,uhex " Show the last line instead of '@'; show non-printable chars as <hex>
45 set lazyredraw    " Do not update screen while executing macros
46 set list          " listchars only works with 'list'
47 set listchars=tab:>_,trail:_,extends:+ " Show tabs, trailing spaces, long lines
48 set wrap          " Visually wrap long lines
49
50 " With 'set wrap' wrap long lines at a character in 'breakat'
51 " Please note 'nolist' is required to use 'linebreak'
52 set linebreak showbreak=+\ " A plus and a space
53
54 set sidescroll=1  " The minimal number of columns to scroll horizontally
55
56 " 5 highlighting
57
58 colorscheme phd
59 set nohlsearch    " Stop the search highlighting
60
61 " 6 multiple windows
62
63 set hidden        " Don't unload a buffer when no longer shown in a window; allow to switch between buffers/windows when the buffer is modified
64 set laststatus=2  " Always show status line
65
66 set splitbelow    " A new window is put below of the current one
67 set splitright    " A new window is put right of the current one
68
69 " 8 terminal
70
71 set ttyfast       " terminal connection is fast
72
73 set title         " Set title to the value of 'titlestring' or to 'filename - VIM'
74 set titleold=     " string to restore the title to when exiting Vim
75 " String to use for the Vim window title; with statusline printf items:
76 " display filename, modification flag, full path, argument list status,
77 " the current user, host and program name (to distinguish vim/view/etc).
78 set titlestring=%t%(\ %M%)%(\ (%{expand(\"%:p:h\")})%)%(\ %a%)\ -\ [%{$USER}@%{hostname()}]\ %{v:progname}
79
80 " 9 using the mouse
81
82 set mouse=ar      " Use mouse in all modes, plus hit-return
83
84 " 12 messages and info
85
86 set ruler         " Show cursor position below each window
87 set showcmd       " Show (partial) command keys in the status line
88
89 " Short message for [Modified];
90 " overwrite message for writing a file with subsequent message;
91 " truncate long file messages
92 set shortmess=mot
93
94 " 14 editing text
95
96 set backspace=indent,eol,start
97 set complete+=k   " Scan spell dictionaries for completion in addition to standard places
98 set infercase     " adjust case of a keyword completion match
99 set nojoinspaces  " Do not insert two spaces after a '.', '?' and '!' with a join command
100 set nrformats=hex " I seldom edit octal numbers, but very often dates like 2001-02-01
101
102 " Default value 'croql' is a compromise for both natural and programming
103 " languages, but as any compromise it works good for neither natural nor
104 " programming languages. This value is good for natural texts,
105 " let ftplugins to set values suitable for programming languages.
106 set formatoptions=2t " list of flags that tell how automatic formatting works
107
108 " 15 tabs and indenting
109
110 set expandtab     " expand <Tab> to spaces in Insert mode
111 set shiftwidth=3  " number of spaces used for each step of (auto)indent
112 set smarttab      " a <Tab> in an indent inserts 'shiftwidth' spaces
113 set softtabstop=3 " number of spaces to insert for a <Tab>
114
115 set noautoindent  " Do not automatically set the indent of a new line
116
117 " 18 mapping
118
119 set timeout timeoutlen=3000 ttimeoutlen=100 " allow timing out up to 3 seconds halfway into a mapping; 100 ms to wait for a key code or mapped key sequence to complete
120
121 " 19 reading and writing files
122 if v:version >= 703
123    set cryptmethod=blowfish " encryption method for file writing: zip or blowfish
124 endif
125
126 " 20 the swap file
127
128 " list of directories for the swap file; remove . (the current directory)
129 if has("win32")
130    set directory=$TEMP//,c:/tmp//,c:/temp//
131 else
132    set directory=~/tmp/vim//,~/tmp//,/var/tmp//,/tmp//
133 endif
134 " if a directory ends in two path separators "//"
135 " or "\\", the swap file name will be built from the complete path to
136 " the file with all path separators substituted to percent '%' signs.
137 " This will ensure file name uniqueness in the preserve directory.
138
139 " 21 command line editing
140
141 set history=1000  " how many command lines are remembered
142 set suffixes+=.pyc,.pyo " list of file name extensions that have a lower priority
143 set wildignore+=*.py[co] " Ignore these patterns when completing file names
144 set wildmenu      " command-line completion shows a list of matches
145 set wildmode=longest,list:longest,full " Bash-vim completion behavior
146
147 " Keep undo history across sessions by storing it in a file
148 if has('persistent_undo')
149     let myUndoDir = expand('$HOME/tmp/vim/undo')
150     " Create dirs
151     call system('mkdir -p ' . myUndoDir)
152     let &undodir = myUndoDir
153     set undofile
154 endif
155
156 " 22 executing external commands
157
158 if has("filterpipe")
159    set noshelltemp " Use pipes on Unix
160 endif
161
162 " 25 multi-byte characters
163
164 " Automatically detected character encodings
165 set fileencodings=ucs-bom,us-ascii,utf-8,koi8-r,cp1251,cp866,latin1
166
167 " 26 various
168
169 " ! - when included, save and restore global variables that start
170 "     with an uppercase letter, and don't contain a lowercase letter;
171 " h - disable the effect of 'hlsearch';
172 " ' - number of files for which the marks are remembered;
173 " " and < - maximum number of lines saved for a register;
174 " s - maximum size of an item in Kbytes.
175 if version < 603
176    set viminfo=!,h,'50,\"1000
177 else
178    set viminfo=!,h,'50,<1000,s10
179 endif
180
181 " c - convert viminfo to the current encoding;
182 if has("iconv")
183    set viminfo^=c
184 endif
185
186 " Removable media paths
187 if has("win32")
188    set viminfo+=ra:,rb:
189 endif
190
191 " ----------
192
193 if has("gui_running")
194    set background=light
195
196    if has("win32")
197       "set guifont=Courier_New:h18:cRUSSIAN
198       set guifont=Lucida_Console:h18:cRUSSIAN
199    else
200       set guifont=Monospace\ 16
201       set toolbar=icons,text " how to show the toolbar
202    endif
203
204    set guicursor+=a:blinkon0 " Stop cursor blinking
205
206    " Make shift-insert work like in Xterm
207    " map <S-Insert> <MiddleMouse>
208    " map! <S-Insert> <MiddleMouse>
209
210    " ----------
211    " From http://slobin.pp.ru/vim/_vimrc.html
212
213    " Arrows should go into wrapped lines, but not while popup menu is visible
214    imap <expr> <Down> pumvisible() ? "<Down>" : "<C-O>gj"
215    imap <expr> <Up> pumvisible() ? "<Up>" : "<C-O>gk"
216
217    " The <CR> key should select from completion menu without adding a newline
218    imap <expr> <CR> pumvisible() ? "<C-Y>" : "<CR>"
219    " ----------
220
221 else
222    if (&term =~ "linux") || ($BACKGROUND == 'DARK') || ($BACKGROUND == 'dark')
223          \ || has("win32")
224       " Background of the terminal is black or dark grey
225       set background=dark
226    else
227       set background=light
228    endif
229
230    if (&term =~ "linux")
231       execute 'set t_kb=' . nr2char(127)
232    endif
233
234    if (&term =~ "rxvt") || (&term =~ "screen") || (&term =~ "term") || (&term =~ "vt100")
235       execute 'set t_kb=' . nr2char(127)
236
237       " 'autoselect' to always put selected text on the clipboard;
238       " 'unnamed' to use the * register like unnamed register '*'
239       " for all yank, delete and put operations;
240       " This allows to use mouse for copy/paste in local xterm,
241       " but prevents to save the unnamed register between sessions.
242       " set clipboard=autoselect,unnamed,exclude:cons\|linux
243
244       if has ("terminfo")
245          " set t_Co=256
246          set t_Co=16
247       else
248          set t_Co=8
249          set t_Sf="\e[3%dm"
250          set t_Sb="\e[4%dm"
251       endif
252    endif
253
254    if (&term =~ "screen")
255       set ttymouse=xterm2 " Enable mouse codes under screen/tmux
256       if empty(&t_ts)
257          " Enable window title under screen/tmux
258          let &t_ts = "\e]2;"
259          let &t_fs = "\007"
260       endif
261    endif
262 endif
263
264 " Multiline comments often confuse vim syntax highlighting - these maps
265 " allow to resynchronize; the first is faster, the second is more thorough
266 nmap \sc :syntax sync clear<Enter>
267 nmap \ss :syntax sync fromstart<Enter>
268
269
270 " AUTOCOMMANDS
271
272 " Enable filetype detection
273 filetype plugin indent on
274
275 runtime macros/matchit.vim
276
277 " Reread me after editing
278 autocmd BufWritePost ~/.vimrc source ~/.vimrc | syntax on
279
280 if version >= 700
281 " Save all files before running any quickfix command (grep, makeprg, etc.)
282 autocmd QuickFixCmdPre * wall
283 " automatically close quickfix if it's the only window left
284 autocmd WinEnter * if winnr('$') == 1 && &buftype == "quickfix" | quit | endif
285 endif
286
287 " Syntax highlighting
288 autocmd BufReadPost * syntax on
289
290
291 " Restore last known cursor position
292 function! RestorePosition()
293    if exists('b:position_restored')
294       return
295    endif
296
297    if line("'\"") > 0
298       call cursor(line("'\""), col("'\""))
299    endif
300    let b:position_restored = 1
301 endfunction
302
303 " When editing a file, always jump to the last cursor position (if saved)
304 autocmd BufReadPost * call RestorePosition()
305
306
307 function! SetupEncoding(encoding)
308    if !has("iconv") || exists('b:encoding_set') || strlen(a:encoding) == 0
309       return
310    endif
311
312    call RestorePosition()
313    let b:encoding_set = 1
314    execute "edit ++enc=" . a:encoding
315 endfunction
316
317
318 " http://lwn.net/Articles/226514/
319
320 augroup gpg
321 " Remove ALL autocommands for the current group.
322 autocmd!
323 autocmd BufReadPre,FileReadPre *.gpg set viminfo=
324 autocmd BufReadPre,FileReadPre *.gpg setlocal noswapfile
325 autocmd BufReadPost *.gpg :%!gpg -q -d
326 autocmd BufReadPost *.gpg | redraw
327 autocmd BufWritePre *.gpg :%!gpg --default-recipient-self -q -e -a
328 autocmd BufWritePost *.gpg u
329 autocmd VimLeave *.gpg :!clear
330 " For OpenSSL:
331 " BufReadPost: use "openssl bf -d -a"
332 " BufWritePre: use "openssl bf -salt -a"
333 augroup END
334
335
336 function! SetWrap()
337    setlocal wrap
338    map <Up> gk
339    imap <Up> <C-O>gk
340    map <Down> gj
341    imap <Down> <C-O>gj
342    let b:wrap = 1
343 endfunction
344
345 function! SetNoWrap()
346    setlocal nowrap
347    map <Up> k
348    map <Down> j
349    let b:wrap = 0
350 endfunction
351
352 command! SetWrap call SetWrap()
353 command! SetNoWrap call SetNoWrap()
354
355 " b:wrap can be set by an ftplugin
356 autocmd BufReadPost * if !exists("b:wrap") | call SetWrap() | endif
357
358
359 " MAPPINGS
360
361 " Do not unindent #-comment
362 inoremap # X<C-H>#
363
364 " map <C-A> <Home>
365 " map <C-E> <End>
366 " map <C-Z> :shell<CR>
367
368
369 if version >= 700
370 " WEB BROWSERS
371
372 let s:URL_re="\\v(((https?|ftp|gopher|telnet)://|(mailto|file|news|about|ed2k|irc|sip|magnet):)[^' \t<>\"]+|(www|web|w3)[a-z0-9_-]*\\.[a-z0-9._-]+\\.[^' \t<>\"]+)[a-z0-9/]\\c"
373
374 function! ExtractURL()
375    let line = getline('.')
376    let parts = split(line, s:URL_re . '\zs')
377
378    if len(parts) == 0
379       throw 'ExtractURLCannotFindURL' " No URL found
380    endif
381
382    let length = 0
383    let column = col('.')
384
385    for p in parts
386       let save_length = length
387       let length += len(p)
388       if length >= column
389          break
390       endif
391    endfor
392
393    let pos = match(p, s:URL_re)
394    if pos == -1
395       throw 'ExtractURLCannotFindURL' " No URL found
396    endif
397
398    if save_length + pos >= column " cursor left of the URL
399       throw 'ExtractURLCannotFindURL'
400    endif
401
402    return strpart(p, pos)
403 endfunction
404
405 function! EscapeURL(url)
406    let url = substitute(a:url, ' ', '%20', 'g')
407    return escape(url, '!#$%')
408 endfunction
409
410 function! OpenURL(url, newwin)
411    execute "!webbrowser " . a:newwin . " '" . EscapeURL(a:url) . "'"
412 endfunction
413
414 function! ExtractOpenURL(newwin)
415    try
416       let url = ExtractURL()
417    catch /^ExtractURLCannotFindURL$/
418       echoerr 'No URL found under cursor'
419       return
420    endtry
421
422    call OpenURL(url, a:newwin)
423 endfunction
424
425 function! EncodeOpenURL(url, newwin)
426    execute "!webbrowser-encode-url " . a:newwin . " '" . EscapeURL(a:url) . "'"
427 endfunction
428
429
430 " Send current link to a browser
431 nmap \b :call ExtractOpenURL('')<CR>
432 nmap \w :call ExtractOpenURL('-n')<CR>
433 nmap \t :call ExtractOpenURL('-t')<CR>
434 " Send visual block to a browser
435 vmap \b ""y:call OpenURL('<C-R>"', '')<CR>
436 vmap \w ""y:call OpenURL('<C-R>"', '-n')<CR>
437 vmap \t ""y:call OpenURL('<C-R>"', '-t')<CR>
438 " Encode and send visual block to a browser
439 vmap \B ""y:call EncodeOpenURL('<C-R>"', '')<CR>
440 vmap \W ""y:call EncodeOpenURL('<C-R>"', '-n')<CR>
441 vmap \T ""y:call EncodeOpenURL('<C-R>"', '-t')<CR>
442 " Send current file's name to a browser
443 nmap \B :call EncodeOpenURL('file:' . expand("%:p"), '')<CR>
444 nmap \W :call EncodeOpenURL('file:' . expand("%:p"), '-n')<CR>
445 nmap \T :call EncodeOpenURL('file:' . expand("%:p"), '-t')<CR>
446
447 endif " version >= 700
448
449
450 " SPELLING
451
452 if has("spell") " Works in 7.0+
453    " Use builtin spellchecker
454    nmap \sv :syntax off <BAR> setlocal spell<CR>
455    " Clear
456    nmap \sn :syntax on <BAR> setlocal nospell<CR>
457 endif
458
459
460 " Russian letters mapped in normal mode
461 "if has("keymap")
462 "   set keymap=russian-jcuken
463 "endif
464
465 if has("langmap")
466    if v:version >= 702
467       " langmap in utf-8 mode requires at least Vim 7.2.109
468       scriptencoding utf-8
469       set langmap=ё`Ё~йqцwуeкrеtнyгuшiщoзpх[ъ]фaыsвdаfпgрhоjлkдlж\\;э'
470                  \яzчxсcмvиbтnьmб\\,ю.ЙQЦWУEКRЕTНYГUШIЩOЗPХ{Ъ}
471                  \ФAЫSВDАFПGРHОJЛKДLЖ:Э\\"ЯZЧXСCМVИBТNЬMБ<Ю>
472
473    elseif &encoding != 'utf-8'
474       scriptencoding koi8-r
475       set langmap=ё`Ё~йqцwуeкrеtнyгuшiщoзpх[ъ]фaыsвdаfпgрhоjлkдlж\\;э'
476                  \яzчxсcмvиbтnьmб\\,ю.ЙQЦWУEКRЕTНYГUШIЩOЗPХ{Ъ}
477                  \ФAЫSВDАFПGРHОJЛKДLЖ:Э\\"ЯZЧXСCМVИBТNЬMБ<Ю>
478    endif
479
480    scriptencoding us-ascii
481 endif
482
483
484 if version >= 700
485 function! W()
486    " let encodings=filter(split(&fileencodings, ','), 'v:val != "ucs-bom"')
487    let encodings = ['us-ascii']
488    if $LC_CTYPE =~ 'UTF-8' " UTF-8 or en_US.UTF-8 or ru_RU.UTF-8 or such
489       let encodings += ['utf-8']
490    elseif $LC_CTYPE == 'ru_RU.KOI8-R'
491       let encodings += ['koi8-r', 'utf-8']
492    elseif v:lc_time == 'Russian_Russia.1251'
493       let encodings += ['cp1251', 'utf-8']
494    endif
495
496    for e in encodings
497       try
498          execute 'set fileencoding=' . e
499          w
500          break
501       catch /E513: write error, conversion failed/
502          continue
503       endtry
504    endfor
505
506    if &modified
507       throw '"' . expand('%') . '" E513: write error, conversion failed; tried ' . join(encodings, ',')
508    elseif has("spell")
509       call SetupSpell()
510    endif
511 endfunction
512
513 command! W call W()
514 endif
515
516
517 function! SlowTerm()
518    set laststatus=1
519    set noruler
520    set shortmess=aoOtT
521    set noshowcmd
522    set scrolljump=5 " The minimal number of lines to scroll vertically when the cursor gets of the screen
523    set sidescroll=5
524    set nottyfast
525    set notitle
526    set timeoutlen=5000
527    set nowildmenu
528    set wildmode=list:longest
529    set viminfo=!,h,'10,<100,s5
530    syntax off
531    highlight NonText cterm=NONE ctermfg=NONE
532 endfunction
533
534 if exists("$SLOWTERM")
535    call SlowTerm()
536 endif
537
538
539 if has("python")
540 python << END_OF_PYTHON
541 import sys, os
542
543 virtualenv_dir = os.environ.get('VIRTUAL_ENV')
544 if virtualenv_dir:
545     sys.path.insert(0, virtualenv_dir)
546     activate_this = os.path.join(virtualenv_dir, 'bin', 'activate_this.py')
547     execfile(activate_this, dict(__file__=activate_this))
548 END_OF_PYTHON
549 endif
550
551
552 " ----------
553 " From http://slobin.pp.ru/vim/_vimrc.html
554
555 " These options will be kept between editing sessions
556 " let options = []
557
558 " Called automagically after .viminfo and plugins are loaded, sets
559 " miscellaneous options from persistent global variables
560 " function! VimEnter()
561   " for optname in g:options
562   "   let varname = "g:OPT_" . toupper(optname)
563   "   if exists(varname)
564   "     execute "let &" . optname . " = " . varname
565   "   endif
566   " endfor
567
568   " if has("gui_running") && exists("g:WINPOSX") && exists("g:WINPOSY")
569   "   execute "winpos" g:WINPOSX g:WINPOSY
570   " endif
571 " endfunction
572
573 " Called automagically before .viminfo is saved, saves miscellaneous
574 " options into persistent global variables
575 " function! VimLeavePre()
576   " call filter(g:, 'v:key !~# "^OPT_"')
577   " for optname in g:options
578   "   let varname = "g:OPT_" . toupper(optname)
579   "   execute "let " . varname . " = &g:" . optname
580   " endfor
581
582   " if has("gui_running")
583   "   let g:WINPOSX = getwinposx()
584   "   let g:WINPOSY = getwinposy()
585   " endif
586 " endfunction
587
588 " autocmd VimEnter * call VimEnter()
589 " autocmd VimLeavePre * call VimLeavePre()
590
591
592 " Called automagically after every buffer read, enables fileencoding
593 " setting from modeline (see Tip #911: http://vim.wikia.com/wiki/VimTip911)
594 function! AutoEncoding()
595   if &modified && &fileencoding != ""
596     call SetupEncoding(&fileencoding)
597   else
598     redraw
599   endif
600   autocmd! auto-encoding
601   augroup! auto-encoding
602 endfunction
603
604 augroup auto-encoding
605 autocmd!
606 autocmd BufWinEnter * call AutoEncoding()
607 augroup END
608
609
610 let CONVERT=1
611
612 " Value of a character under cursor; better than standard '0x%02B (%b)'
613 function! HexDec()
614   let char = matchstr(getline("."), ".", col(".") - 1)
615   if g:CONVERT
616     let char = iconv(char, &encoding, &fileencoding)
617     let format = "0x%02X <%d>"
618   else
619     let format = "0x%02X (%d)"
620   endif
621   let char = char2nr(char)
622   return printf(format, char, char)
623 endfunction
624
625
626 if has("iconv")
627    " Helper function for :DecodeQP and :DecodeURL commands
628    function! DecodeHex(arg)
629      return iconv(printf("%c", str2nr(submatch(1), 16)), a:arg, &encoding)
630    endfunction
631
632    " Custom completion for encoding names
633    function! EncList(ArgLead, CmdLine, CursorPos)
634      return filter(split(&fileencodings, ','),
635                  \ "strpart(v:val, 0, strlen(a:ArgLead)) == a:ArgLead")
636    endfunction
637
638    if version >= 700
639    " Command for decoding qp-encoded text
640    command! -bar -nargs=? -range -complete=customlist,EncList DecodeQP
641           \ <line1>,<line2>s/=\(\x\x\|\n\)/\=DecodeHex(<q-args>)/eg
642
643    " Command for decoding url-encoded text
644    command! -bar -nargs=? -range -complete=customlist,EncList DecodeURL
645           \ <line1>,<line2>s/%\(\x\x\)/\=DecodeHex(<q-args>)/eg
646    endif
647 endif
648
649
650 if has("spell")
651    function! SetupSpell()
652       if &fileencoding =~ 'ascii'
653          setlocal spelllang=en spellfile=~/.vim/spell/en.ascii.add
654       elseif &fileencoding == 'koi8-r'
655          setlocal spelllang=en,ru spellfile=~/.vim/spell/en.ascii.add,~/.vim/spell/ru.koi8-r.add
656       elseif &fileencoding == 'utf-8'
657          setlocal spelllang=en,ru spellfile=~/.vim/spell/en.ascii.add,~/.vim/spell/ru.utf-8.add
658       else
659          setlocal spelllang= spellfile=
660       endif
661    endfunction
662    autocmd BufReadPost * call SetupSpell()
663 endif
664
665
666 function! SyntaxName()
667   echomsg synIDattr(synID(line("."), col("."), 1), "name")
668 endfunction
669
670
671 if has("python")
672
673 python << END_OF_PYTHON
674
675 import sys, rlcompleter, unicodedata, vim
676 from itertools import *
677 vim_complete = rlcompleter.Completer().complete
678
679 def vim_comp_list():
680   """Implementation of CompList() function"""
681   arglead = vim.eval("a:ArgLead")
682   fence = int(vim.eval("match(a:ArgLead, '\(\w\|\.\)*$')"))
683   left, right = arglead[:fence], arglead[fence:]
684   try:
685     completions = (vim_complete(right, i) for i in count())
686     candidates = list(takewhile(bool, completions))
687   except NameError:
688     candidates = []
689   suggestions = [left + x for x in candidates]
690   vim.command("return " + repr(suggestions))
691
692 def vim_calc(command):
693   """Implementation of :Calc command"""
694   global _
695   try:
696     result = eval(command)
697   except SyntaxError:
698     exec command in globals()
699   else:
700     if result != None:
701       print result
702       _ = result
703       xx = ''.join('\\x%02x' % ord(x) for x in str(_))
704       vim.command('let @" = "%s"' % xx)
705
706 def vim_pydo(command):
707   """Implementation of :Pydo command"""
708   codeobj = compile(command, "command", "eval")
709   line1 = vim.current.range.start
710   line2 = vim.current.range.end
711   delta = 0
712   for numz in range(line1, line2+1):
713     line = vim.current.buffer[numz-delta]
714     uline = unicode(line, vim.eval('&fileencoding'))
715     num = numz + 1
716     words = line.split()
717     result = eval(codeobj, globals(), locals())
718     if result is None or result is False:
719       del vim.current.buffer[numz-delta]
720       delta += 1
721       continue
722     if isinstance(result, list) or isinstance(result, tuple):
723       result = " ".join(map(str, result))
724     else:
725       result = str(result)
726     vim.current.buffer[numz-delta] = result
727
728 def vim_unicode_name():
729   try:
730     char = vim.eval("matchstr(getline('.'), '.', col('.') - 1)")
731     print map(unicodedata.name, char.decode(vim.eval("&encoding")))
732   except (AttributeError, ValueError), target:
733     print "%s: %s" % (target.__class__.__name__, target.message)
734
735 END_OF_PYTHON
736
737 " Custom completion for python expressions
738 function! CompList(ArgLead, CmdLine, CursorPos)
739   python vim_comp_list()
740 endfunction
741
742 " Python command line calculator
743 command! -nargs=+ -range -complete=customlist,CompList Calc
744        \ <line1>,<line2> python vim_calc(<q-args>)
745
746 " Python text range filter
747 command! -nargs=+ -range -complete=customlist,CompList Pydo
748        \ <line1>,<line2> python vim_pydo(<q-args>)
749
750 " Display unicode name for the character under cursor
751 command! Uname python vim_unicode_name()
752 command! UName call Uname()
753
754 endif
755 " ----------
756
757 " This has to go to the very end of ~/.vimrc to allow reading the .vimrc
758 set secure        " safer working with script files in the current directory