简体   繁体   English

UltiSnips 和 YouCompleteMe

[英]UltiSnips and YouCompleteMe

I have bundles ultisnips and youcompleteme installed on my macvim.我在我的 macvim 上安装了 ultisnips 和 youcompleteme 包。 The problem is that ultisnips doesn't work because tab is bound by ycm.问题是 ultisnips 不起作用,因为选项卡受 ycm 约束。 I tried putting let g:UltiSnipsExpandTrigger = "<s-tab>" so that I can trigger the snippet completion with shift-tab, but it doesn't work for some unknown reason.我尝试放置let g:UltiSnipsExpandTrigger = "<s-tab>"以便我可以使用 shift-tab 触发代码段完成,但由于某些未知原因它不起作用。 I could use caps as the trigger, but so far I've found no way to do that.我可以使用大写作为触发器,但到目前为止我还没有找到办法做到这一点。

Do any of you use those two add-ons together?你们中有人一起使用这两个附加组件吗? What can I do to make shift-tab work?我该怎么做才能使 shift-tab 工作? Can you recommend another key to trigger snippets?你能推荐另一个触发片段的键吗?

Another option is using the SuperTab plugin:另一种选择是使用 SuperTab 插件:

" if you use Vundle, load plugins:
Bundle 'ervandew/supertab'
Bundle 'Valloric/YouCompleteMe'
Bundle 'SirVer/ultisnips'

" make YCM compatible with UltiSnips (using supertab)
let g:ycm_key_list_select_completion = ['<C-n>', '<Down>']
let g:ycm_key_list_previous_completion = ['<C-p>', '<Up>']
let g:SuperTabDefaultCompletionType = '<C-n>'

" better key bindings for UltiSnipsExpandTrigger
let g:UltiSnipsExpandTrigger = "<tab>"
let g:UltiSnipsJumpForwardTrigger = "<tab>"
let g:UltiSnipsJumpBackwardTrigger = "<s-tab>"

Here YouCompleteMe is bound to a different combination Ctrl n , but then that combination is bound to tab through SuperTab.这里 YouCompleteMe 绑定到不同的组合Ctrl n ,但该组合通过 SuperTab 绑定到 Tab。 UltiSnips and SuperTab play nice together, so you can then just bind UltiSnips to tab directly and everything will work out. UltiSnips 和 SuperTab 一起玩得很好,所以你可以直接将 UltiSnips 绑定到选项卡,一切都会好起来的。

Try this suggestion on a page from the YouCompleteMe issue tracker.在 YouCompleteMe 问题跟踪器的页面上尝试此建议。 In your .vimrc:在您的 .vimrc 中:

let g:UltiSnipsExpandTrigger="<c-j>"

While this setting will make expanding a snippet share the default mapping for jumping forward within a snippet , it simulates TextMates' behavior as mentioned in the UltiSnips help tags.虽然此设置将使扩展片段共享在片段内向前跳转的默认映射,但它模拟了 UltiSnips 帮助标签中提到的 TextMates 行为。

Since I've mapped my Caps Lock key to Ctrl, this mapping works pretty smoothly.由于我已将 Caps Lock 键映射到 Ctrl,因此此映射非常顺利。

copy the following code to your vimrc, and enjoy.将以下代码复制到您的 vimrc,并享受。 This function will handle all issues between YCM and UltiSnips.此功能将处理 YCM 和 UltiSnips 之间的所有问题。

function! g:UltiSnips_Complete()
    call UltiSnips#ExpandSnippet()
    if g:ulti_expand_res == 0
        if pumvisible()
            return "\<C-n>"
        else
            call UltiSnips#JumpForwards()
            if g:ulti_jump_forwards_res == 0
               return "\<TAB>"
            endif
        endif
    endif
    return ""
endfunction

au BufEnter * exec "inoremap <silent> " . g:UltiSnipsExpandTrigger . " <C-R>=g:UltiSnips_Complete()<cr>"
let g:UltiSnipsJumpForwardTrigger="<tab>"
let g:UltiSnipsListSnippets="<c-e>"
" this mapping Enter key to <C-y> to chose the current highlight item 
" and close the selection list, same as other IDEs.
" CONFLICT with some plugins like tpope/Endwise
inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"

