3

I have the following line in my vimrc to enable Javascript completion:

autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS

The problem is that if I'm working on a JS file that contains a lot of comments, for example:

// draws the map and the pieces depending on the state of the game

Vim picks words from those comments, such as draws, map, etc.; and considers them as acceptable code suggestions, which they are clearly not. How can I filter them out?

1
  • 1
    Are you sure that you are using omnifunc (<C-x><C-o> it should also say Omni completion in the bottom left)? When I test this my vim doesn't select words from comments. keyword completion on the other hand does select words from comments.
    – FDinoff
    Commented Apr 26, 2013 at 14:00

2 Answers 2

1

Vim has a number of completion mechanism suited for different needs: keyword completion, file name completion… and omni completion, the kind of completion most suited to programming.

Omni completion is usually initiated by pressing <C-x><C-o> and will certainly never pick a suggestion from the comments in your file. Are you sure you are using omni completion and not something else? <C-n> or <C-p>, maybe? See :h ins-completion for the full list.

Also, that line is totally useless. Supposing you have filetype plugin indent on in your ~/.vimrc, it's completely unnecessary to tell Vim to use JS completion in JS file.

1
  • Right you are about the useless lines, thanks. Your comment about the type of completion I am using is also good, using <C-x><C-o> solved the problem. It's some plugin I have installed which mapped a different completion to my Tab key. I'm going to try to fix that and also test what the other answer mentioned. I'll accept your answer depending on my results. Thanks!
    – user347284
    Commented Apr 26, 2013 at 21:43
1

JS autocompletion requires dynamic type inference, which is difficult for non-IDE text editors.(JS has no static class, you know!) Vim probably just implement a fuzzy matching algorithm to make completion. (I just guess, because I don't use vim personally). I think Vim also takes comments into consideration because it probably does not interpret js!

You can try the new Ternjs plugin for Vim. You need node.JS. Make sure your Vim has Python support.

Online demo is on http://ternjs.net/

Generally, javascript autocompletion is difficult to accomplish due to the dynamic nature of that language. Ternjs makes type inference like javascript engine, and thus makes autocompletion better. (Personally I have tried tern in SublimeText. Great Experience except initial loading and no builtin function such as document.body.appendChild)

3
  • Tern looks really good! I like the ability to refactor a var's name and to get its type right in the editor. I'll give it a try tomorrow and accept your answer depending on my result. Thanks!
    – user347284
    Commented Apr 26, 2013 at 21:56
  • Excellent! I was able to set up Tern really quickly and I'm glad to finally have the features I mentioned above in vim. Tern is not really the answer to the question I asked so I'm going to have to accept @romainl's answer though. Sorry, wish I could accept yours as well.
    – user347284
    Commented Apr 26, 2013 at 22:13
  • 1
    Never mind :). I 'm really glad to recommend tern and you like it. And thank you for your voting up! Commented Apr 27, 2013 at 5:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.