2016-08-02 24 views

回答

2

要做到这一点安全,您需要恢复不仅是一个寄存器的内容,而且其类型:

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' 
+2

不要忘记调用'从'setr​​eg':在finally'区的功能,以防您可能会失败的操作 - 这经常发生。 –

+0

谢谢。我能找到的最好的例子是最后一段代码:http://vimhelp.appspot.com/usr_41.txt.html#41.9 让我知道如果你找到更好的东西! –

+0

嗯,我真的不能说“更好的东西”,但以防万一,在[lh-vim-lib](https://github.com/LucHermitte/lh-vim-lib)中,我已经定义了一个['#h##exit()'](https://github.com/LucHermitte/lh-vim-lib/blob/74e378709c558c1a5b54f1e8d811dcd386f3109a/doc/lh-vim-lib.txt#L283)函数意味着清理和恢复东西半自动。然而,它并没有使用'getregtype()',但是因为我没有意识到它(最近?)的存在。 –