How to distinguish linear address and linear range from 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 different results.

Can I write a function that behaves like join ?
How to join distinguish between line address and line range?

+6
source share
1 answer

By defining a custom :command , the -range and -count allow you to better control how the range is used. However, I think that even this will not allow you to exactly duplicate the behavior of :join . The interface for Vim user commands is not as rich as it is for built-in commands.

As a workaround, you can use histget('cmd', -1) to get the command line that called your command and analyze the exact command call, including the original range (which can then be reused by passing it to another command, but execution linear arithmetic is problematic with it, since it is an unprocessed range, not the actual line numbers). The workaround will only work for interactive teams, is fragile and requires some effort. Perhaps you can avoid this problem altogether by specifying two different commands instead.

+4
source

Source: https://habr.com/ru/post/927805/


All Articles