i have this in my vimrc我的 vimrc 中有这个

"" YouCompleteMe
let g:ycm_key_list_previous_completion=['<Up>']

"" Ultisnips
let g:UltiSnipsExpandTrigger="<c-tab>"
let g:UltiSnipsListSnippets="<c-s-tab>"

thats what i did on my first try, but i misspelled UltiSnips with Ultisnips.. oh well, worked out in the end!这就是我第一次尝试时所做的,但我把 UltiSnips 和 Ultisnips 拼错了……哦,好吧,最后解决了!

I personally chose to not use <tab> with YouCompleteMe but navigate it manually.我个人选择不将<tab>与 YouCompleteMe 一起使用,而是手动导航。

So I added this to my .vimrc :所以我将它添加到我的.vimrc

let g:ycm_key_list_select_completion=[]
let g:ycm_key_list_previous_completion=[]

which simply disables the tab key for YCM.这只是禁用 YCM 的Tab键。 Instead you use the movement keys (arrows or CTRL-N / CTRL-P ) and select the entry with CR .相反,您使用移动键(箭头或CTRL-N / CTRL-P )并使用CR选择条目。 UltiSnips works default with tab . UltiSnips 默认使用tab

Just putting together answers by Michaelslec, Joey Liu and along with solutions I found in this issue thread and this guy's vimrc , I now have this which solved pretty much all problems.只是将 Michaelslec、Joey Liu 的答案以及我在这个问题线程这个人的 vimrc 中找到的解决方案放在一起,我现在已经解决了几乎所有问题。

function! g:UltiSnips_Complete()
  call UltiSnips#ExpandSnippet()
  if g:ulti_expand_res == 0
    if pumvisible()
      return "\<C-n>"
    else
      call UltiSnips#JumpForwards()
      if g:ulti_jump_forwards_res == 0
        return "\<TAB>"
      endif
    endif
  endif
  return ""
endfunction

function! g:UltiSnips_Reverse()
  call UltiSnips#JumpBackwards()
  if g:ulti_jump_backwards_res == 0
    return "\<C-P>"
  endif

  return ""
endfunction


if !exists("g:UltiSnipsJumpForwardTrigger")
  let g:UltiSnipsJumpForwardTrigger = "<tab>"
endif

if !exists("g:UltiSnipsJumpBackwardTrigger")
  let g:UltiSnipsJumpBackwardTrigger="<s-tab>"
endif

au InsertEnter * exec "inoremap <silent> " . g:UltiSnipsExpandTrigger     . " <C-R>=g:UltiSnips_Complete()<cr>"
au InsertEnter * exec "inoremap <silent> " .     g:UltiSnipsJumpBackwardTrigger . " <C-R>=g:UltiSnips_Reverse()<cr>"

Although I know this post is a little old, I have my own function that is a little more optimized than the one given above:虽然我知道这篇文章有点旧,但我有自己的功能,比上面给出的更优化一点:

function! g:UltiSnips_Complete()
  call UltiSnips#ExpandSnippetOrJump()
  if g:ulti_expand_or_jump_res == 0
    if pumvisible()
      return "\<C-N>"
    else
      return "\<TAB>"
    endif
  endif

  return ""
endfunction

Of course, if you just keep the settings that Joey Liu provided and then just use this function everything will work just perfectly!当然,如果你只是保留Joey Liu提供的设置,然后使用这个功能,一切都会完美无缺!

EDIT: Also, I use another function to increase back-stepping functionality between YouCompleteMe and UltiSnips.编辑:另外,我使用另一个函数来增加 YouCompleteMe 和 UltiSnips 之间的后退功能。 I'll show you what I mean:我会告诉你我的意思:

function! g:UltiSnips_Reverse()                                                                                               
  call UltiSnips#JumpBackwards()                                                                                              
  if g:ulti_jump_backwards_res == 0        
    return "\<C-P>"                                                                                                           
  endif                                                                                                                       

  return ""                                                                                                                   
endfunction

Then just put this in your .vimrc:然后把它放在你的 .vimrc 中:

