2

Following an example such as:

##1##
many text
and lines
##z##
##2##
more text
##z##
...etc

How could i make the line number display only on chunks of text, inside of the ##digit## and the ##z## string? Something like this:

0 ##1##
1 many text
2 and lines
0 ##z##
0 ##2##
1 more text
0 ##z##

So that it only show line number for the in-between text.

2 Answers 2

2

I have similar problem so I have developed and posted a plugin (https://github.com/rickhowe/partialnumber.vim).

In your example,

:g/^##.##\n\ze[^#]/+1,/[^#]\zs\n##.##$/ SetPNU

would show numbers in sign column.

1
  • I searched for this for days on github and somehow missed this :D Commented Sep 9, 2020 at 4:22
2

You cannot. From :h 'number' (emphasis mine):

Print the line number in front of each line

You can only enable number or relativenumber for the whole window.

See:

:help 'number'
:help 'relativenumber'

Edit:

In Vim, this isn't feasible because 'signcolumn' has a max width of 2 characters. For some reason, even though Neovim supports larger sign columns, each individual sign text must still be 1 or 2 characters. If this were not the case, it would be possible in Neovim (Relevant source code). This would have to be changed.

This might be easier done in Lua (I don't know, I've never written a Lua plugin for Nvim), but here is a solution in Vimscript. No guarantees about performance or "correctness" (it's kind of just thrown together), but it appears to work, let me know if I've missed some bug:

let g:custom_number_delimiter='##'
let s:sign_id=500 " script level variable of current sign ID

function! s:get_section_lines() abort
    " save cursor pos to restore
    let l:cursor_pos=[line('.'), col('.')]

    " Get line number by:
    " 1. :g/re/p for start of sections
    " 2. split output of :g/re/p into List
    " 3. isolate line numbers from output
    let l:section_start_lnums=s:isolate_lnums(split(execute('g/' . g:custom_number_delimiter . '.\+' . g:custom_number_delimiter . '/p#'), "\n"))

    " no matches
    if len(l:section_start_lnums) == 0
        return []
    endif

    " restore cursor pos (:g/re/p moves to last match, be nice and put it back)
    call cursor(cursor_pos)

    " build list of ranges in the format [[range_start_line, range_end_line], ...]
    let l:prev_lnum=-1
    let l:curr_lnum=-1
    let l:sections=[]
    for lnum in l:section_start_lnums
        let l:prev_lnum=l:curr_lnum
        let l:curr_lnum=lnum

        if l:prev_lnum != -1
            call add(l:sections, [l:prev_lnum, l:curr_lnum-1])
        endif
    endfor

    " logically, final section extends to end of file
    call add(l:sections, [l:curr_lnum, line('$')])

    return l:sections
endfunction

" gradually trim line until only the line number remains
function! s:isolate_lnums(section_lines) abort
    if a:section_lines[0] =~ "Pattern not found"
        return []
    endif

    let l:lnums=[]
    for section_line in a:section_lines
        " trim trailing characters
        let l:lnum = substitute(section_line, '^\s\+\d\+\zs.*', '', '')

        " trim excess whitespace
        let l:lnum = substitute(l:lnum, '\s', '', 'g')

        call add(l:lnums, str2nr(l:lnum))
    endfor

    return l:lnums
endfunction

" define more signs, if needed
" note: doesn't abort on error
function! s:define_signs(sections)
    let l:biggest_diff=-1
    for section in a:sections
        let l:curr_diff=section[1]-section[0]
        let l:biggest_diff=l:curr_diff > l:biggest_diff ? l:curr_diff : l:biggest_diff
    endfor

    for i in range(l:biggest_diff+1)
        " fail silently when lnum is >= 100 in Vim
        " In NeoVim use :h 'signcolumn' and :h 'number' accordingly
        exec 'silent! sign define Lnum' . i . ' texthl=LineNr text=' . i
    endfor
endfunction

" place signs in buffer
" note: doesn't abort on error
function! s:place_signs(sections)
    " clear all of our signs for current buffer
    exec 'sign unplace * group=CustomNumber buffer=' . bufnr()

    " no matches
    if len(a:sections) == 0
        return
    endif

    " define signs, if needed
    call s:define_signs(a:sections)

    for section in a:sections
        let l:custom_lnum=0
        for lnum in range(section[0], section[1])
            exec 'silent! sign place ' . s:sign_id . ' line=' . lnum . ' name=Lnum' . l:custom_lnum . ' group=CustomNumber buffer=' . bufnr()
            let l:custom_lnum+=1
            let s:sign_id+=1
        endfor
    endfor
endfunction

augroup CustomNumber
    autocmd!
    autocmd BufEnter,BufNew,TextChanged,TextChangedI,TextChangedP * call s:place_signs(s:get_section_lines())
augroup END

And here is what that looks like in Vim:

example picture

See:

:h 'signcolumn'
:h :sign
:h :sign-define
:h :sign-place
:h :sign-unplace
14
  • added an edit to my example, do you think it is still impossible? (in vimscript) Commented Sep 8, 2020 at 2:02
  • 1
    You could technically use some of the things from :h sign-commands and/or :h sign-function-details, but in my opinion a sign column-based numbering system would be clunky and awkward. In addition to the mess that would be managing sign column numbering, you might also have to implement a system similar to :h indentexpr, in order to calculate how each line should be numbered. Again, doable, but not simple. Commented Sep 8, 2020 at 2:41
  • would love an example given it's doable :) (if you want i mean) Commented Sep 8, 2020 at 2:48
  • and i'm well aware on the "clunky and awkward" part, but that's only because i didn't specify in more detail the use for it, since it'll be offtopic (and the example succeed in illustrating the goal) Commented Sep 8, 2020 at 2:51
  • 1
    @NordineLotfi I've updated my answer with a first pass solution Commented Sep 8, 2020 at 18:01

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.