When I hit Enter on SuperTab's popup suggestion, it automatically creates a new line at the end of the inserted keyword.
Is it possible to select an option in the SuperTab popup without creating a new line?
More specifically, is there a way for me to configure space as the method of selecting the code completion suggestion?
inoremap <expr> <Space> pumvisible() ? "\<C-y>" : " "
Note the <C-y> that accepts currently selected option, you may want to use it directly instead of remapping space. You will see more in :h popupmenu-keys.
Put this in your .vimrc
let g:SuperTabCrMapping=1
Related
I'm Neovim user
and I use Plugin neoclide/coc.vim for autocompletion
but when my cursor focus out, I can not get suggestion box again
(How can I call this box? suggest box or hint box? I want to know this box name)
I want to trigger it and see again, Is there any command or variable for this?
Yes, the answer is on the front page of CoC's github project. You need to map something to coc#refresh(). For instance, to use Ctrl + Space to trigger completion, you need to put the following in your vimrc:
inoremap <silent><expr> <c-space> coc#refresh()
There are more examples on the project's page.
TLDR: try this Ctrl+#
I found this in README of neoclide/coc.nvim:
" Use <c-space> to trigger completion.
if has('nvim')
inoremap <silent><expr> <c-space> coc#refresh()
else
inoremap <silent><expr> <c-#> coc#refresh()
endif
The weirdness here is that, Ctrl+Space is not working, despite using neovim NOT vim.
But there is a good trick to get your desired key binding as it appears from your terminal emulator point of view:
Enter insert mode
Press Ctrl+v
Then press your keys you want to bind, Ctrl+Space
Ops! nothing is inserted in my case. It seems that my terminal doesn't recognize using Ctrl and Space or understands this keybinding differently and intercepts it. So this is the real problem!
Things I tried to solve the problem:
I changed the default coc config a little bit:
inoremap <silent><expr> <c-space> coc#refresh()
inoremap <silent><expr> <c-#> coc#refresh()
I tried bash as I was using zsh as a shell, but the problem is still there.
Then I tried another terminal emulator and the problem is solved, alhamdulillah.
I've commented in https://github.com/neoclide/coc.nvim/issues/2299#issuecomment-686293001
The suggestion box in your image is signatureHelp. If you want to reopen it, you need to trigger triggerCharacters in your function, usually is ( and ,. The triggerCharacters is defined by LS.
To Browse from suggestion box:
- <Tab >
- <Ctrl - x> <Ctrl -f>
- <Ctrl - p ><Ctrl -n>
- <Ctrl - n ><Ctrl -n>
To Confirm selection
- <Ctrl - y>
I'm using a plugin that performs multiple *noremap operations in its initialization. After I added the following mapping to YCM/UltiSnips:
inoremap <expr> <CR> pumvisible() ? "<C-R>=<SID>ExpandSnippetOrReturn()<CR>" : "\<CR>"
I've broken the other plugin's ability to see the <Enter> key, or rather I overrode/destroyed the original inoremap. I want to reenable that functionality, but without disabling ability to pick snippets from the auto-complete menu using Enter key. This keymap is effectively saying "if the menu is visible, select the option, otherwise simulate <Enter> key". What I need it to say instead is "if the menu is visible, select the option, otherwise perform whatever command <Enter> key is already mapped to". So basically, at the time of declaration of this inoremap, I need to expand existing inoremap for this key into the else.
I've looked at <C-R>=feedkeys('<CR>')<CR> and <C-R>=mapcheck('\<CR>', 'i')<CR> but couldn't get either to work. Can someone help me out?
Finally figured it out... this was way more vim troubleshooting than I needed for one day.
function! LoadPumvisibleEnter()
let smartmap = maparg("<Enter>", "i")
execute printf('inoremap <script> <expr> <CR> pumvisible() ? "<C-R>=<SID>ExpandSnippetOrReturn()<CR>" : %s', smartmap)
endfunction
au VimEnter * :execute LoadPumvisibleEnter()
throwing it in a function allowed me to stay sane with escapes and which mode I was in
autocommand is needed because vim apparently runs vimrc BEFORE loading any plugins (Does Vim load plugins after loading vimrc?), so without it the map will not yet be set
So in insert mode if you hit ctrl-x s on a misspelled word you get a nicely formatted popup menu of spelling suggestions. This is awesome.
The comparable command in normal mode (z=), however, gives a bland plain-text list that eats the whole screen.
I've partially solved this by adding the following keybinding in my .vimrc:
nnoremap <Leader>s ea<C-X><C-S>
This works perfectly and hitting s in normal mode gives me the same drop down... The only problem is I'm now left in insert mode at the end of everything. Is there some way to get the drop down style selection and end up in normal mode after the replace is all said and done?
You can't do it directly. The popup menu you're thinking of is specifically referred to as "insert mode completion".
However, you're halfway there, by mapping a key that enters insert mode and starts the completion. Now all you need is to map a key that selects the entry (like the 'enter' key) to also exit insert mode.
You should test the return value of pumvisible() in your mapping, to prevent it firing when you don't want. Example (from comments):
inoremap <expr> <CR> pumvisible() ? "\<C-y><Esc>" : "\<CR>"
Perhaps you could also set a variable or something, or use a key you won't use to end actual insert mode completion.
I have these insert mode mappings in my .vimrc file:
imap <C-e> <C-o>A
imap <C-a> <C-o>I
They make Ctrl-A and Ctrl-E move the cursor to the start and end of the line without leaving insert mode, a la emacs keybindings.
However, I just realized that the Ctrl-E mapping introduces a conflict with the autocompletion submode. The documentation in :help complete_CTRL-E states:
When completion is active, you can use CTRL-E to stop it and go back to the originally typed text.
Thus, my Ctrl-E mapping interferes with this. Is there a way that I can make Ctrl-E jump to the end of the line only if auto-completion is not active?
There is no proper way to test whether the
Ctrl+X-completion mode is active or not.
However, the following two workarounds are possible.
1. If one uses the popup menu to choose from the list of available
completions (especially in the case of menuone set in the completeopt
option), an acceptable solution might be the mapping
inoremap <expr> <c-e> pumvisible() ? "\<c-e>" : "\<c-o>A"
2. A general solution can be based on a side effect: In the
completion submode, it is disallowed to enter Insert mode recursively
(see :helpgrep Note: While completion), so if an attempt to do so
fails, we can suppose that we are in the midst of a completion:
inoremap <c-e> <c-r>=InsCtrlE()<cr>
function! InsCtrlE()
try
norm! i
return "\<c-o>A"
catch
return "\<c-e>"
endtry
endfunction
I want Vim's completion menu to work more like the one in Visual Studio. I want to be able to write the start of a variable (or something which completion is available), hit the omni-completion keys and get the completion menu. Now I want the completion menu to highlight the best match but not add it to the buffer. Now I want to be able to either hit ENTER to input the selected element or enter more letters to filter the completion menu even more (possibly highlighting a new item in the completion menu) and then hit enter to insert.
The wiki page/tips at http://vim.wikia.com/wiki/Make_Vim_completion_popup_menu_work_just_like_in_an_IDE is supposed to do this but I can't get it to work. Has someone else tried the code from that tip and made it work as I describe?
The referenced wiki page contained broken mappings. A user (Spiiph) changed the Vim code to something that looked better to him, but was not right. I reversed the changes he made and now the inoremap statements should work.
To avoid confusion these are IMHO the right commands:
inoremap <expr> <c-n> pumvisible() ? "\<lt>c-n>" : "\<lt>c-n>\<lt>c-r>=pumvisible() ? \"\\<lt>down>\" : \"\"\<lt>cr>"
inoremap <expr> <m-;> pumvisible() ? "\<lt>c-n>" : "\<lt>c-x>\<lt>c-o>\<lt>c-n>\<lt>c-p>\<lt>c-r>=pumvisible() ? \"\\<lt>down>\" : \"\"\<lt>cr>"
PS: When it comes to using Vim's completion feature I found the SuperTab script
very convenient. Maybe this script is also interesting for you in this context...
I use supertab, which I really like, and the following seems to work great:
imap <expr> <Tab> pumvisible() ? "<Tab>" : "<Tab><Down>"
For reference: I'm running debian sid, and this works both for vim 7.2.245 and for my patched 7.2.259.