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

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.

Related

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

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.

vim word complete, how to make the first one always as default

I am using several word complete plugins in vim (word_complete.vim, autocomplpop, omnicppcomplete-0.41). So far so good. It will pop up menus to let you choose which word to use, while I am typing the first characters
When I only type 2 characters, vim will set the first word in the popup menu as the default one, then you can directly press enter to use that word.
But the problem is usually 2 characters are not enough to narrow down the words to be complete. I need to type more. After my typing more than 2 characters, the default chosen word will disappear, then I have to use CTRL-N or CTRL-P to choose the word, although it is the first one in the popup menu.
Below is shows what I have:
The first is when I only type 2 characters
But after the third character is typed in, it appears as:
although "airline_detect_whitespace" is what i what to choose, I still need to type CTRL-N to choose it.
I am asking is there a way to configure the way vim chooses its default matcher?
for example, I want to type 5 characters before the default chosen word disappears.
Or is there a way to always make the first one in the popup menu to be chosen by default?
Thanks.
Have a look at the Make Vim completion popup menu work just like in an IDE page on the Vim Tips Wiki. It describes the setup to achieve this. Especially these mappings should create the behavior you want: Always have one menu entry pre-selected.
inoremap <expr> <C-n> pumvisible() ? '<C-n>' :
\ '<C-n><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'
inoremap <expr> <M-,> pumvisible() ? '<C-n>' :
\ '<C-x><C-o><C-n><C-p><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'
Those three plugins have overlapping features and certainly conflicting mappings for the pop-up menu and autocommands and stuff.
For example, AutoComplPop does everything wordcomplete does in a smarter and more automated way. OmniCPPComplete, has an obviously better C++ completion algorithm than the default one in Vim (and thus AutoComplPop's one) and can be set to not perform autocompletion.
I'd suggest you remove wordcomplete from your config, disable OmniCPPComplete's "may complete" feature and let AutoComplPop deal with the "autocompletion" side of the problem.

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.

enter button insert a new line instead of choosing an alternative

Recently when I tried to use the ctrl+n or ctrl+p to auto-complete, when there are multiple alternatives, tapping the enter button will insert a new line instead choose the alternative I want.
This did not happen before, maybe because I installed too many plugins and caused the conflicts. It would be horrible to check all these plugins' shot cuts and find out the source. So mapping the built-in auto-complete to some other keys could be a solution, but I don't know how to do that.
This is not a big problem but really made coding not "smooth". Anybody has met this situation before and how did you deal with it.
Short answer: You don't need to use <cr> to accept a match.
Snippet from the vim help :h popupmenu-keys
The behavior of the <Enter> key depends on the state you are in:
first state: Use the text as it is and insert a line break.
second state: Insert the currently selected match.
third state: Use the text as it is and insert a line break.
In other words: If you used the cursor keys to select another entry in the
list of matches then the <Enter> key inserts that match. If you typed
something else then <Enter> inserts a line break.
I would suggest you use <c-n> and <c-p> to switch to the correct mapping and then continue on with your typing. Typically this means I type a space or some other punctuation key and the menu closes. I never use <cr> to select a menu item. If however you really want to accept a match use <c-y>. Think "yes" to the selected menu item.
I just fixed a very similar problem caused by the vim-autoclose plugin by replacing it with the Auto-Pairs plugin. I doubt that yours is exactly the same culprit but it's actually not too bad hunting down the guilty party if you use pathogen for your plugins - just move half of your plugins from ~/.vim/bundle (or wherever they are) into a different folder, restart vim and test the autocompletion. If it works as expected, you know that one of the plugins you have moved out is responsible, so you can do the same again until you have narrowed it down.
Before that, you can also try running vim -u NONE which ignores your .vimrc file - it may be that you've snuck something in yourself and this is the fastest way to rule it out.
Good luck anyway. This took me about ten minutes and after probably a year of occasional annoyance I wish I had taken the time earlier.

How to i show omnicomplete's autocomplete list without autocompleting current text

I am learning vim with omnicomplete. I am wondering whether I can show the omnicomplete list without autocompleting current text. For example,
If I type "str.c" and then invoke omnicomplete, it will show a list containing possible words starting with "c", and autocomplete the current text to, say "capitalize".
If, for example, the method I am looking for is count, I have to scroll all the way down the list to find it, or delete the "apitalize" part, and then type o, etc. Is it possible to just show the list, so that if I invoke it after c, it will show the list without autocompleting to capitalize? For example to something like below, where I can still type o to quickly go to count
Thanks to #romainl comment, I looked up completeopt, and I found the answer Make Vim completion popup menu work just like in an IDE. It has a bunch of other related tips. To make the behaviour as described in the question,
:set completeopt=longest,menuone
You can also just stick with the basic vim functionality, and use C-e while in completion to have vim remove the list and go back to the point you initiated the completion.
Alternatively, you can press <C-P> right after <C-X><C-O> to go back to what you typed while keeping the completion menu open.
Check out :h ins-completion-menu for more information.
I think that the initial problem was the lack of noinsert in completeopt option.

Resources