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?

3
  • :norm! diw should be enough. And what do you mean the function will be triggered in i mode?
    – 3N4N
    Mar 26, 2019 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, 2019 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.
    – 3N4N
    Mar 26, 2019 at 6:46

3 Answers 3

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.

2
  • 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, 2019 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, 2019 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>
3
  • Of course, <c-x> is a very important sub-mode in vim. So, not very wise to shadow it. You should mention that.
    – 3N4N
    Mar 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 for ciw, 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.
    – dedowsdi
    Mar 26, 2019 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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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