René Nyffenegger
René Nyffenegger

Reputation: 40533

Is it possible for a "range" function to return a value

The following simple function is supposed to count and return the number of lines that are visually selected:

fu! TQ84_fu_test() range 

    return line("'> ")  - line("'<") + 1

endfu

If I try to call it like so

:'<,'>echo TQ84_fu_test()

Vim gives me the error E481 No range allowed (which is somehow documented)

Is there still a way to circumvent this behaviour, so that I can "fetch" the return value of a function operating on a range.

I assume I could set a global variable within the function and read this global variable after the function had been called, but, if possible, I'd rather not do that.

Upvotes: 1

Views: 301

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172648

range only works with :call, not :echo. Since you don't use the a:firstline / a:lastline special variables that come with range, you don't need to bother here (using visual mode and its marks), anyway.

Upvotes: 1

Benoit
Benoit

Reputation: 79205

You needn't '<,'> in the command line, because the :echo command does not operate on ranges. This will not prevent your function to use the < and > marks which still can be resolved.

So, just after hitting : use Ctrl-U to delete the marks and use :echo TQ84_fu_test(). This should work.

Upvotes: 2

Related Questions