1

I'm building a CLI application where you're supposed to be able to open files at particular lines.

I noticed that many applications (vim, nano, kakoune) use the + argument to allow you to open a file at a certain line:column. However, this is not supported by all applications, for example vis and vi.

I notice that the application tig is able to figure out and open the correct line regardless, and I wonder how it can do it. Is there a generic way to open files at a particular line within a terminal?

1
  • 1
    Maybe this tig application is open source? You could then simply check what it does. My guess: Explicit handling for all the cases the author could think of.
    – Daniel B
    Commented May 22 at 13:57

1 Answer 1

1

The tig application (which is indeed open source under the GPL-2.0 license) supports opening a file with an editor at a particular line in some specific way. It does so by adding the line argument after the name of the file to edit, e.g.:

$EDITOR filename.txt +42

This works with vim but does not work with, say, emacs, nano or pico. If the editor does not support this line number argument and exits with an error (which is not necessarily the case, e.g., nano assumes the line number argument is the name of a second file to open), then tig reports a message and disables the line number argument.

I cannot see how any application could guess the way in which an arbitrary editor can open a file at a specific line. If you are interested in something that works like tig but with more editors, I suggest pattern matching the contents of $EDITOR and acting differently for the most common editors.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .