home ~ projects ~ socials

Get The Number Of Lines In A Neovim Buffer/File With Lua

As far as I can tell the way to get the number of lines in a file is to grab them with nvim_buf_get_lines and count them. Not sure if that starts breaking with large enough files.

local count_lines = function(buffer_number) 
    local lines = vim.api.nvim_buf_get_lines(
        buffer_number, 0, -1, true
    )
    local count = 0
    for _ in pairs(lines) do 
        count = count + 1 
    end
    return count
end

TODO

    Show usage

    write up the arguments

-- end of line --