Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

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.
  • However, :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?

like image 661
kev Avatar asked Oct 16 '12 04:10

kev


1 Answers

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.

like image 56
Ingo Karkat Avatar answered Oct 01 '22 23:10

Ingo Karkat