Wikia

Vim Tips Wiki

Changes: Get cursor position as byte percentage instead of line percentage

Edit

Back to page

(Remove html character entities)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 4: Line 4:
 
|previous=267
 
|previous=267
 
|next=269
 
|next=269
|created=June 27, 2002
+
|created=2002
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Larry Clapp
 
|author=Larry Clapp
Line 37: Line 37:
 
Here is a variation on the same theme.
 
Here is a variation on the same theme.
   
Add this to your vimrc, then type <tt>:HowFar</tt> or <tt>:HF</tt> to display "Byte aaaa of bbbbb (xx%)" on the bottom line. Note: The command! statement is one long line.
+
Add this to your vimrc, then type <code>:HowFar</code> or <code>:HF</code> to display "Byte aaaa of bbbbb (xx%)" on the bottom line. Note: The command! statement is one long line.
   
 
<pre>
 
<pre>

Latest revision as of 05:22, July 13, 2012

Tip 268 Printable Monobook Previous Next

created 2002 · complexity intermediate · author Larry Clapp · version 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()}%%

ReferencesEdit

CommentsEdit

Here is a variation on the same theme.

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.

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

Around Wikia's network

Random Wiki