So I am trying to create a Lua function to move the visual selection elsewhere. For question simplicity, lets assume 1 line above the current visual selection position.
This question is not about moving the text itself, that's trivial. It is about moving the selection cursors back to the now moved text.
First, something like
vim.api.nvim_input('koko') doesn't work because doesn't support arbitrary positions. It also doesn't work because it seems there is a race condition and the move hasn't occurred yet and it just destroys the selection.
local pos1 = vim.fn.getpos('v') local pos2 = vim.fn.getpos('.') pos1 = pos1 - 1 pos2 = pos2 - 1 vim.fn.setpos('.', pos2) vim.fn.setpos('v', pos1)
doesn't work because it says "illegal argument" in the error prompt. Apparently, moving
v isn't supported. Using
'> doesn't work because it's not the same thing.
This also doesn't work and I have no idea why:
vim.api.nvim_win_set_cursor(0, pos2) vim.api.nvim_input("o") vim.api.nvim_win_set_cursor(0, pos1)
So, how can I set the visual selection using the API in a way that's not horrible and also reliable?