Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim (vimscript) get exact character under the cursor

Tags:

vim

I am getting the character under the cursor in vimscript the following way:

getline('.')[col('.')-1] 

It works exactly like it should, however there is something I dislike. consider this [] the cursor. When there is a bracket next to the cursor like so: }[] , ][] , )[] or {[] the cursor actually returns the bracket. What do I have to set so it will always return the character exactly under the cursor or atleast ignore if there is a bracket to it's left?

Note: I suspect that it might have to do with the brackets highlight, though I am not sure.

Note2: for the situation to occur there has to be a matching bracket.

like image 484
Stanimirovv Avatar asked Apr 27 '14 13:04

Stanimirovv


1 Answers

Though I cannot reproduce the problem you're describing, there's another problem with your code: Because of the string indexing (and this is one of the uglier sides of Vimscript), it only works with single-byte characters, but will fail to capture chars like Ä or 𠔻 (depending on the encoding used). This is a better way of capturing the character under the cursor:

:echo matchstr(getline('.'), '\%' . col('.') . 'c.')

Edit: Since about Vim 7.4.1742, Vim has new strgetchar() and strcharpart() functions that work with character indexes, not byte addressing. This is helpful in many circumstances, but not here, because you still can only get the byte-index position of the cursor (or the screen column with virtcol(), but that's not the same as character index).

like image 104
Ingo Karkat Avatar answered Oct 23 '22 00:10

Ingo Karkat