読者です 読者をやめる 読者になる 読者になる

僕のvimrcを公開するよ

なんでも、明日vimrc読書会なんてものが開催されるらしい。
第1回vimrc読書会開催のお知らせ
せっかくなので参加してみる*1。で、練習(?)に僕のvimrcを公開するよ。ちなみに、戦闘力は364。

どっかから取ってきたのがほとんどだけど、アレンジしたとこがあるのでちょっと説明。

let is_bash=1

vimにはbashというfiletypeはなくて、bashスクリプトでもshとみなされる。で、そのままではbash専用のところの色がおかしくなって困るんだけど、これを設定しておけばbashの色付けになってキレイだよ。

au BufReadCmd *.jar,*.xpi call zip#Browse(expand(""))

vimにはzipファイルのpluginがあって、zipファイルを直接読み書きできる。この設定でvimにzipファイルと思わせる拡張子が追加されるので、jarファイルとかでも同じように編集できるようになる。詳しくは:help zip-extensionを見てね。と言うか、そっくりそのままこれが書いてあるよ。

function! Navi()のところ

開いているファイル内の関数ジャンプができる。元ネタはEmacsのnavi.elらしいね。VimWikiのtips/92を参考に、ちょっとスマートに改造した(h[123456]h[1-6]にしたりとか)。JavaScriptのf: function() {}みたいな書き方にも対応してるのでいいんじゃないかな。まあctags使えばいいんだけど。

map関連

sは使わない。んで、sjで下のウィンドウ、とかしてる。で、J/Kで半ページ上下移動、行連結はS。あとC-KをESCにしてる。

vimrc全体は続きを読んでね。

" $Date: 2008-12-18 03:12:53 +0900 (木, 18 12月 2008) $
" $Revision: 323 $

" Remove ALL autocommands for the current group.
autocmd!

""""""""""""""""""""""""""""""""""""""""
"basic
""""""""""""""""""""""""""""""""""""""""
set nocompatible
set number
set showmode
set showcmd
set wildmode=list:longest
set backspace=0     "same as vi
set textwidth=0     "don't breake line however long it is
set nrformats-=octal    "don't use octal number
set history=50
let g:netrw_alto=1
let loaded_matchparen=1

set showmatch       "jump to the matching bracket
set matchtime=1     "1/10sec time scale

set list        "show non-printing character
set listchars=eol:.,tab:>\ ,trail:\         "end of line, tab, spases of <eol>

" When editing a file, always jump to the last cursor position
autocmd BufReadPost *
\ if line("'\"") > 0 && line ("'\"") <= line("$") |
\   exe "normal! g'\"" |
\ endif

"invert scroll bind for all windows
function! InvertScrollBindAll()
    if &scrollbind
        windo set noscrollbind
        echo "disable scrollbind"
    else
        windo set scrollbind
        echo "enable scrollbind"
    endif
endfunction

"invert number and list options
function! InvertList()
    if &number || &list
        set nonumber
        set nolist
    else
        set number
        set list
    endif
endfunction

"convert into HTML entity reference
function ConvertToHTMLEntityRef()
    let s:line = getline(".")
    let s:repl = substitute(s:line, '&', '\&amp;', "g")
    let s:repl = substitute(s:repl, '<', '\&lt;', "g")
    let s:repl = substitute(s:repl, '>', '\&gt;', "g")
    let s:repl = substitute(s:repl, '"', '\&quot;', "g")
    call setline(".", s:repl)
endfunction


""""""""""""""""""""""""""""""""""""""""
"statusline
""""""""""""""""""""""""""""""""""""""""
set laststatus=2    "always show statusline
set statusline=%n\:\ %f\ %m\ %r%{&foldenable!=0?'[fen]':''}%=%l/%L\ (%p%%)\ %{'['.(&fenc!=''?&fenc:&enc).']['.&ff.']'}


""""""""""""""""""""""""""""""""""""""""
"indent
""""""""""""""""""""""""""""""""""""""""
set tabstop=4
set shiftwidth=4
set softtabstop=0

"set expandtab

set autoindent
filetype plugin indent on


""""""""""""""""""""""""""""""""""""""""
"backup
""""""""""""""""""""""""""""""""""""""""
set backup
set backupcopy=yes
set backupdir=$HOME/.backup/vi,.


""""""""""""""""""""""""""""""""""""""""
"search
""""""""""""""""""""""""""""""""""""""""
set hlsearch
set incsearch

"invert highlight matching
function! InvertHlsearch()
    if &hlsearch
        set nohlsearch
        echo "disable hlsearch"
    else
        set hlsearch
        echo "enable hlsearch"
    endif
endfunction

"search highlighted word in visual mode
vnoremap * "uy/<C-R>u<CR>
vnoremap # "uy?<C-R>u<CR>

""""""""""""""""""""""""""""""""""""""""
"color
""""""""""""""""""""""""""""""""""""""""
set background=light
"set background=dark

syntax enable
"syntax off

"Tab
highlight SpecialKey ctermfg=Grey
"End of line
highlight NonText ctermfg=Grey
"Search pattern
highlight Search term=reverse ctermfg=White ctermbg=Blue

