I've found answers for how to get the word/WORD under the cursor or the character under the cursor, or the whole line (getline(".")). How can I get a given text-object under the cursor?
More specifically, I want to get what would be selected by vi', and use that characters in a function (without messing up the registers).
The easiest and most straightforward is indeed by yanking to a register; just make sure you save and restore the original contents.
let l:save_clipboard = &clipboard
set clipboard= " Avoid clobbering the selection and clipboard registers.
let l:save_reg = getreg('"')
let l:save_regmode = getregtype('"')
normal! yi'
let l:text = @@ " Your text object contents are here.
call setreg('"', l:save_reg, l:save_regmode)
let &clipboard = l:save_clipboard
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With