Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM: Zero-Indexed Line Numbers in :set number

Tags:

vim

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.

like image 637
Arafangion Avatar asked Jul 24 '09 08:07

Arafangion


Video Answer


2 Answers

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
like image 106
DrAl Avatar answered Sep 22 '22 15:09

DrAl


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.

like image 42
Aaron Digulla Avatar answered Sep 20 '22 15:09

Aaron Digulla