" Popup Menu
hi Pmenu cterm=NONE ctermfg=Black ctermbg=White
hi PmenuSel cterm=underline,bold ctermfg=White ctermbg=DarkBlue
hi PmenuSbar term=NONE cterm=NONE
hi PmenuThumb term=reverse cterm=reverse

"highlight zenkaku space
function! ZenkakuSpaceHighlight()
    syntax match ZenkakuSpace " " display containedin=ALL
    highlight ZenkakuSpace term=underline ctermbg=LightCyan
endf

if has("syntax")
    syntax on
    augroup invisible
        autocmd! invisible
        autocmd BufNew,BufRead * call ZenkakuSpaceHighlight()
    augroup END
endif

""""""""""""""""""""""""""""""""""""""""
"language
""""""""""""""""""""""""""""""""""""""""
language C


""""""""""""""""""""""""""""""""""""""""
"character and linefeed code
""""""""""""""""""""""""""""""""""""""""

"auto recognition (from zun wiki)
"linefeed code
set fileformats=unix,dos,mac

if exists('&ambiwidth')
    set ambiwidth=double
endif

"character code
if &encoding !=# 'utf-8'
    set encoding=japan
    set fileencoding=japan
endif

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

    if iconv("\x87\x64\x87\x6a", 'cp932', 'eucjp-ms') ==# "\xad\xc5\xad\xcb"
        " iconv support eucJP-ms
        let s:enc_euc = 'eucjp-ms'
        let s:enc_jis = 'iso-2022-jp-3'
    elseif iconv("\x87\x64\x87\x6a", 'cp932', 'euc-jisx0213') ==# "\xad\xc5\xad\xcb"
        " iconv suppot JISX0213
        let s:enc_euc = 'euc-jisx0213'
        let s:enc_jis = 'iso-2022-jp-3'
    endif

    " set fileencodings
    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\|euc-jisx0213\|eucjp-ms\)$'
            set fileencodings+=cp932
            set fileencodings-=euc-jp
            set fileencodings-=euc-jisx0213
            set fileencodings-=eucjp-ms
            let &encoding = s:enc_euc
            let &fileencoding = s:enc_euc
        else
            let &fileencodings = &fileencodings .','. s:enc_euc
        endif
    endif

    " unlet variables
    unlet s:enc_euc
    unlet s:enc_jis
endif

if has('autocmd')
    function! AU_ReCheck_FENC()
        if &fileencoding =~# 'iso-2022-jp' && search("[^\x01-\x7e]", 'n') == 0
            let &fileencoding=&encoding
        endif
    endfunction
    autocmd BufReadPost * call AU_ReCheck_FENC()
endif


"code conversion functions
function! ToUtf8()
    set fileencoding=utf-8
    set fileformat=unix
endfunction

function! ToShiftJis()
    set fileencoding=cp932
    set fileformat=dos
endfunction

function! ToEucJp()
    set fileencoding=euc-jp
    set fileformat=unix
endfunction


""""""""""""""""""""""""""""""""""""""""
"filetype
""""""""""""""""""""""""""""""""""""""""
"sh
let is_bash=1

"zip
"see also :help zip-extension
au BufReadCmd *.jar,*.xpi call zip#Browse(expand("<amatch>"))


""""""""""""""""""""""""""""""""""""""""
"fold
""""""""""""""""""""""""""""""""""""""""
"set foldmethod=syntax
set foldmethod=indent

set nofoldenable


""""""""""""""""""""""""""""""""""""""""
"navi
""""""""""""""""""""""""""""""""""""""""

