vimscript:如何保存和恢复注册?

时间:2016-08-02 12:28:50

标签: vim

如何保存寄存器的当前状态,然后稍后将其恢复?

我想确保我的功能没有意外的副作用。

1 个答案:

答案 0 :(得分:4)

要安全地执行此操作,您不仅需要还原寄存器的内容,还需要还原其类型:

let old_reg = getreg("a")               " Save the current content of register 'a'
let old_reg_type = getregtype("a")      " Save the type of the register as well

" <Use the register 'a' as a temporary scratch pad here>

call setreg("a", old_reg, old_reg_type) " Restore register 'a'