Result from CocInfo
## versions
vim version: NVIM v0.3.1
node version: v11.2.0
coc.nvim version: 0.0.37
term: konsole-256color
platform: linux
## Error messages
Describe the bug
When the popupmenu is visible if the user has not highlighted any suggested completion then pressing Enter should insert a line break. But when using coc.nvim with no custom keybinding for <CR> pressing Enter makes the popupmenu disappear, but does not insert a line break.
The documentation for popupmenu-completions explains about the three possible states for the popupmenu:
There are three states:
- A complete match has been inserted, e.g., after using CTRL-N or CTRL-P.
- A cursor key has been used to select another match. The match was not
inserted then, only the entry in the popup menu is highlighted.- Only part of a match has been inserted and characters were typed or the
backspace key was used. The list of matches was then adjusted for what is
in front of the cursor.
It also explains the behavior of Enter when the popupmenu is open:
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.
The behavior that I observe seems to match what is described by state 2. The editor attempts to insert the selected match, but since there is no match selected it simply closes the popupmenu. In either of the other states pressing Enter would insert a line break.
My understanding is that the popupmenu should not enter state 2 unless the user takes action to select a match from the popupmenu. If I have typed some text but have not selected a completion then I expect the popupmenu to be in state 3. It seems that coc.nvim puts the popupmenu into state 2 without that explicit user interaction.
To Reproduce
<CR> with :iunmap <CR>Expected Results
The popupmenu should disappear, a line break should be inserted, the cursor should be positioned on the new line, any text that was typed immediately before pressing Enter should be preserved.
Actual Results
The popupmenu disappears, and entered text is preserved, but no line break is inserted. It is necessary to press Enter twice to insert a line break in this situation.
Coc doesn't add <CR> mapping for you, check out where it's added by verbose imap <CR>
It's expected behavior with complete option of noinsert,noselect,menuone, which required for increment filter.
You have to use other plugin if you prefer the behavior of menu,preview.
@hallettj, setting this inside your config file will give you the expected behavior of enter that you're asking for.
inoremap <silent><expr> <CR> pumvisible() ? "\<C-y><CR>" : "\<CR>"
It's not entirely obvious, but this page in the wiki gets you almost all of the way there. It's only missing the extra CR in the ternary statement.
@jared-w won't that send enter after item selection though?
@jared-w yep, that tip doesn't work. Also check README for updated mapping
@chemzqm If the noinsert,noselect behavior is necessary then I can work around that. Thank you for the explanation!
@oblitum, @jared-w Thank you for the follow-up advice! With the mapping in the wiki does not change the issue of pressing enter twice. Of course that mapping is helpful for triggering actions on Enter.
@jared-w Adding <CR> after <C-y> seems to prevent snippet expansion from working using enter, and does not seem to trigger other actions such as automatically adding import lines either. But it does resolve the double Enter issue, and I could type <C-y> instead of Enter to confirm completions. So that is a viable workaround.
@jared-w Adding
<CR>after<C-y>seems to prevent snippet expansion from working using enter, and does not seem to trigger other actions such as automatically adding import lines either. But it does resolve the double Enter issue, and I could type<C-y>instead of Enter to confirm completions. So that is a viable workaround.
@hallettj you can simply use <c-y> then, it's a default vim mapping for selection, no need to create additional one.
I mean, if you simply remove the mapping with pumvisible and just use default vim behavior of <c-y>, it will still work. Check :h complete_CTRL-Y.
You can reproduce it by: vim -u NORC, then:
:set completeopt=noinsert,noselect,menuone
Type characters foo f.
type <C-n> at the end and then type o and <enter>.
What you got is pupup menu become hidden without new line.
It should be third state, so the documentation:
third state: Use the text as it is and insert a line break.
is wrong.
Hi,
I also have this issue, it has been closed, but I don't really understand what is the conclusion here, or a correct workaround?
The suggestion by jared-w ( https://github.com/neoclide/coc.nvim/issues/262#issuecomment-447140117 ) seems to work for me, when I don't have anything selected in the popup menu, it closes the menu and inserts a <cr>, which is the behaviour I would like. However oblitum seems to indicate there is something wrong with this mapping, but I don't understand what exactly?
I don't have any problems with ultisnips snippets completion or execution/expansion (I do use <C-j> to expand snippets, so there is no clash (if that was the problem maybe?)).
but I don't understand what exactly?
That keymap should just works fine.
You can use call coc#rpc#request('hasSelected', []) to check if there's any complete item selected.
Also use call coc#_select_confirm() to make vim select first complete item if no item selected and do comfirm completion.
Most helpful comment
That keymap should just works fine.
You can use
call coc#rpc#request('hasSelected', [])to check if there's any complete item selected.Also use
call coc#_select_confirm()to make vim select first complete item if no item selected and do comfirm completion.