4

I was looking at some of the special characters for use in a status line, and I found one that shows the percentage through file of displayed window (%P). However, I can find no way of getting this value through a function or a command that is provided by default. I did see that this is the value displayed by the ruler option, but as far as I know, there is no way of capturing that value. How do you get this percentage?

Note

I know about the :file command, but that doesn't display the percentage I want. (you can capture this value with the :redir command)

4

If you're really determined to do it, it can be done.

A mostly accurate translation of the source function might look like this:

function! Stl_P()
    let above = line('w0') - 1
    let below = line('$') - line('w$')
    if below <= 0
        return above ? 'Bot' : 'All'
    elseif above <= 0
        return 'Top'
    else
        return printf('%2d%%', above > 1000000 ?
            \ above / ((above + below) / 100) :
            \ above * 100 / (above + below))
    endif
endfunction

This doesn't work with diff buffers where at least one of the files being diffed has removed lines, since there is no way to get the w_topfill in VimL (or perhaps I didn't ponder about it deep enough). Oh well.

  • Great! This works (almost) exactly the same way ruler and %P does. I thought that translating the source code could be a way of accomplishing this. – EvergreenTree Jul 8 '15 at 19:26
  • Very nice! But I still hope for more useful questions (in real life) that will get such good answers. – VanLaser Jul 8 '15 at 19:49
3

A simple way (line number accuracy):

function! LinePercent()
    return line('.') * 100 / line('$') . '%'
endfunction

Sample usage:

:echo LinePercent()

And with byte accuracy:

function! BytePercent()
    let crt_byte = line2byte(line('.')) + col('.') - 1
    let last_byte = line2byte(line('$')) + col(['$', '$']) - 1
    return crt_byte * 100 / last_byte . '%'
endfunction

echo BytePercent()

:h ruler says just that: "relative position in the file". It's true that the numbers displayed by the ruler are not entirely consistent with the above methods. It may be using line('w0'), line('w$'), or some other combination (e.g. the mean between firsts and last visible lines, or chars, or bytes, instead of the current cursor position) when computing the file position (it also shows 'Top' or 'Bottom' ...) . One can look in Vim source, if needed, but does one really need that?

  • Yes, this does return the line percentage, but I was looking for a function which shows the scroll percentage. (or whatever value ruler shows when it is set) – EvergreenTree Jul 8 '15 at 18:04
  • I can see the need to know a line, char or byte relative position in a file or buffer, but not the need to have a value of an unknown yet formula :) – VanLaser Jul 8 '15 at 18:29
  • I don't really have a particular use for it, I just thought it might be interesting to see what people answer and whether it is possible or not. :P – EvergreenTree Jul 8 '15 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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