3

I'm using nvim v0.4.3 and am trying to apply some of the tricks + alpha (e.g. disabling substitution preview) to make editing large files faster.

I want to do that even to unnamed buffers (i.e. buffers without actual filenames), but I don't know how to get the size of the current buffer's content in vim script.

For named buffers, one can use getfsize() but it doesn't work for unnamed buffers.

g<C-g> displays the size in bytes, but I want to get the size info in vim script. It looks like getbufinfo() doesn't return the size info.

Is there any way to do it?

Thank you.

4
  • Welcome to Vi and Vim!
    – filbranden
    Jun 13, 2020 at 15:08
  • 1
    @filbranden Thanks for the hello and the perfect answer!
    – noname
    Jun 13, 2020 at 15:52
  • 2
    Happy to help! Btw I found about it under :help g_CTRL-G, so one great piece of advice with (Neo)Vim is to always start with the help system, which is really thorough!
    – filbranden
    Jun 13, 2020 at 15:53

1 Answer 1

5

The wordcount() function will return statistics for the current buffer, including the total number of bytes and total number of characters.

From :help wordcount():

The result is a dictionary of byte/chars/word statistics for the current buffer. This is the same info as provided by g_CTRL-G.

The return value includes:

  • bytes: Number of bytes in the buffer
  • chars: Number of chars in the buffer
  • etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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