I'm writting a complete script, and when the function is triggered in i mode, I need to delete the word under the cursor and call the function complete
to put new words. I only know to delete the origin word by <backspace>
. Is there a good way to delete the word in i mode?
3 Answers
complete({startcol}, {matches})
provides the function to replace the old word. You can use complete(col(".") - ${length}, {matches})
to replace the old word, ${length}
is the length of your old word.
Although you can use c-w in i mode to delete until start of current word, it's much more convenient to switch to normal mode to change the entire word.
inoremap <leader>c <c-o>diw<c-r>=complete()<cr>
-
Of course, <c-x> is a very important sub-mode in vim. So, not very wise to shadow it. You should mention that.– 3N4NMar 26, 2019 at 6:47
-
you don't need to switch to normal mode, you can use
c-o
to switch temporarily to normal mode just forciw
,c-r
also works in insert mode. Mar 26, 2019 at 6:55 -
Good points. I found that i need another l operation to move the cursor after switch back to normal mode.– dedowsdiMar 26, 2019 at 7:13
If complete
conforms to the spec of 'completefunc'
(or can be made to, either by editing or wrapping it):
set completefunc=complete
Then just use <C-x>u
in insert mode (see :help ins-completion
).
:norm! diw
should be enough. And what do you mean the function will be triggered in i mode?:normal! diw
and then callcomplete
, the position where I put the new word will be different if the cursor is at the end of line or not:h complete()
, sorry. But I don't think some corner cases would be hard to solve. Maybe if you have some test examples, it would be easier to try.