先日はじめてのvimスクリプト(vimでモーション(or選択範囲)をレジスタの内容で置き換えるオペレータ)を書いてみましたが、
Re: vimでモーション(or選択範囲)をレジスタの内容で置き換えるオペレータ @ 7bit - while ("im the true Vim master"); - vimグループ
でアドバイスを頂いたので&自分で他のスクリプト書いて少し慣れたので修正しました。マークやレジスタの退避やらなんやらは不要のような気がしたので除外。
機能的に前回と違うのは、レジスタ指定出来る(しない場合はデフォルトレジスタ)になったこと。v:register退避のためだけに呼ぶ関数がダーティな気はします。
" replace selection with register
function! s:ReplaceMotion(type, ...)
let reg = empty(s:lastreg) ? '"' : s:lastreg
let op_mode = 'v' " default: character
let marks = '<>' " default: visual mode
if !a:0 " normal mode
let marks = '[]'
if a:type == 'line'
let op_mode = 'V'
endif
endif
exe 'normal! `'.marks[1].'$'
let paste_cmd = getpos("'".marks[1]) == getpos('.') ? 'p' : 'P'
exe 'normal! `'.marks[0].'"_d'.op_mode.'`'.marks[1].'"'.reg.paste_cmd
endfunction
function! s:SaveReg()
let s:lastreg = v:register
endfunction
" default mapping
if !hasmapto('<Plug>ReplaceMotion', 'n')
nmap <silent> <C-K> <Plug>ReplaceMotion
endif
if !hasmapto('<Plug>ReplaceVisual', 'v')
vmap <silent> <C-K> <Plug>ReplaceVisual
endif
" export the plugin mapping
nnoremap <silent> <Plug>ReplaceMotion :<C-U>call <SID>SaveReg()<CR><ESC>:set opfunc=<SID>ReplaceMotion<CR>g@
vnoremap <silent> <Plug>ReplaceVisual :<C-U>call <SID>SaveReg()<CR><ESC>:call <SID>ReplaceMotion('', 1)<CR>
~/.vim/plugin/ 以下に放り込んでプラグインとして使います。
a:type==の所が’list’というわけのわからん物になっていたのでlineに直しました

No Comments yet »
コメント RSS TrackBack URI
コメントをどうぞ