0

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"
endfunction

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)
  vim.api.nvim_set_current_line(new_line)
end

What I really want is something like this:

function insert_date()
    vim.buffer.insert(os.date('%FT%T%z'))
end

Is there an easier way to insert text with Lua?

6
  • 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. Apr 27 at 15:10
  • @VivianDeSmedt what is Vim APS script? Apr 27 at 15:44
  • I mean API script using vim function like setbufline getcurpos ... Apr 27 at 16:22
  • Why? Just use VimScript. Apr 27 at 18:39

0

Your Answer

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