How to make js completion pop up automatically in HTML files?
Asked Answered
G

2

6

There are two ways to use tern_for_vim plugin in HTML files as the webpage say.

use tern_for_vim plugin in HTML files

Both method can work ,both of them can't make js completion menu pop up automatically.

Method1:

1.vim test.html
2.:setlocal omnifunc=tern#Complete
3.To input `<C-X><C-O>` after `document.` 

Now js completion pop up.
Two issues remain for this method.
1.To write setlocal omnifunc=tern#Complete in .vimrc can't work.
Why?

2.How to make js completion menu pop up automatically after document. ,instead of input <C-X><C-O>?

Method2:

sudo cp .vim/bundle/tern_for_vim/after/ftplugin/javascript_tern.vim   .vim/bundle/tern_for_vim/after/ftplugin/html_tern.vim

You should input <C-X><C-O> after document. in order to call js completion menu for your html file edited.

The js completion menu for js file edited can't pop up after document. automatically.

1.How to make js completion menu pop up automatically after document. ,instead of input <C-X><C-O>? (same as in Method1 the second item.)

Galbraith answered 15/3, 2018 at 8:6 Comment(2)
from my point of view your question is totally unclearElegize
Duplicate of https://mcmap.net/q/98176/-autocompletion-in-vim/4989460Randeerandel
L
0

Here is a answer to the same question.

You can use a plugin like AutoComplPop to get automatic code completion as you type.

2015 Edit: I personally use YouCompleteMe now.

Lindsaylindsey answered 23/3, 2018 at 14:58 Comment(0)
A
0

i'm guessing you're using Method 2 and it's working using <C-X><C-O>

you're gonna need inoremap to do this , see : What's the meaning of 'inoremap' in vimrc

and since Tern for Vim is hooked to omni completion ( as https://github.com/ternjs/tern_for_vim ) , you can modify it's key mapping to behave like you want.

from the documentation : http://vimdoc.sourceforge.net/htmldoc/insert.html#ft-javascript-omni

You can use of in mapping to have the popup menu used when typing a character and some condition is met. For example, for typing a dot:

inoremap <expr> . MayComplete()
func MayComplete()
    if (can complete)
      return ".\<C-X>\<C-O>"
    endif
    return '.'
endfunc

Openning vim and typing this in command mode will do the trick :

:inoremap <expr> . ".\<C-X>\<C-O>"

this basically turns the . into a trigger to fire up <CTRL-X> <CTRL-O>

but it's better to put the :map commands into ~/.vim/ftplugin/{filetype}_mappings.vim . (This requires that you have :filetype plugin on.)

and this is for general vim key mapping : http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-Tutorial(Part_1)

check this to see it working : https://i.sstatic.net/G6hRZ.jpg

PS : i used Pathogen to install Tern for Vim ( https://github.com/tpope/vim-pathogen )

i hope this answers you question or at least gives you an idea.

Aquaplane answered 23/3, 2018 at 18:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.