home ~ socials ~ projects ~ rss

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

April 2025

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
Share link:
https://www.alanwsmith.com/en/2w/1f/0t/rp/?get-the-number-of-lines-in-a-neovim-buffer-file-with-lua