This is a simple user-defined function:
fun! Foo() range
echo a:firstline a:lastline
endfun
:5call Foo()
and :5,5call Foo()
give me the same result.:5j
and :5,5j
give me different results.Can I write a function which behave like join
?
How does join
distinguish between line address and line range?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With