tags:

views:

401

answers:

2

Using VIM, it is easy to display line numbers for any given file with:

:set number

However, the line numbering is 1-indexed, this means that the numbering starts at 1. Normally this is exactly what i want, however a particular tool I am using to compile my code reports zero-indexed line numbers - that is, line numbers start at zero.

How do I change vim's line numbering to compensate so that viewing the numbers in the vim buffer corresponds to the errors provided by the tool, and in addition jumping to a particular number corresponds to that of the tool as well.

That is, if the tool tells me that there is an error on line 98, I want to jump to that line by typing "98G", not "97G", and I want that line (which is really line 97 in vim) to display "98" in the line number list.

Edit:

While I can filter the output of the tools, while fairly simple it is not a trivial task because the tool also outputs hex values that correspond to each line, which has the same zero-indexed form, and this is is output in informational messages as well, not just for errors, and I have many such projects.

+2  A: 

I suggest to wrap your tool in a small script. In the script, either copy the source code and add an empty line at the top of the source or use awk to fix the output (parse the error messages and add 1).

The only way to make vim display line numbers starting with 0 is to patch the source and create a new option (say lno for line number offset) and add this value to the line number. You'd need to patch this in quite a few places (display, goto commands, search patterns, etc). Shouldn't take more than one or two years to make it work :) Good luck on getting the author of Vim to accept this as a patch.

Aaron Digulla
The first is infeasible, but the latter is a potential solution, however I specifically want to try doing this in vim first.
Arafangion
(That is, it is feasible to use sed, but would prefer not to and use vim instead as an autocmd)
Arafangion
+3  A: 

I don't think this is possible; however, assuming the output of your external tool is just text, it would probably be fairly easy to filter the output such that the numbering is 1-indexed to match Vim. Can you give us an example of your output?

Edit

Alternatively, if you call the external command from Vim, you could do something like (basically, add a blank line, run the external command and then delete the blank line):

command! RunMyExternalProgramme call RunMyExternalProgramme()

func! RunMyExternalProgramme()
    " Save the old setting of makeprg
    let s:savedMakePrg = &makeprg

    " Save the screen layout
    let s:savedView = winsaveview()

    if config_file != ''
        " Put a blank line at the start of the file
        :1put! =''
        " Save
        write
        " Change makeprg and run it
        let &makeprg = '/path/to/programme -options etc'
        make
        " Delete the blank line
        1d
        " Save
        write
    endif

    " Restore the screen layout (optional)
    call winrestview(s:savedView)

    " Restore the old setting of 'makeprg'
    let &makeprg = s:savedMakePrg
endfunc
Al
The output is such that it would be a reasonably simple task, however I would have to do such filtering on every project for which I use the tool. I would prefer an autocmd trigger that tells vim to adjust its line numbering instead, if possible.
Arafangion
Do you call the external tool from Vim (e.g. with "makeprg")?
Al
I think this is the most ideal approach for my situation, thanks AI!If it doesn't work, I'll figure something similar out along those lines. :)
Arafangion