function! Navi()
  if &ft ==? "c" || &ft ==? "cpp"
    vimgrep /^[^ \t#/\\*]\+[0-9A-Za-z_ :\t\\*]\+([^;]*$/j %
  elseif &ft ==? "perl"
    vimgrep /^\s*sub\s/j %
  elseif &ft ==? "ruby"
    vimgrep /^\s*\(class\|module\|def\|alias\)\s/j %
  elseif &ft ==? "python"
    vimgrep /^\s*\(class\|def\)\s/j %
  elseif &ft ==? "javascript"
    vimgrep /^\s*function\s\|[a-zA-Z_$][a-zA-Z0-9_$]*\s*[=:]\s*function\s*(/j %
  elseif &ft ==? "sh"
    vimgrep /^\s*\(\h\w*\s*()\|function\s\+\h\w*\)/j %
  elseif &ft ==? "html"
    vimgrep /\c^\s*\(<h[1-6]\|<head\|<body\|<form\)/j %
  elseif &ft ==? ""
    "Text ( 1. 2. ,etc )
    vimgrep /^\s*\d\+\./j %
  elseif &ft ==? "java"
    vimgrep /^\s*[^#/\*=]\+[0-9a-zA-Z_ \t\*,.()]\+{[^;]*$/j %
  elseif &ft ==? "diff"
    "diff (unified format)
    vimgrep /^@@[0-9 \t,+-]\+@@$/j %
  else
    echo "This filetype is not supported."
  endif
  cw
endfunction


""""""""""""""""""""""""""""""""""""""""
"map
""""""""""""""""""""""""""""""""""""""""
"no effect keys
map s <Nop>
map <SPACE> <Nop>

"make CTRL-K an additional ESC
noremap <C-K> <ESC>
cnoremap <C-K> <C-C>
inoremap <C-K> <ESC>

noremap Y y$
noremap gh ^
noremap gl $
noremap S gJ

"define tcsh style editing keys
cnoremap <C-A> <Home>
cnoremap <C-F> <Right>
cnoremap <C-B> <Left>

"scroll
noremap J <C-D>
noremap K <C-U>

"jump
"exclusive motion
noremap sm `
"linewise motion
noremap sl '

"buffer operation
nnoremap sp :bp<CR>
nnoremap sn :bn<CR>

"window operation
"open and close
noremap ss <C-W>s
noremap sc <C-W>c
noremap so <C-W>o
"move
noremap sj <C-W>j
noremap sk <C-W>k
"resize
noremap + <C-W>+
noremap - <C-W>-
noremap z<CR> <C-W>_
noremap z0 1<C-W>_
noremap z. <C-W>=

nnoremap <Space>w :<C-u>write<CR>
nnoremap <Space>q :<C-u>qall<CR>

"call function
"invert hlsearch
nnoremap <SPACE>h :call InvertHlsearch()<CR>
"invert scrollbind
nnoremap sb :call InvertScrollBindAll()<CR>
"navi
nnoremap g<SPACE> :call Navi()<CR>
nnoremap go :copen<CR>
nnoremap gc :cclose<CR>

"invert number and list options
nnoremap <silent> sv :call InvertList()<CR>

"convert into HTML entity reference
map sh :call ConvertToHTMLEntityRef()<CR>

""""""""""""""""""""""""""""""""""""""""
"for plugins
""""""""""""""""""""""""""""""""""""""""
"NERD_tree
noremap sd :NERDTreeToggle<CR>

"eregex
nnoremap / :M/
nnoremap ,/ /

"bufferlist
map <silent> sf :call BufferList()<CR>
hi BufferSelected term=underline ctermfg=DarkMagenta cterm=underline
hi BufferNormal term=NONE ctermfg=DarkBlue

"closetag.vim
if filereadable($HOME . '/.vim/scripts/closetag.vim')
    au Filetype html,xml,xsl source $HOME/.vim/scripts/closetag.vim
endif

"xul.vim (Syntax for XUL)
let xul_noclose_script = 1

"autocomplpop.vim JavaScript
let g:AutoComplPop_Behavior={'javascript' : [
      \     {
      \       'command'  : "\<C-n>",
      \       'pattern'  : '\k\k$',
      \       'excluded' : '^$',
      \       'repeat'   : 0,
      \     },
      \     {
      \       'command'  : "\<C-x>\<C-f>",
      \       'pattern'  : (has('win32') || has('win64') ? '\f[/\\]\f*$' : '\f[/]\f*$'),
      \       'excluded' : '[*/\\][/\\]\f*$\|[^[:print:]]\f*$',
      \       'repeat'   : 1,
      \     },
      \     {
      \       'command'  : "\<C-x>\<C-o>",
      \       'pattern'  : '\([^. \t]\.\)$',
      \       'excluded' : '^$',
      \       'repeat'   : 0,
      \     },
      \   ]}

" fuzzyfinder.vim
let g:FuzzyFinderOptions = { 'Base':{}, 'Buffer':{}, 'File':{}, 'Dir':{}, 'MruFile':{}, 'MruCmd':{}, 'Bookmark':{}, 'Tag':{}, 'TaggedFile':{}}
let g:FuzzyFinderOptions.Base.ignore_case = 1
let g:FuzzyFinderOptions.Base.key_open_vsplit = '<C-v>'

let g:FuzzyFinderOptions.File.excluded_path = '\v\.svn|\~$|\.o$|\.exe$|\.bak$|\.swp$|((^|[/\\])\.[/\\]$)'
let g:FuzzyFinderOptions.Dir.excluded_path = '\v\.svn|((^|[/\\])\.{1,2}[/\\]$)'

nnoremap <silent> <SPACE>b :FuzzyFinderBuffer<CR>
nnoremap <silent> <SPACE>f :FuzzyFinderFile<CR>
nnoremap <silent> <SPACE>r :FuzzyFinderMruFile<CR>
nnoremap <silent> <SPACE>c :FuzzyFinderMruCmd<CR>
nnoremap <silent> <SPACE>d :FuzzyFinderDir<CR>
nnoremap <silent> <SPACE>A :FuzzyFinderAddBookmark<CR>
nnoremap <silent> <SPACE>B :FuzzyFinderBookmark<CR>
nnoremap <silent> <SPACE>E :FuzzyFinderEditInfo<CR>


""""""""""""""""""""""""""""""""""""""""
"source local rc file
""""""""""""""""""""""""""""""""""""""""
if filereadable($HOME . '/.vimrc_local')
    source $HOME/.vimrc_local
elseif filereadable($HOME . '/_vimrc_local')
    source $HOME/_vimrc_local
endif

" vim:ft=vim:

*1:追記: 用事が入って参加できなくなった