I have this line in .vimrc file

set ruler

which shows the percentage of current line. But for very large text files, it would just show 0% for first couple hundreds of lines, which is not quite useful. How can I set ruler such that is would show, for example, 0.04%?

Thanks in advance!

  • 2
    You'd probably need to put your own code in the statusline to do it. For reference, this is the "default" with 'ruler' set: :set statusline=%<%f\ %h%m%r%=%-14.(%l,%c%V%)\ %P – D. Ben Knoble Nov 29 '19 at 16:33

One possible solution is to do the calculation by yourself and add them in your status line:

First you will need a function which returns as a string your current position as a decimal percentage:

function! GetCurrentPosition()
    return printf('%.2f%%', ( 100.0 * line('.') / line('$') ))

The function uses line('.') to get the position of the cursor and line('$') to get the total number of lines in the buffer. If also uses printf() to convert the result to a string, keeping only 2 decimal numbers and using %% to add a literal percent sign.

Then you can add the result of this function to your statusline like this:

set statusline+=%{GetCurrentPosition()}

And you should get your desired result:

enter image description here

| improve this answer | |

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.