0

In Neovim, the cursor shape automatically changes based on the mode you are in. I am writing a function in which I temporarily want to change what the cursor looks like in insert mode, then enter insert mode and do something, return to normal mode, and then revert to the defaults. I tried that with this function:

function! Cache()
    let oldcursor = &guicursor
    let &guicursor = "i:block"
    call feedkeys("a\<c-x>\<c-o>\<escape>")
    let &guicursor = oldcursor
endfunction

But this didn't work. It didn't return the &guicursor value to the oldcursor after running the line before it. I wanted it to first run call feedkeys("\<c-x>\<c-o>\<escape>"), and then run let &guicursor = oldcursor. But it seemed like it didn't wait for the feedkeys to finish before returning the originial value of guicursor.

romainl
  • 186,200
  • 21
  • 280
  • 313
Amarakon
  • 173
  • 5
  • It is not clear what you want to achieve based on the function. Why do you use the feedkeys part? I suppose this is a xy problem. – jdhao Aug 18 '22 at 02:14
  • I want to change the cursor shape (`let &guicursor = "i:block"`), do something in insert mode then exit insert mode (`call feedkeys("a\\\")`), and finally return the cursor to what it was before (`let &guicursor = oldcursor`). – Amarakon Aug 18 '22 at 04:19
  • Then wouldn't that require two functions? First, you save the original state of cursor, and set cursor shape to block, then you do whatever you want in insert mode. Finally, you restore the original cursor shape. It can not be achieved in single fuction. – jdhao Aug 18 '22 at 06:23
  • So how would I implement it in two functions? – Amarakon Aug 18 '22 at 08:40

0 Answers0