Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM: Determine if function called from visual-block mode

Tags:

vim

From within a function, how can it be determined if the function was called from visual-block mode. This involves calling the function both from:

  • normal-mode mapping
  • command-line mode

For the precise function, luckily it behaves identically under either normal/command mode or visual-mode with a single line selected. Obviously with more than one line - from a:firstline/lastline - the function was not called from normal mode.

The problem now is that I need to know if I am in visual-block mode, single line or not.

I've tried all the following to no avail:

function! T() range
    echo [a:firstline, a:lastline]
    echo [getpos("'<")[1:2], getpos("'>")[1:2]]
    echo visualmode()
    echo mode()
endfun
vnoremap TT :call T()<CR>
nnoremap TT :call T()<CR>

Output from visual-line mode (notice crazy max-int output):

[3, 4]
[[3, 1], [4, 2147483647]]
V
n

I require answers of the variety either "can't be done" or "step-by-step". No vague do-this then do-that then finish with this... I'm tired of jumping through obscure VIM Goldberg-esque loopholes just to accomplish simple tasks not provided by any built-in functionality, and honestly my vim-fu is still young.

like image 739
user19087 Avatar asked Sep 23 '26 13:09

user19087


1 Answers

Just invoke your function like this:

vnoremap TT :call T(visualmode())<CR>
nnoremap TT :call T('')<CR>

With an empty argument, the function was invoked from normal mode. Else, the passed character represents the visual mode (i.e. v vs. V vs. ^V).

like image 154
Ingo Karkat Avatar answered Sep 25 '26 06:09

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!