2012年5月16日水曜日

surround.vimの拡張

このエントリーをはてなブックマークに追加

デフォルトのsurround.vimでは、囲み文字で囲まれた文字を削除する場合は、「di'」、「di"」の様に囲み文字を指定する必要がある。

・di"
"Hello World" → ""
・di'
'Hello World' → ''

自動的に囲み文字を判別してくれて、削除してくれたら便利だな、と思ったので、スクリプトを書いてみた

nnoremap ci :call ExSurround("ci")
nnoremap di :call ExSurround("di")
function! s:ExSurround(cmd)
    let pattern = "'\"{[("
    let front = strpart(getline("."), 0, col("."))
    let max = -1
    for pat in split(pattern, '.\zs')
        let pos = strridx(front, pat)
        if pos > max
            let max = pos
        endif
    endfor
    if max >= 0
        let sorround = strpart(front, max, 1)
        call feedkeys(a:cmd . sorround, 'n')
    endif
endfunction


・di
"Hello World" → ""
'Hello World' → ''

対応している文字は'、"、{、[、(