M HYPE SPLASH
// general

Show current file size in vim editor

By John Campbell

I'm heavily using Vim to edit and work with my files, now I'm starting to open big files and it would be useful to see the file size directly from VIM itself.

Is there a way to show the current file size in vim ?

At the moment I'm doing :

:!ls -lah %

Is there an internal way to display the current file size ?

3 Answers

Hit g CTRL-g to see some statistics on the current file in the status line, including file size.

1

Yes, there is an internal way to display current file size.

A simple way is as below:

:echo getfsize(expand(@%))

or little more verbose, as below:

:echo 'Size of ' @% ' file is ' getfsize(expand(@%)) ' bytes'

Alternatively, you can put it in a function and assign a key binding (map) for handy access. Something like this: Put following code in your vimrc file:

function! GetFilesize(file) let size = getfsize(expand(a:file)) echo 'Size of ' a:file ' is ' size ' bytes'
endfunction
map <leader>s :call GetFilesize(@%)<CR>

And from within control mode, press \s (assuming <leader> is set to backslash).

0

Add to .vimrc to display the file size in human readable format (eg 11.8KB) in the status line:

set statusline+=%{FileSize(line2byte('$')+len(getline('$')))}
function! FileSize(bytes) let l:bytes = a:bytes | let l:sizes = ['B', 'KB', 'MB', 'GB'] | let l:i = 0 while l:bytes >= 1024 | let l:bytes = l:bytes / 1024.0 | let l:i += 1 | endwhile return l:bytes > 0 ? printf(' %.1f%s ', l:bytes, l:sizes[l:i]) : ''
endfunction

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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