M
M
Maxim2015-12-27 21:57:54
vim
Maxim, 2015-12-27 21:57:54

How to make autocomplete and snippets work in vim?

I use YouCompleteMe for autocomplete and vim-snipmate for snippets . I would like to hang both plugins on Tab, so that there would be some kind of "smart" enumeration of options. If there is a snippet, then we write the snippet, if not, then we scroll through the autocomplete.
Tell me, is it possible to implement this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Masterov, 2016-01-04
Antonikhin @STJ

Such a combination can be obtained in the bundle neocomplete.vim + ultisnips .
neocomplete requires Vim built with Lua support (why Lua was chosen is explained here ). Building and compiling under Linux is quite common and not difficult, but for Windows you can find a ready-made build from the current build version ( 1 , 2 , 3 and Lua itself ). UltiSnips requires python support, and it seems to be only version 2.
When configuring neocomplete, you need to add ultisnips as a source:

let g:neocomplete#sources = {
  \ '_':          ['buffer', 'file/include'],
  \ 'javascript': ['omni', 'file/include', 'ultisnips', 'tag']
  \}

And make it so that the search in it starts from the first character, and for clarity, the found is always on top:
call neocomplete#custom#source('ultisnips', 'rank', 100)
call neocomplete#custom#source('ultisnips', 'min_pattern_length', 1)

If Tab is used for manual auto-completion, then I advise you to use a function that will allow you to save the usual work of Tab'a, taking into account the presence of lines and spaces:
inoremap <silent> <Tab> <C-r>=<SID>neoComplete("\<Tab>")<CR>
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<C-x>\<C-o>"

function! s:neoComplete(key)
  if pumvisible()
    return "\<C-n>"
  endif
  let [curPos, lineLength] = [getcurpos()[4], col('$')]
  let isText = curPos <= lineLength
  let isStartLine = curPos <= 1
  let isBackspace = getline('.')[curPos-2] =~ '\s'
  if isText && !isStartLine && !isBackspace
      return neocomplete#helper#get_force_omni_complete_pos(neocomplete#get_cur_text(1)) >= 0
          \ ? "\<C-x>\<C-o>\<C-r>=neocomplete#mappings#popup_post()\<CR>"
          \ : neocomplete#start_manual_complete()
  endif
  return a:key
endfunction

To expand UltiSnips snippets, I advise you to use an almost similar function: (example with tilde)
inoremap <silent> ` <C-r>=<SID>ultiComplete("\`")<CR>
xnoremap <silent> ` :<C-u>call UltiSnips#SaveLastVisualSelection()<CR>gvs
snoremap <C-c> <Esc>

function! s:ultiComplete(key)
  if len(UltiSnips#SnippetsInCurrentScope()) >= 1
    let [curPos, lineLength] = [getcurpos()[4], col('$')]
    let isBackspace = getline('.')[curPos-2] =~ '\s'
    let isStartLine = curPos <= 1
    let isText = curPos <= lineLength
    if isText && !isStartLine && !isBackspace
      return UltiSnips#ExpandSnippet()
    endif
  endif
  return a:key
endfunction

UltiSnips seems to have support for snippets in the SnipMate format , and besides that, it is indicated that the plugin works with YouCompleteMe. But I haven’t checked this for a long time, considering today the YouCompleteMe plugin as an unnecessary heap, and SnipMate is completely dead, the authors of which once could not agree on who should fork whom.
I will add an answer if necessary. But you can just see how it's done for me .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question