home ~ projects ~ socials

Move The Cursor To The End Of A Neovim Buffer/File With Lua

local move_cursor_to_end_of_buffer = function()
    local source_lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
    local line = 0
    for _ in pairs(source_lines) do 
        line = line + 1 
    end
    local character = string.len(
        source_lines[line]
    )
    vim.api.nvim_win_set_cursor(0, {line, character})
end

Notes

  • This is for the current buffer which is assumed to be in the current window

Introduction

This is how I'm moving the cursor to the end of a file buffer in a Neovim window.

-- end of line --