我有一个概念验证的解决方案,适用于d
带有运动和可视模式的命令。它还不是一个完全健壮的解决方案(例如,d[count]d
不起作用,也不起作用[count]D
),但是几乎涵盖了我的所有实际用例。
它可以通过设置客户操作员功能来工作:
- 将寄存器1–8的内容存储在字典中,
- 执行删除到寄存器1中
- 将寄存器2–9设置为先前保存的寄存器1–8的内容。
请参阅:help map-operator
以获取有关操作员功能如何工作的解释。
function! ShiftingDeleteOperator(type)
let reg_dict = {}
for k in range(1, 8)
execute printf("let reg_dict[%d]=getreg('%d', 1)", k, k)
endfor
if a:type ==# 'v'
execute 'normal! `<v`>d'
elseif a:type ==# 'V'
execute 'normal! `<V`>d'
elseif a:type ==# "\<C-V>"
execute "normal! `<\<C-V>`>d"
elseif a:type ==# 'char'
execute 'normal! `[v`]d'
elseif a:type ==# 'line'
execute "normal! '[V']d"
else
return
endif
let deleted = getreg('"', 1)
call setreg(1, deleted)
for [k, v] in items(reg_dict)
execute printf("call setreg(%d, v)", k + 1)
endfor
endfunction
" Call the function for d{motion} via operatorfunc
nnoremap <silent> d :set operatorfunc=ShiftingDeleteOperator<CR>g@
" Call the function when d or x are hit in visual mode
vnoremap d :<C-U>call ShiftingDeleteOperator(visualmode())<CR>
vnoremap x :<C-U>call ShiftingDeleteOperator(visualmode())<CR>
" Use the d{motion} as defined above to add limited support for `D` command
nmap D d$
" Use the standard dd command
nnoremap dd dd