As title.

  • My target is to disable a plugin before it is applied to a buffer that will be loaded with the content of a file.
  • I can get the file name before it is loaded.
  • The file to be opened is assumed to be very large. (>1MB)
  • What's the recommended way to do this? Is there any API for this (considering the performance) or should I use some terminal program to get it?

If any info. is missing please let me know.

  • What does "loaded" means to you?
    – romainl
    Jan 30, 2023 at 10:50
  • @romainl: I meant before the content of a file is loaded into a buffer. I probably misused the term. Just updated the title. Jan 30, 2023 at 10:55
  • 2
    I think you are looking for :help BufReadCmd in combination with :help file-functions.
    – romainl
    Jan 30, 2023 at 11:18
  • Judging from your question, you actually don't want it "before loaded"; you want it "before some specific autocommand". Then you should figure which one exactly.
    – Matt
    Jan 30, 2023 at 12:07

1 Answer 1


How to get the total lines of a file [...]

This can be done in many ways in Neovim and/or Lua (based on the tags you used):

  1. You can use the Lua API io.popen(str_term_cmd):read(...) to read the output of the command str_term_cmd.
  2. You can use the Neovim port of Vim's built-in API vim.fn.system(). I will prefer this way since:
    1. If {cmd} is a List it runs directly (no 'shell'), from the docs.
    2. Some extra checks are done by Bram: Result is a String, filtered to avoid platform-specific quirks, from the docs.
    3. This way is more "Vim". And you can avoid the need to :close() the file handle.

Remember to turn the output into a number. Here is an example usage:

tonumber(vim.fn.system({ 'wc', '-l', vim.fn.expand('%') }):match('%d+')) > 1000

[...] before it's loaded into a buffer?

If that plugin does provide a callback then you just do the calculation there. Otherwise, you should take a look at BufReadPre, FileReadPre, :help BufReadCmd, :help file-functions, as others have suggested.

  • very interesting. bookmarked.
    – gildux
    Jan 31, 2023 at 5:38

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.