This handy dandy function inserts the current date and time on the current line after the cursor:

function! InsertDate()
    execute "normal \"=strftime('%FT%T%z')\<CR>p"

I think Neovim's Lua API is meant to simplify this kind of junk, but the only way I've found to do this in Lua is quite verbose:

function insert_date()
  local pos = vim.api.nvim_win_get_cursor(0)[2]
  local line = vim.api.nvim_get_current_line()
  local new_line = line:sub(0, pos) .. os.date('%FT%T%z') .. line:sub(pos + 1)

What I really want is something like this:

function insert_date()

Is there an easier way to insert text with Lua?

  • Related: vi.stackexchange.com/questions/42019/… Commented Apr 27, 2023 at 14:55
  • To be fair you compare here a Vim command (normal) with Vim APS script. If you would have to implement the functionality using the VimScript API it would have been quite similar to what you wrote in lua. To my understanding the benefit to have lua is not to simplify but to use a widespread language (lua) and benefit from its momentum instead of using a language that is only developed for Vim (VimScript). What you can expect from using lua is a better or more accessible documentation, more language functionalities, more libraries, better performance. Commented Apr 27, 2023 at 15:10
  • @VivianDeSmedt what is Vim APS script? Commented Apr 27, 2023 at 15:44
  • I mean API script using vim function like setbufline getcurpos ... Commented Apr 27, 2023 at 16:22
  • Why? Just use VimScript. Commented Apr 27, 2023 at 18:39


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.