Max
Max

Reputation: 99

Relative line numbers from a fixed line

I'm using vim to manage a datafile that will be consumed by a program, and the format of the data file is as follows:

<Header line 1>
<Header line 2>
<Header line 3>
<Data line 1>
<Data line 2>
...
<Data line N>

With this format, which I cannot change, the data values don't start until line 4 in the file. The output from the program, however, refers to data values by number, which makes it difficult to quickly search through the file to find the right line. I experimented with vim 7.3+'s :set relativenumber (:set rnu) option, but it's designed to continuously update the base line used to calculate the relative line numbers.

I'm wondering if there's a way to fix the base line at line 3, so that lines 4, 5, and 6 will show up as lines 1, 2, and 3 (consistent with the program output). Any help would be appreciated!

UPDATE: What I ended up doing was adding this option manually in the source code. Very few changes were required; all I did was copy all of the code for :set relativenumber into a new option called :set fixednumber and then just disabled the section that automatically updates the line number when the row changes (this section is in the vim source file move.c). Now there are three mutually exclusive modes:

:set number -- normal line numbers  
:set relativenumber -- automatically updating relative numbers  
:set fixednumber -- relative line numbers that are fixed against the currently selected row when the option was set

Upvotes: 1

Views: 1774

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172510

Vim is meant for editing text files as-is, so it'll require some contortions to make it suit your needs.

You can re-define the G command in your data file's buffers, like this:

:nnoremap <buffer> G :<C-u>execute (v:count ? v:count + 3 : line('$'))<CR>

The number column would be still off, though. To correct that, you'd have to remove the three initial header lines (maybe storing them in a buffer-local variable) when the buffer is loaded, and briefly re-insert them before the buffer is saved. This can be achieved with autocmds, but it's a little bit tricky to get right.

Another alternative is using a plugin like NarrowRegion, which opens a selected range in a scratch buffer, and synchronizes the contents back on save.

Upvotes: 0

romainl
romainl

Reputation: 196476

No. The 0 in relativenumber is always the line on which your cursor is and the 1 in number is always line 1 of the buffer/file.

You could, however, open a new window with only the lines 4 -> N and work there.

Or add a +3 to each "jump to line n"…

With this mapping:

nnoremap <F9> :3+

you'd just need to hit <F9>, type the line number and hit <Enter>.

Upvotes: 2

Related Questions