3

I want to add support for various formats of filenename with linenumber for vim to understand. E.g. I get output from git grep <keyword> like this:

first/foo.py:123:
first/bar.py:333:
second/foo.py:777:

To open these files and jump to a specific line I have to change the format to e.g.:

first/foo.py +123
first/bar.py +333
second/foo.py +777

I wonder if there is a way to automatically make this conversion so that in command line this statement:

vim first/foo.py:123:

would have the same result as this command:

vim first/foo.py +123

It would also be great if vim could understand Python traceback line format, e.g.:

File "/third/foo.py", line 55,   
2

2 Answers 2

2

I wonder if there is a way to automatically make this conversion so that in command line this statement: vim first/foo.py:123: would have the same result as this command: vim first/foo.py +123.

There's a Vim plug-in that implements precisely that: lervag/file-line.

It recognizes common separators such as colons or also parens (first/foo.py(123)), and it can take an optional second number to indicate which column to switch to.

It works both on the command-line (vim first/foo.py:123:) or on the :edit command inside Vim (:e first/foo.py:123:).

How to pre-process command line arguments provided to vim?

You can look at the implementation of that plug-in to see how it does it... It's a bit complicated, but it's under 100 lines of Vimscript.


Having said that, using the quickfix feature of Vim is probably the most appropriate way to handle this. You can set 'errorformat' appropriately to handle other file:line formats (such as the one from Python exceptions.) And you can use the -q command-line option to start Vim with a list of references to lines to populate the quickfix list as you open Vim.

2
  • Was not able to install bogado/file-line using Vundle, however lervag/file-line has worked.
    – niekas
    Commented Aug 28, 2020 at 7:32
  • @niekas Thanks! Looks like that fork is actually better maintained... I updated the answer to point to that one now.
    – filbranden
    Commented Aug 28, 2020 at 7:59
4

This doesn't answer your question, but it does answer your use-case.

If you do :let &grepprg = "git grep --line-number" then running :grep <keyword> inside of vim will populate your quickfix list. Which you can navigate with :cnext and the like.


So how does this work at a high level? The vim command :grep invokes a command specified by the vim option grepprg, and tries to parse the output with the vim option errorformat (effectively a list of scanf-style "matchers"). Lines that can be matched with an errorformat "matcher" are parsed according to the matcher, and used to populate the quickfix list. Output lines from an invocation of grepprg that cannot be matched are ignored.

Knowing this, you could write an errorformat matcher for anything you want, including File "/third/foo.py", line 55,:

:let &errorformat += 'File "%f", line %l,'

For a full understanding/low level, check out:

  • :help 'grepprg' and
  • :help quickfix and
  • :help 'errorformat'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.