Send Keystrokes To Neovim With nvim_feedkeys()

July 2021

This is how I'm sending keystrokes to Neovim in lua script and plugins.

local delete_key = vim.api.nvim_replace_termcodes('<BS>', true, false, true)
vim.api.nvim_feedkeys(delete_key, "i", true)

Here's some examples I pulled from a repo that's no longer around (TODO: clarify these)

-- Feed some keystrokes into the current buffer, replacing termcodes.

function helpers.feed(text, feed_opts)
  feed_opts = feed_opts or 'n'
  local to_feed = vim.api.nvim_replace_termcodes(text, true, false, true)
  api.nvim_feedkeys(to_feed, feed_opts, true)
end

And for insert:

-- Insert some text into the current buffer.
function helpers.insert(text)
  helpers.feed('i' .. text, 'x')
end
end of line