I was trying to change my coc.nvim autocomplete key, and found this question in Stack Overflow, but the guy who answer this question doesn't explain really good how to customize it as you want, so I will explain it to help the NeoVim users that are racking the brain for this as I was.
Short Answer
If you want to bind Tab for autocompletion, paste this in your .vimrc or init.vim
inoremap <silent><expr> <tab> pumvisible() ? coc#_select_confirm() : "\<C-g>u\<TAB>"
inoremap <silent><expr> <cr> "\<c-g>u\<CR>"
Detailed answer
So, you have to make 2 insert mode remaps, in this case I will remap my completion to Tab key.
inoremap <silent><expr> <tab> pumvisible() ? coc#_select_confirm() : "\<C-g>u\<TAB>"
Obs: If you want to bind other key to autocomplete:
inoremap <silent><expr> [the key that you want to autocomplete] pumvisible() ? coc#_select_confirm() : "\<C-g>u\<TAB>"
Now, CoC will autocomplete with Tab key too, but Enter is also autocompleting, I want to bind Enter by just be Enter, not an autocompletion key.
In VimScript Enter is represented by <cr>
inoremap <silent><expr> <cr> "\<c-g>u\<cr>"
Obs:
inoremap <silent><expr> [this is the current autocompletion key] "\<c-g>u\[this is the bind that I am giving to the key]"
The example vim configuration gives some very useful tips for COC.
But specifically for you this is relevant:
" Use tab for trigger completion with characters ahead and navigate.
" NOTE: There's always complete item selected by default, you may want to enable
" no select by `"suggest.noselect": true` in your configuration file.
" NOTE: Use command ':verbose imap <tab>' to make sure tab is not mapped by
" other plugin before putting this into your config.
inoremap <silent><expr> <TAB>
\ coc#pum#visible() ? coc#pum#next(1) :
\ CheckBackspace() ? "\<Tab>" :
\ coc#refresh()
inoremap <expr><S-TAB> coc#pum#visible() ? coc#pum#prev(1) : "\<C-h>"
" Make <CR> to accept selected completion item or notify coc.nvim to format
" <C-g>u breaks current undo, please make your own choice.
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm()
\: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
function! CheckBackspace() abort
let col = col('.') - 1
return !col || getline('.')[col - 1] =~# '\s'
endfunction
Related
I can use K to show the man pages in Vim before I install the coc.nvim. And when I using the coc.nvim to do the same thing, instead of man pages, the documentation was in a hover are. But sometimes, the text cannot be displayed at one time, like this:
I have tried many ways to scroll the hover area, j, Ctrl+j, Shift+j, Ctrl+p, Shift+p, Tab, j..., but they didn't work.
So what can I do to scroll the documentation so that I can read the whole text
Check :h coc#float#has_scroll:
if has('nvim-0.4.0') || has('patch-8.2.0750')
nnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
nnoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
inoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(1)\<cr>" : "\<Right>"
inoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(0)\<cr>" : "\<Left>"
vnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
vnoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
endif
My vim version was too low, after updating it, the problem had been solved
In MacVim 8.0, ctrl-f brings up a search window instead of scrolling forward in a buffer by one full screen. ctrl-F (i.e. pressing the SHIFT key also) gives the same result.
Is there a troubleshooting step or a repair step I can take?
I don't know about the MacVim. But I had experienced same situation at window gvim.
I mean when I press "Ctrl+F" I don't want to see search window.
I found the below at "C:\Program Files (x86)\Vim\vim81\mswin.vim".
To resolve your problem, all you have to do is just comment the below line, that's it.
if has("gui")
" CTRL-F is the search dialog
noremap <expr> <C-F> has("gui_running") ? ":promptfind\<CR>" : "/"
inoremap <expr> <C-F> has("gui_running") ? "\<C-\>\<C-O>:promptfind\<CR>" : "\<C-\>\<C-O>/"
cnoremap <expr> <C-F> has("gui_running") ? "\<C-\>\<C-C>:promptfind\<CR>" : "\<C-\>\<C-O>/"
" CTRL-H is the replace dialog,
" but in console, it might be backspace, so don't map it there
nnoremap <expr> <C-H> has("gui_running") ? ":promptrepl\<CR>" : "\<C-H>"
inoremap <expr> <C-H> has("gui_running") ? "\<C-\>\<C-O>:promptrepl\<CR>" : "\<C-H>"
cnoremap <expr> <C-H> has("gui_running") ? "\<C-\>\<C-C>:promptrepl\<CR>" : "\<C-H>"
endif
I think what happened is that gvim 8.0 has a mapping for <Ctrl-f> in mswin.vim, but the previous version of gvim does not have that mapping in mswin.vim
I use both versions (on different computers), and so got surprised by the <Ctrl-f> mapping in the gvim 8.0 version.
My vimrc sources mswin.vim because I like the copy-paste functionality.
if you are using Vscode, change "<C-f>" from false to true.
I'm using the spf13 package for Vim. However, whenever I enter some text and press escape, some weird characters get appended to it.
For example, if I type this and press escape,
hello
I get this:
hellopumvisible() ? "\" : "\
There is a mapping on my .vimrc file that reads like this, which I think might be responsible:
" some convenient mappings
inoremap <expr> <Esc> pumvisible() ? "\<C-e>" : "\<Esc>"
inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<CR>"
inoremap <expr> <Down> pumvisible() ? "\<C-n>" : "\<Down>"
inoremap <expr> <Up> pumvisible() ? "\<C-p>" : "\<Up>"
inoremap <expr> <C-d> pumvisible() ? "\<PageDown>\<C-p>\<C-n>" : "\<C-d>"
inoremap <expr> <C-u> pumvisible() ? "\<PageUp>\<C-p>\<C-n>" : "\<C-u>"
I guess it's taken from http://vim.wikia.com/wiki/Improve_completion_popup_menu. But for some reason, this is giving me odd behavior. Any advice?
EDIT:
Here's the output of :inoremap <esc>
i <Esc> *#pumvisible() ? '<C-E>' : '<C-R>=<SNR>32_FlushBuffer()<CR>pumvisible() ? "\<C-E>" : "\<Esc>"'
i <Esc> * pumvisible() ? "\<C-E>" : "\<Esc>"
And I'm using Vim 7.3 on Windows 8.
Solution
The autoclose plugin is interfering with mapping. I've written the steps to remove it here: http://crossplatform.net/dev/vim-spf13-writes-random-characters-when-pressing-escape.html. It works fine now.
Probably, this maybe FAQ. And this will be helpful for others. So I copied answer from comment line above.
When you have a problem about vim mappings.
Check :verbose inoremap at the first.
If you know the keys which have problem, then do it with specified key, for example :verbose inoremap <esc>.
This fixes it for me,
in your ~/.vimrc or ~/.config/nvim/init.vim file add:
let g:AutoClosePreserveDotReg = 0
After searching a bit on the net it seems that I can't map CtrlSpace to anything/alot. Is there a way to do it today, what I found was usually 2 years old.
I've run into the same issue, the short answer is yes you can, and not only in the gui version. Adding this on you .vimrc is enough:
inoremap <C-Space> <C-x><C-o>
inoremap <C-#> <C-Space>
The problem seems to be that Terminal.app doesn't interpret <C-Space> correctly and Vim understands it as <C-#> which is a built-in mapping (:help CTRL-#).
Maybe you could go with something like the following in your .vimrc:
if !has("gui_running")
inoremap <C-#> <C-x><C-o>
endif
which seems to work, here, but I don't like the idea of overriding built-ins like that.
Instead you should try with <Leader> (:help leader), it gives you huge possibilities for defining your own custom mappings and (depending on the mapleader you choose) won't interfere with OS/app specific shortcuts/limitations and hence be more portable.
With this in my .vimrc:
let mapleader=","
inoremap <leader>, <C-x><C-o>
I just hit ,, to complete method names.
The nitpicker broke pablox solution. The crux of the solution was just about remapping. So when you disable remapping, it cannot work.
If you really want to throw in a noremap, this is what it looks like:
inoremap <expr><C-space> neocomplete#start_manual_complete()
imap <C-#> <C-Space>
What will not work: inoremap <C-#> <C-Space> 'cause the <C-Space> part will not be remapped itself.
Have you tried :inoremap <c-space> <c-x><c-o> ?
Does CtrlX CtrlO do anything when you type in insert mode? Is omnifunc set?
Add the following code to ~/.vimrc:
" Ctrl-Space for completions. Heck Yeah!
inoremap <expr> <C-Space> pumvisible() \|\| &omnifunc == '' ?
\ "\<lt>C-n>" :
\ "\<lt>C-x>\<lt>C-o><c-r>=pumvisible() ?" .
\ "\"\\<lt>c-n>\\<lt>c-p>\\<lt>c-n>\" :" .
\ "\" \\<lt>bs>\\<lt>C-n>\"\<CR>"
imap <C-#> <C-Space>
Source: https://coderwall.com/p/cl6cpq
To accommodate both Windows and Linux I applied this to ~/.vimrc
if has("unix")
inoremap <C-#> <c-x><c-o>
elseif has("win32")
inoremap <C-Space> <c-x><c-o>
endif
I had better results with this set of mappings across all modes on Mac OS. Have not tested Windows or Linux.
I don't understand how the excepted answer is supposed to work in terminal mode.
inoremap <C-space> <ESC>
vnoremap <C-space> <ESC>
cnoremap <C-space> <C-c>
" When in terminal, <C-Space> gets interpreted as <C-#>
imap <C-#> <C-space>
vmap <C-#> <C-space>
cmap <C-#> <C-space>
Like the others said, using inoremap with the correct key for your term (as discovered using i_Ctrl_v) should work. I will add to this another possible cause for problems with insert mode mappings: paste mode. As the docs state:
When the 'paste' option is switched on (also when it was already on):
- mapping in Insert mode and Command-line mode is disabled
This may seem irrelevant, but this very thing tripped me up trying to get a similar inoremap binding to work in Vim 8.2. I had set paste in my .vimrc, and had to chop it up with :finish statements (as [recommended in the vim faq) to isolate the line causing the problem.
Hi i installed the neocomplcache script and its great but i want to use a feature its called SuperTab, and the documentation on the site i think its wrong but i know that somebody with .vimrc experience can help me.
The site is this
In the script's site there are a bunch of lines you can add to your .vimrc to get features, there is a line that i think its badly formated, and thats why it doesnt work when i add like it is, the line is this:
" SuperTab like snippets behavior.
"imap <expr><TAB> neocomplcache#sources#snippets_complete#expandable() ? "\<Plug>neocomplcache_snippets_expand)" : pumvisible() ? "\<C-n>" : "\<TAB>"
I'll add the line with the paragraphs below and above it for context.
" Plugin key-mappings. imap
(neocomplcache_snippets_expand) smap
(neocomplcache_snippets_expand) inoremap
neocomplcache#undo_completion() inoremap
neocomplcache#complete_common_string()
" SuperTab like snippets behavior. "imap
neocomplcache#sources#snippets_complete#expandable() ?
"\(neocomplcache_snippets_expand)" : pumvisible() ? "\" :
"\"
" Recommended key-mappings. " : close popup and save indent.
inoremap neocomplcache#smart_close_popup() . "\" "
: completion. inoremap pumvisible() ? "\" :
"\" " , : close popup and delete backword char. inoremap
neocomplcache#smart_close_popup()."\" inoremap
neocomplcache#smart_close_popup()."\" inoremap
neocomplcache#close_popup() inoremap
neocomplcache#cancel_popup()
How am i supposed to add it to enable that feature?
Thanks.
I don't think I understand the problem, but I'd do this:
" SuperTab like snippets behavior.
imap <expr><TAB> neocomplcache#sources#snippets_complete#expandable() ? "\<Plug>(neocomplcache_snippets_expand)" : pumvisible() ? "\<C-n>" : "\<TAB>"