Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get text-object under cursor via vim script?

Tags:

vim

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).

like image 330
Yosh Avatar asked Dec 21 '25 12:12

Yosh


1 Answers

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
like image 140
Ingo Karkat Avatar answered Dec 23 '25 04:12

Ingo Karkat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!