1

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?

  • :norm! diw should be enough. And what do you mean the function will be triggered in i mode? – klaus Mar 26 at 3:04
  • But using :normal! diw and then call complete, the position where I put the new word will be different if the cursor is at the end of line or not – NiaBie Mar 26 at 6:17
  • I don't have any experience with utilizing :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. – klaus Mar 26 at 6:46
4

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.

  • But in this way you can't delete the part of the word which is on the right side of the cursor. – NiaBie Mar 26 at 10:45
  • You can use cursor() to move your cursor to the end of the word. You can learn how to use the function with :h cursor – 嬲你屋里娘 Mar 26 at 10:57
2

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. – klaus Mar 26 at 6:47
  • you don't need to switch to normal mode, you can use c-o to switch temporarily to normal mode just for ciw, c-r also works in insert mode. – Christian Brabandt Mar 26 at 6:55
  • Good points. I found that i need another l operation to move the cursor after switch back to normal mode. – dedowsdi Mar 26 at 7:13
1

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.