How show wildmenu automagically, without the need to press <Tab>? - vim

In my Vimrc, I have this set:
set wildmenu
set wildmode=full
set wildignorecase
When you are typing like NERDTree, and press Tab you see the suggests results in your statusline. Looks awesome, right?
I would like to have a function, that every time you press a character in the command, the Tab is automatically pressed. So every time, you type a character, you see the suggested commands in the statusline. Like Emacs M-x.
I looked in help in the autocommands, but none of the events described the event (pressing characters in command line).
Anyone have a idea which event I mean?

There is no such event. You're probably thinking of InsertCharPre, but that's limited to insert mode. You would need to override every printable character in command-line mode via :cmap, but that could have other side effects, or interfere with some plugins.
Better think hard whether you really need this. The wild menu is modeled after shell completion, and that has to be explicitly triggered via <Tab>, too.

Perhaps you want wilder.nvim.

Related

Complete space-separated words in Vim

I often like to complete more just a Vim keyword. For example, I want to complete an arbitrary pathname or something like self.logger.debug("...") which I already have somewhere in my text file.
C-n and C-p use the 'iskeyword' option and thus only complete Vim keywords.
What is the best way to implement a space-separated word completion?
inoremap <C-m> ???
My only idea is to change 'iskeyword', use normal word completion, and reset 'iskeyword' it after that.
Both #Ingo Karkat and #Luc Hermitte provide excellent solutions. However if you want to do this natively then Vim provides some solutions which might help you. Typically completion uses plain <c-n>/<c-p> however there is an completion submode accessed via <c-x>.
Filename completion
Use <c-x><c-f> to start completing a filename. You can use <c-n>/<c-p> just like you normally would after you have started completion to move between options. If the completion ends in a directory (e.g. /usr/bin/) then just execute <c-x><c-f> to start completion into that directory.
Whole line completion
If you are commonly using the same line, but it isn't worth making a snippet or an abbreviation, then type the start of the line you wish then <c-x><c-l> to start line completion. Then just use <c-n>/<c-p> as you normally would.
Multi-word completion
You can use <c-x><c-n>/<c-x><c-p> to complete another word that follows the current word. This one is sort of tough to explain without just trying it.
Let's say you have the following text:
self.logger.debug("foo")
Let's say you would like another self.logger.debug somewhere else.
So type: sel then use <c-p> to as you normally would complete to self
Then use <c-x><c-p> to complete to self.logger (may need to do some <c-p>/<c-n> to get to .logger).
Once self.logger is completed then use <c-x><c-p> again for the .debugger part.
Note: this does use iskeyword so it may not complete exactly as you want, but should be pretty close.
For more help
:h ins-completion
:h compl-whole-line
:h compl-current
:h compl-filename
:h 'complete'
IMO, snippets are the best way to proceed in your case -- as you certainly don't want to change 'iskeyword' option (it'd trigger too many undesired side-effects, and as you said you'd need to restore it afterward, which is not trivial if possible at all). You could use abbreviations or mappings, but then you'd loose the "completion" feeling/feature you'd get with snippet plugins.
There exist plenty different snippet plugins. I'm quite sure there are plenty answers here on SO, or on vi.SE which describe the existing plugins.
For pathnames, you have i_CTRL-X_CTRL-f, but indeed it stops at each directory. In that case you could may be override i_CTRL-X_CTRL-f to alter &isk (and key sequences that valid/abort completion), trigger the completion, and then restore &isk and the mappings when you validate/abort the completion. This restoration at the end of completion is what some snippet plugins do. That's what I do in the core functions used in mu-template to take care of the completion. (Explanations of how this works on vi.SE)
I have written a plugin that is powered by my CompleteHelper plugin that does just that:
The WORDComplete plugin finds matches for WORDs that start with the non-blank characters in front of the cursor and end at the next whitespace. By default, it is triggered in insert mode with <C-x><C-w>. Like the built-in completions, the source buffers it considers can be configured.

vim omnifunc popup on keypress [duplicate]

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?
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.
Try - and modify if necessary - this plugin: http://www.vim.org/scripts/script.php?script_id=1879
I'm a happy user.
You should look at :h autocmd. I believe the InsertChange event could be used to do what you want.

how to autocomplete options in vim command mode

my setting for command mode completion is:
set wildmenu
set wildmode=longest,list,full
currently when i type
:set fdm=
in command mode, then press tab, manual appended, if i Press tab again , character ^I appended, what i want is manual changed to another foldmethod options such as syntax, indent and so on.
does anyone know is that possible or if there is any plugin could do that ?
thanks !
As you say, when you press <Tab> after :set fdm=, you get manualinserted.
That could seem the usual autocomplete behaviour we are used to in many places, manual being just the first of all possible values. So, you expect that repeating <Tab> will give you more possibilites.
But that's not indeed the case. What you get when pressing <Tab> in such a situation is not the first autocompletion alternative, but the current option value. So, you're getting manual because that's in fact the default value for that option. Successive <Tab>s get inserted literally, as this behaviour is only fired right after =.
From Vim's help:
The old value of an option can be obtained by hitting 'wildchar' just after
the '='. For example, typing 'wildchar' after ":set dir=" will insert the
current value of 'dir'. This overrules file name completion for the options
that take a file name.
So, what you described is expected behaviour. See :help cmdline-completion for the whole story.
I don't know about any plugin capable of changing this to what you want.

Calling omnicompletion for every keypress in vim

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?
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.
Try - and modify if necessary - this plugin: http://www.vim.org/scripts/script.php?script_id=1879
I'm a happy user.
You should look at :h autocmd. I believe the InsertChange event could be used to do what you want.

Is there a way to change the behavior of the vim omnicomplete menu?

Omnicompletion is working, but it automatically inserts the first result.
What I'd like to do is open the omnicomplete menu, then be able to type to narrow down the results, then hit enter or tab or space or something to insert the selected menu item.
Is this possible?
The command you are looking for is:
:set completeopt+=longest
It will insert the longest common prefix of all the suggestions, then you can type and delete to narrow down or expand results.
set wildmenu
set wildmode=list:longest,full
Found here.
There is also a great plugin for all of your completion needs called SuperTab continued.
This plugin might do what you are after: autocomplpop
Or you can try and make Vim completion popup menu work just like in an IDE.
This is the general Vim completion behaviour. For a complete overview, you can do
:he compl-current
But for your specific case (which you require the completion to be in state 2 or 3 (described in the document above). You can simply use Backspace, or Control-H to jump from state one to state two. In state 2 you can narrow the search by typing regular characters. So to complete completion with narrowing:
compl<C-X><C-P><BS>letion
It is totally backwards, I know, but that's how it works.
Edit: You can use the Down arrow key too isntead of Control-H or Backspace, and it has the benefit of not deleting a character.

Resources