2016-08-02 47 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()',但是因爲我沒有意識到它(最近?)的存在。 –