Vim logo vim online Vim Book Ad

 Tip #268: Get cursor position as byte percentage instead of line percentage

 tip karma   Rating 5/9, Viewed by 402 

created:   June 27, 2002 7:16      complexity:   intermediate
author:   Larry Clapp      as of Vim:   5.7

On line 300 of a thousand line file, Vim will show you that you're 30% through the file.  But what if most of the lines have one character in them, and some of them have twenty thousand?  Sometimes it comes in handy to know your percentage through the file in terms of current-byte / total-bytes.  I looked through the Vim docs and couldn't find a way to do this, so I wrote a Vim function to show it.

Put this in your .vimrc:

function! Percent()
    let byte = line2byte( line( "." ) ) + col( "." ) - 1
    let size = (line2byte( line( "$" ) + 1 ) - 1)
    " return byte . " " . size . " " . (byte * 100) / size
    return (byte * 100) / size
endfunction

(Uncomment the first return to see intermediate values.)

And put this somewhere in your "set statusline=...":

    %{Percent()}%%

See "help statusline", "help eval".

 rate this tip  Life Changing Helpful Unfulfilling 

<<selectively displaying abbreviations | Syntax highlighting is "out of sync", seems to correct itself with refresh ?? >>

Additional Notes

[email protected], July 25, 2002 17:29
Here is a variation on the same theme. (Sorry Larry, I would have written to you first if I'd had your addy.)
Add this to your .vimrc, then type :HowFar or :HF to display "Byte aaaa of bbbbb (xx%)" on the bottom line.
Note: The command! statement is one long line.
-- Tony Mechelynck < [email protected] >


function! BytePercent()
let CurByte = line2byte (line ( "." ) ) + col ( "." ) - 1
let TotBytes = line2byte( line( "$" ) + 1) - 1
return ( CurByte * 100 ) / TotBytes
endfunction

command! -nargs=0 -bar HowFar echo "Byte " . ( line2byte( line( "." ) ) + col( "." ) - 1 ) . " of " . ( line2byte( line( "$" ) + 1 ) - 1 ) . " (" . BytePercent() . "%)"

cnoreabbrev HF HowFar

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo