Mapping Tab to Omicompletion in GVim - vim

Omnicompletion works pressing <C-X><C-O> making a dropdown list appear.
I wanted to map it as <S-Tab> <C-X><C-O> but first I wanted to test it as: <C-F5> <C-X><C-O>.
It does complete the word but the dropdown list doesn't show up. (the same happens with the plugin SuperTab).
Any suggestions?
EDIT: it Works like this inoremap <S-F5> <C-X><C-O> but it doesn't work like this inoremap <Tab> <C-X><C-O> or <TAB> <C-X><C-O>
CODE:
" autocomplete using tab
imap <C-F5> <C-X><C-O>

One day, I found this (probably on vim.org):
fu! InsertTabWrapper(direction)
let char_before = col('.') - 1
if !char_before || getline('.')[char_before - 1] !~ '\k'
return "\<tab>"
elseif "backward" == a:direction
return "\<c-p>"
else
return "\<c-n>"
endif
endfu
inoremap <tab> <c-r>=InsertTabWrapper("forward")<cr>
inoremap <s-tab> <c-r>=InsertTabWrapper("backward")<cr>
And it works like a charm.

Related

How to select first item in popup menu and close menu in a single keybind for autocomplete in nvim?

I'm using coc.nvim for autocomplete and when the popup menu appears, I'd like Tab to select the first item and close the menu. At the moment I've keybinded Tab to <C-n><CR>, but the <CR> actually puts in a line return which is not what I want.
inoremap <expr> <Tab> pumvisible() ? "\<C-n><Space>" : "<Tab>"
I found the answer the coc.nvim's example docs:
https://github.com/neoclide/coc.nvim/blob/e1a4ce4d95d1d89b6dd31019cc4387425aa09b86/doc/coc.txt#L892-L909
inoremap <silent><expr> <TAB>
\ pumvisible() ? coc#_select_confirm() :
\ coc#expandableOrJumpable() ?
\ "\<C-r>=coc#rpc#request('doKeymap', ['snippets-expand-jump',''])\<CR>" :
\ <SID>check_back_space() ? "\<TAB>" :
\ coc#refresh()
function! s:check_back_space() abort
let col = col('.') - 1
return !col || getline('.')[col - 1] =~# '\s'
endfunction
let g:coc_snippet_next = '<tab>'
Works like an absolute charm.
Try inoremap <expr> <Tab> pumvisible() ? coc#_select_confirm() : "<Tab>".
After struggling for so long and trying these things I realised it works out the box fine.
To select the first item in the dropdown list press: ctrl+y.
Press: ctrl+n select the next item in the drop down list.
Press: ctrl+p to select the previous item in the drop down list.
To use tab and shift tab as well to navigate next and previous items see the documentation.
To use tab to select first item in the drop down list below the above documentations config:
inoremap <expr> <TAB> pumvisible() ? "\<C-y>" : "\<C-g>u\<TAB>"

What vim plugin does leetcode use to auto-close parenthesis?

I like the way leetcode vim auto close braces.
It closes when I type " ' ( { [ which can be done with simple vim map
When I try to close one more tim it doesn't add another " ' ) } ] and skip. Most vim plugin works like this.
When I press { and enter, it automatically add new line for my code and indent for me.
To be specific
{<cursor>}
when I press enter, it becomes
{
cursor
}
I don't know which plugin works like leetcode vim.
Plus, what is some vanilla vim way to solve 2nd issue?
I tried to analyze by looking at plugins but it was too complicated.
These vanilla vim mapping will do what you are looking for, especially the last two is interesting.
inoremap " ""<left>
inoremap ' ''<left>
inoremap ( ()<left>
inoremap [ []<left>
inoremap { {}<left>
inoremap {<CR> {<CR>}<ESC>O
inoremap {;<CR> {<CR>};<ESC>O
This autocomplete in insert mode, provided set paste is not set. When we don't want the mapping, we need to escape it using ctrl + v before typing the mapped char like ( { etc.
#dlmeetei's answer would print ()) when I type (), and escape mapping would be burdensome.
So, I improved the answer above, by creating a simple function, so please correct and improve my idea.
"" check whether current charcter (on cursor) equals parameter
"" then decide whether to put new parenthesis
func! AutoClose(...)
let cur = getline(".")[col(".")]
if cur != a:1
if a:1 == "'" || a:1 == '"'
execute "normal!a".a:1.a:1
else
execute "normal!a".a:1
endif
execute "normal!h"
else
execute "normal!l"
endif
endfunc
inoremap ( ()<left>
inoremap [ []<left>
inoremap { {}<left>
inoremap ) <ESC>:call AutoClose(')') <CR>a
inoremap ] <ESC>:call AutoClose(']') <CR>a
inoremap } <ESC>:call AutoClose('}') <CR>a
inoremap " <ESC>:call AutoClose('"') <CR>a
inoremap ' <ESC>:call AutoClose("'") <CR>a
inoremap {<CR> {<CR>}<ESC>O
inoremap {;<CR> {<CR>};<ESC>O

omnicomplete in vim with shift-tab not working?

Im trying to get vim to allow me to circle through the autocomplete popup list with the tab key. It works fine for tab but not for s-tab (shift-tab).
It seems like shift-tab somehow canceles the autocomplete menu before applying C-P
Anyone got any ideas?
function InsertTabWrapper(direction)
if pumvisible()
if "forward" == a:direction
return "\<C-N>"
else
return "\<C-P>"
endif
endif
let col = col('.') - 1
if !col || getline('.')[col - 1] !~ '\k'
return "\<tab>"
else
return "\<c-x>\<c-o>"
endif
endfunction
inoremap <tab> <c-r>=InsertTabWrapper("forward")<cr>
inoremap <s-tab> <c-r>InsertTabWrapper("backward")<cr>
You missed the equal sign "=" after <c-r> for the <s-tab> mapping.
However, I would suggest doing it like this:
function! InsertTabWrapper()
if pumvisible()
return "\<c-n>"
endif
let col = col('.') - 1
if !col || getline('.')[col - 1] !~ '\k'
return "\<tab>"
else
return "\<c-x>\<c-o>"
endif
endfunction
inoremap <expr><tab> InsertTabWrapper()
inoremap <expr><s-tab> pumvisible()?"\<c-p>":"\<c-d>"
Use <expr> mapping. It's nicer to see and clearer (many people don't know about <c-r>= things.
Mapping <s-tab> like this and you can do unindent in insertmode.

Pressing tab in HTML files in vim goes to the next empty tag pair rather than expanding a completion

I'm not sure where the functionality of tabbing to the next empty tag pair in HTML files comes from. I would like to disable it completely.
The problem only comes with using SuperTab and SnipMate together, if I remove SuperTab tab goes back to expanding normally, if I remove SnipMate, tab goes back to showing completions.
In the past I've had both working fine and would like to do so again.
I'm using:
http://github.com/msanders/snipmate.vim
http://github.com/scrooloose/snipmate-snippets
http://github.com/ervandew/supertab
With vim-update-bundles. Default configuration for both. Other options I have enabled are syntax on. autoindent. smartindent. expandtab. nocompatible. filetype indent plugin on.
SnipMate and SuperTab both use ⇥, that makes their combination very annoying and unpredictable.
After years of TextMate, ⇥ expansion was a habit I couldn't/didn't want to drop so I ditched SuperTab quickly and learned to love Vim's native Omni Completion:
inoremap <leader>, <C-x><C-o>
inoremap <leader>: <C-x><C-f>
inoremap <leader>= <C-x><C-l>
For a more streamlined experience, autocomplpop is surprisingly fast and smart.
The solution that fit my needs is neocomplcache.
It's a nice mix between autocomplpop, supertab and snipmate. Well, the snippet part is just a little buggy, but quite usable.
However I don't use ⇥ to expand, the popup omnicompletion comes up after the 3rd written character. You move with ⇥ thru the list, and expand the snippets (a la textmate), with CtrlK, but you can map that to you choice.
These are my .vimrc settings.
""""""""""""""""""""""""""""""
" => neocomplcache plugin
""""""""""""""""""""""""""""""
" TODO: Still need to tweak behavior with <TAB> to expand
" snippets, change throughout the autocompletion list
" Use neocomplcache.
let g:neocomplcache_enable_at_startup = 1
" Use smartcase.
let g:neocomplcache_enable_smart_case = 1
" Use camel case completion.
let g:neocomplcache_enable_camel_case_completion = 1
" Use underbar completion.
let g:neocomplcache_enable_underbar_completion = 1
" Set minimum syntax keyword length.
let g:neocomplcache_min_syntax_length = 3
let g:neocomplcache_lock_buffer_name_pattern = '\*ku\*'
let g:neocomplcache_snippets_dir = '~/.vim/snippet/'
" Define dictionary.
let g:neocomplcache_dictionary_filetype_lists = {
\ 'default' : '',
\ 'vimshell' : $HOME.'/.vimshell_hist',
\ 'scheme' : $HOME.'/.gosh_completions'
\ }
" Define keyword.
if !exists('g:neocomplcache_keyword_patterns')
let g:neocomplcache_keyword_patterns = {}
endif
let g:neocomplcache_keyword_patterns['default'] = '\h\w*'
" Plugin key-mappings.
imap <C-k> <Plug>(neocomplcache_snippets_expand)
smap <C-k> <Plug>(neocomplcache_snippets_expand)
inoremap <expr><C-g> neocomplcache#undo_completion()
inoremap <expr><C-l> neocomplcache#complete_common_string()
" SuperTab like snippets behavior.
imap <expr><TAB> neocomplcache#sources#snippets_complete#expandable() ? "\<Plug>(neocomplcache_snippets_expand)" : pumvisible() ? "\<C-n>" : "\<TAB>"
" Recommended key-mappings.
" <CR>: close popup and save indent.
" inoremap <expr><CR> neocomplcache#smart_close_popup() . "\<CR>"
" <TAB>: completion.
inoremap <expr><TAB> pumvisible() ? "\<C-n>" : "\<TAB>"
" <C-h>, <BS>: close popup and delete backword char.
inoremap <expr><C-h> neocomplcache#smart_close_popup()."\<C-h>"
inoremap <expr><BS> neocomplcache#smart_close_popup()."\<C-h>"
inoremap <expr><C-y> neocomplcache#close_popup()
inoremap <expr><C-e> neocomplcache#cancel_popup()
" AutoComplPop like behavior.
"let g:neocomplcache_enable_auto_select = 1
" Shell like behavior(not recommended).
"set completeopt+=longest
"let g:neocomplcache_enable_auto_select = 1
"let g:neocomplcache_disable_auto_complete = 1
"inoremap <expr><TAB> pumvisible() ? "\<Down>" : "\<TAB>"
"inoremap <expr><CR> neocomplcache#smart_close_popup() . "\<CR>"
" Enable omni completion.
autocmd FileType css setlocal omnifunc=csscomplete#CompleteCSS
autocmd FileType html,markdown setlocal omnifunc=htmlcomplete#CompleteTags
autocmd FileType javascript setlocal omnifunc=javascriptcomplete#CompleteJS
autocmd FileType python setlocal omnifunc=pythoncomplete#Complete
autocmd FileType xml setlocal omnifunc=xmlcomplete#CompleteTags
" Enable heavy omni completion.
if !exists('g:neocomplcache_omni_patterns')
let g:neocomplcache_omni_patterns = {}
endif
let g:neocomplcache_omni_patterns.ruby = '[^. *\t]\.\w*\|\h\w*::'
"autocmd FileType ruby setlocal omnifunc=rubycomplete#Complete
let g:neocomplcache_omni_patterns.php = '[^. \t]->\h\w*\|\h\w*::'
au BufNewFile,BufRead *.snip set syntax=snippet ft=snippet foldmethod=indent

how to map keys for popup menu in vim

After a completion try, omnicppcomplete will display all the possible items in the pop up menu . To select an certain item in the menu, one should use <C-N> and <C-p> to switch back and forth between different items. I feel that it is very inconvient . It should be very cool if j and k can be used to to take place of <C-N> and <C-P> . so how should I do ?
function! OmniPopup(action)
if pumvisible()
if a:action == 'j'
return "\<C-N>"
elseif a:action == 'k'
return "\<C-P>"
endif
endif
return a:action
endfunction
inoremap <silent>j <C-R>=OmniPopup('j')<CR>
inoremap <silent>k <C-R>=OmniPopup('k')<CR>
I prefer using the tab key for completion (I am not sure where I got this from):
"tab complete
function! InsertTabWrapper(direction)
let col = col('.') - 1
if !col || getline('.')[col - 1] !~ '\k'
return "\<tab>"
elseif "backward" == a:direction
return "\<c-p>"
else
return "\<c-n>"
endif
endfunction
inoremap <tab> <c-r>=InsertTabWrapper ("forward")<cr>
inoremap <s-tab> <c-r>=InsertTabWrapper ("backward")<cr>
CTRL+J and CTRL+K instead: (so you can type j and k)
inoremap <expr><C-J> pumvisible() ? "\<C-n>" : "\<C-J>"
inoremap <expr><C-K> pumvisible() ? "\<C-p>" : "\<C-K>"
Bonus: <ENTER> to select the option
inoremap <expr><Cr> pumvisible() ? "\<C-y>" : "\<Cr>"

Resources