Search code examples
neovim

Is there a Lua function for the `G` motion in Neovim?


Goal

I want to jump to line 55. I can do this with a normal motion in Vim. I can do this programmatically from Lua using vim.cmd. But is there a dedicated Lua function for this?

Vim normal motion

55G

Vim normal motion from Lua

local line = 55
vim.cmd('normal ' .. line .. 'G')

Dedicated Lua function

local line = 55
-- <your answer here>

Solution

  • Lua way of doing this is using vim.api.nvim_win_set_cursor call.

    local line = 55
    vim.api.nvim_win_set_cursor(0, {line, 0})
    

    (or)

    You can use :55 in command mode to switch to 55th line.

    So in lua..

    local line = 55
    vim.cmd(":"..line)