Consider the following line:
€ €
This file is encoded in UTF-8 and between the two Euro marks is a Tab character (the Stack Exchange software expands this to spaces). The byte value is:
e2 82 ac 09 e2 82 ac 0a
In my script I would like to get the character under the cursor.
Vim has several useful functions for dealing with strings in a multibyte-aware fashion, but getting the cursor position seems difficult:
col('.')
will return the byte position. So for the tab it will return4
rather than2
.virtcol('.')
almost works, but will expand tabs, so this isn't a true character position either.- All other functions behave as one of the two above.
What I have so far is:
fun! Getchar()
let l:line = substitute(getline('.'), '\t', repeat(' ', &tabstop), 'g')
return strcharpart(l:line, virtcol('.') - 1, 1)
endfun
nnoremap GG :echo Getchar()<CR>
Which almost works, but not for the above example since the displayed width is less than &tabstop
spaces long.
yl
plus some saving and restoring of register to take care of."
, which I don't use (my default is+
/*
). But you can use any one that won't bother you. Examples:nnoremap q :<C-u>exe 'norm! ""yl' \| echo @"<CR>
nnoremap q ""yl:echo @"<CR>