Calling omnicompletion for every keypress in vim

2019-02-18 18:18发布

问题:

I have a vim script that uses a one line window to get a filename pattern from the user. This pattern can be completed to a full filename from a database if you press CTRL-X CTRL-O. Now the only problem is that you have to press the auto completion shortcut by yourself. But I want the auto completion to work incrementally so that for every character you type it automatically gets updated (think about the CTRL-R file open dialog in Eclipse).

Is there a way to use an autocommand or some kind of callback to call the function behind CTRL-X CTRL-O for each character the user is typing in this particular window?

回答1:

Austin is on the right track, but just with the wrong event. Take a look at the CursorMovedI event of autocmd. Basically, it'll fire any time the keyboard cursor moves while in Insert mode. Type a character? Cursor moves, and the event is fired.

Keep in mind this is a bit heavy-handed for your use, because the cursor can move due to other things than typing or deleting characters. The user could use the arrow keys to move back where they want to edit. You'd be popping up the completion with every move.

I can't find anything in the help about window-local autocommands, but buffer-local exists, so that might be close enough.



回答2:

Try - and modify if necessary - this plugin: http://www.vim.org/scripts/script.php?script_id=1879

I'm a happy user.



回答3:

You should look at :h autocmd. I believe the InsertChange event could be used to do what you want.