au BufEnter * exec "inoremap <silent> " . g:UltiSnipsJumpBackwardTrigger . " <C-R>=g:UltiSnips_Reverse()<cr>"

As well as let g:UltiSnipsJumpBackwardTrigger="<s-tab>" and your set!以及let g:UltiSnipsJumpBackwardTrigger="<s-tab>"和您的设置!

I use both of them together.我同时使用它们。 By default YouCompleteMe binds <Tab> and <Down> to select the next completion item and also <S-Tab> and <Up> to select the previous completion item.默认情况下,YouCompleteMe 绑定<Tab><Down>来选择下一个完成项,同时绑定<Tab> <S-Tab><Up>来选择上一个完成项。 You can change the YouCompleteMe bindings with the g:ycm_key_list_select_completion and g:ycm_key_list_previous_completion options.您可以使用g:ycm_key_list_select_completiong:ycm_key_list_previous_completion选项更改 YouCompleteMe 绑定。 Note that the names of these options were recently changed when the option was changed from a single string to a list of strings. Note that the names of these options were recently changed when the option was changed from a single string to a list of strings.

Based on Siegfried's answer, I am using the following which seems more natural:根据齐格弗里德的回答,我使用了以下看起来更自然的方法:

let g:ycm_key_list_select_completion = ['<C-j>']
let g:ycm_key_list_previous_completion = ['<C-k>']

let g:UltiSnipsExpandTrigger = "<C-l>"
let g:UltiSnipsJumpForwardTrigger = "<C-j>"
let g:UltiSnipsJumpBackwardTrigger = "<C-k>"

I also use the c-hjkl bindings somewhere else (switching from a pane to another), but that would only be in normal mode, so there's no problem.我还在其他地方使用c-hjkl绑定(从一个窗格切换到另一个窗格),但这只会在正常模式下,所以没有问题。

I installed the UltiSnips plugin after the YouCompleteMe plugin so I thought they were conflicting, but in reality I had something more interfering:我在 YouCompleteMe 插件之后安装了 UltiSnips 插件,所以我认为它们有冲突,但实际上我有更多干扰:

set paste

Make sure to remove that from .vimrc if it's present.如果它存在,请确保从.vimrc删除它。

While Many answer works fine in this post, I just want to say that the problem is caused by key binding collision between YCM and UltiSnip, while YCM support UltiSnip snippets by default, it takes the default UltiSnip expand trigger <tab> as its completion select key, so UltiSnip snippets will not be expaned by <tab> .虽然许多答案在这篇文章中工作正常,但我只想说问题是由 YCM 和 UltiSnip 之间的键绑定冲突引起的,虽然 YCM 默认支持 UltiSnip 片段,但它采用默认的 UltiSnip 扩展触发器<tab>作为其完成选择键,因此<tab>不会扩展 UltiSnip 片段。 Give them different key binding will solve the problem, I personally use <cn and <cp> for YCM and use the default <tab> for UltiSnip.给他们不同的键绑定可以解决问题,我个人使用<cn<cp>作为YCM,使用默认的<tab>作为UltiSnip。 You can get more details with help youcompleteme doc in vim.您可以通过 vim 中的help youcompleteme doc 获取更多详细信息。

I use ;我用; to expand UltiSnips, it's so nifty for me扩展 UltiSnips,这对我来说太棒了

let g:UltiSnipsExpandTrigger = ";"

I use kj .我使用kj This is what is in my .vimrc :这是我的.vimrc

let g:UltisnipsExpandTrigger="kj" . let g:UltisnipsExpandTrigger="kj"

It rarely happens that I run into word that has kj in it.我很少遇到包含kj单词。 If this is the case I would just wait a couple of seconds after typing k and that type j .如果是这种情况,我会在输入k和输入j后等待几秒钟。

As mentioned by others, mapping Cj to ultisnips works great.正如其他人所提到的,将 Cj 映射到 ultisnips 效果很好。
let g:UltiSnipsExpandTrigger="<cj>"

Now, if you go a bit further and install xcape and use现在,如果你更进一步安装 xcape 并使用
xcape -e "Shift_L=Control_R|J"

You unleash the power of using just the shift key for utlitsnips.您可以释放仅使用 utlitsnips 的 shift 键的力量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM