kev
kev

Reputation: 161864

How to distinguish between line-address and line-range in vim?

This is a simple user-defined function:

fun! Foo() range
    echo a:firstline a:lastline
endfun

Can I write a function which behave like join?
How does join distinguish between line address and line range?

Upvotes: 6

Views: 119

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172648

By defining a custom :command, the -range and -count attributes allow you better control over how the range is consumed. However, I think even that won't allow you to exactly duplicate the behavior of :join. The interface for custom Vim commands is not as rich as what is available to built-in commands.

As a workaround, you could use histget('cmd', -1) to get the command-line that invoked your command, and parse the exact command invocation, including the original range (which can then be re-used by passing it to another command, but doing line arithmetic with it is problematic, since it's the raw range, not the actual line numbers). The workaround will only work for interactive commands, is brittle, and demands some effort. Maybe you can avoid the issue altogether by defining two different commands instead.

Upvotes: 4

Related Questions