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):
- You can use the Lua API
io.popen(str_term_cmd):read(...)
to read the output of the command str_term_cmd
.
- You can use the Neovim port of Vim's built-in API
vim.fn.system()
. I will prefer this way since:
If {cmd} is a List it runs directly (no 'shell')
, from the docs.
- Some extra checks are done by Bram:
Result is a String, filtered to avoid platform-specific quirks
, from the docs.
- 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.
:help BufReadCmd
in combination with:help file-functions
.