Here's an example:
function! TestCursorPosWithCount() range
echo virtcol('.')
endfunction
nmap <leader>c :call TestCursorPosWithCount()<CR>
If I press <leader>c
, the column number will be shown correctly. But, if I do something like 2<leader>c
, the cursor is moved and the column number will be that of the final cursor position.
So, the question: how can I get the original column number inside the function? I.e. the cursor position, as it was before pressing 2
in the last example?
After receiving the answer, I add here my findings: a small function demo with normal & visual mappings, that can extract line & column information from the given situation:
function! TestCursorPosWithCount(mode) range
if (a:mode == 'n')
echo 'normal mode'
echo 'count: ' . v:count
echo 'position: [' line('.') . ':' . col('.') ']'
else "visual mode
echo 'visual mode: ' . visualmode()
echo 'count: ' . v:count
if visualmode() !=# 'V'
echo 'range: [' a:firstline . ':' . col("'<") ' - '
\ a:lastline . ':' . col("'>") ']'
else
echo 'range: [' a:firstline ' - ' a:lastline ']'
endif
endif
endfunction
nmap <leader>c :<C-u>call TestCursorPosWithCount('n')<CR>
xmap <leader>c :call TestCursorPosWithCount('v')<CR>
<leader>
from the mapping, you have no problem calling the function at the right column ? While digging, I yet found nothing about your problem though ...:h func-range
. By usingrange
, the function will be called only once.