最近の .vimrc (ファイルタイプ毎に括弧の補完を切り替える)

最新の .vimrc 等は、ここで晒してます。

"==<dir>======================================================================
set backupdir=~/.vim/backup
let &directory=&backupdir

"==<etc>======================================================================
set shortmess+=I
set nu!
set cmdheight=1
set hidden
set history=60

syntax on

"==<tab>======================================================================
set cindent
set expandtab

autocmd BufNewFile,BufRead * call SetTab()

function SetTab()
  if &syntax == 'javascript' || &syntax == 'html' || &syntax == 'xhtml' || &syntax == 'css' || &syntax == 'tt2html' || &syntax == 'yaml' || &syntax == 'vim'
    execute 'set softtabstop=2 | set shiftwidth=2 | set tabstop=2'
  else
    execute 'set softtabstop=4 | set shiftwidth=4 | set tabstop=4'
  endif
endf

"==<search>===================================================================
set magic
set ignorecase
set smartcase
set hlsearch
set incsearch

map <F2> :!open -a safari 'http://www.google.co.jp/search?q=<cword>'<CR>
map <F3> :!open -a safari 'http://search.cpan.org/search?query=<cword>&mode=all'<CR>

"==<status line>==============================================================
set laststatus=2
set wildmenu
set ruler
set statusline=%<%f? %m%r%h%w%{'['.(&fenc!=''?&fenc:&enc).']['.&ff.']'}%=%l,%c%V%8P

"==<color>====================================================================
colorscheme desert

set listchars=tab:>_
set list
highlight SpecialKey guifg=#555555

"==<encode>===================================================================
if &encoding !=# 'utf-8'
  set encoding=japan
endif

set fileencoding=japan
if has('iconv')
  let s:enc_euc = 'euc-jp'
  let s:enc_jis = 'iso-2022-jp'

  if iconv("?x87?x64?x87?x6a", 'cp932', 'euc-jisx0213') ==# "?xad?xc5?xad?xcb"
    let s:enc_euc = 'euc-jisx0213'
    let s:enc_jis = 'iso-2022-jp-3'
  endif

  if &encoding ==# 'utf-8'
    let s:fileencodings_default = &fileencodings
    let &fileencodings = s:enc_jis .','. s:enc_euc .',cp932'
    let &fileencodings = &fileencodings .','. s:fileencodings_default
    unlet s:fileencodings_default
  else
    let &fileencodings = &fileencodings .','. s:enc_jis
    set fileencodings+=utf-8,ucs-2le,ucs-2
    if &encoding =~# '^euc-?%(jp?|jisx0213?)$'
      set fileencodings+=cp932
      set fileencodings-=euc-jp
      set fileencodings-=euc-jisx0213
      let &encoding = s:enc_euc
    else
      let &fileencodings = &fileencodings .','. s:enc_euc
    endif
  endif

  unlet s:enc_euc
  unlet s:enc_jis
endif

autocmd FileType cvs :set fileencoding=euc-jp
autocmd FileType svn :set fileencoding=utf-8

"==<dictionary>===============================================================
autocmd Syntax perl set dictionary=~/.vim/dict/perl.dict | set iskeyword+=:
set complete+=k

"==<head comment>=============================================================
iab papp <ESC>:r ~/.vim/templates/perl_application.pl<CR>
iab ppkg <ESC>:r ~/.vim/templates/perl_package.pl<CR>
iab pcls <ESC>:r ~/.vim/templates/perl_class.pl<CR>
iab psub <ESC>:r ~/.vim/templates/perl_subroutine.pl<CR>

"==<buffer>===================================================================
map <LEFT> :bp!<CR>
map <RIGHT> :bn!<CR>
map <UP> :ls<CR>
map <DOWN> :b<SPACE>

"==<syntax check>=============================================================
map ,t :call SyntaxCheck()<CR>

function SyntaxCheck()
  execute ':w'
  if &syntax == 'perl'
    execute ':!perl -cw %'
  elseif &syntax == 'ruby'
    execute ':!ruby -cW %'
  elseif &syntax == 'javascript'
    execute ':!~/bin/js_checker.pl %'
  elseif &syntax == 'yaml'
    execute ':!~/bin/yaml_checker.pl %'
  elseif &syntax == 'html'
    execute '!tidy -quiet -errors --gnu-emacs yes %'
  endif
endf

map ,r :w<CR>:!~/bin/reload_firefox.sh<CR>

"==<move>=====================================================================
nnoremap j gj
nnoremap k gk
nnoremap * g*

"==<pair>=====================================================================
set showmatch
set matchpairs+=<:>

inoremap ( ()<ESC>i
inoremap { {}<ESC>i
inoremap [ <C-R>=AddPair('[')<CR>
inoremap < <C-R>=AddPair('<')<CR>

function AddPair(char)
  if a:char == '['
    if &syntax == 'tt2html'
      return "[%%]?<LEFT>?<LEFT>"
    else
      return "[]?<LEFT>"
    endif
  elseif a:char == '<'
    if &syntax == 'html' || &syntax == 'xhtml' || &syntax == 'tt2html' || &syntax == 'vim'
      return "<>?<LEFT>"
    else
      return '<'
    endif
  endif
endf

inoremap ) <C-R>=ClosePair(')')<CR>
inoremap } <C-R>=ClosePair('}')<CR>
inoremap ] <C-R>=ClosePair(']')<CR>
inoremap > <C-R>=ClosePairHtml('>')<CR>

function ClosePair(char)
  if getline('.')[col('.') - 1] == a:char
    return "?<RIGHT>"
  else
    return a:char
  endif
endf

function ClosePairHtml(char)
  if &syntax == 'html' || &syntax == 'xhtml' || &syntax == 'tt2html' || &syntax == 'vim'
    return ClosePair(a:char)
  else
    return a:char
  endif
endf

nmap ( i(<ESC>lxea)<ESC>
nmap { i{<ESC>lxea}<ESC>
nmap [ i[<ESC>lxea]<ESC>

vmap ( "zdi(<C-R>z)<ESC>
vmap { "zdi{<C-R>z}<ESC>
vmap [ "zdi[<C-R>z]<ESC>

nmap ' i'<ESC>ea'<ESC>
nmap " i"<ESC>ea"<ESC>

vmap ' "zdi'<C-R>z'<ESC>
vmap ," "zdi"<C-R>z"<ESC>

"==<comment out>==============================================================
map ,c :call CommentOut()<CR>

function CommentOut()
  if &syntax == 'perl' || &syntax == 'ruby' || &syntax == 'sh' || &syntax == 'yaml'
    execute ':s/^/#/'
  elseif &syntax == 'javascript'
    execute ':s/^/?/?//'
  elseif &syntax == 'vim'
    execute ':s/^/"/'
  elseif &syntax == 'html' || &syntax == 'xhtml' || &syntax == 'tt2html'
    execute ':s/^?(.*?)$/<!-- ?1 -->/'
  endif

  execute ':nohlsearch'
endf

autocmd Syntax perl,ruby vmap ,b "zdi=begen<CR><C-R>z<CR>=end<ESC>
autocmd Syntax javascript vmap ,b "zdi/*<C-R>z*/<ESC>
autocmd Syntax html,xhtml,tt2html vmap ,b "zdi<!--<C-R>z--><ESC>

余談ですが、最近、Vi Input Manager + Safari で日記を書いています・・・すぐ変になるけど便利(w