Vim's Omnicompletion with Python just doesn

2019-01-21 02:12发布

问题:

I've searched around for an hour, both on Stack Overflow and elsewhere. Alas! Please help. Vim's omnicompletion just doesn't work.

  1. I have Vim 7.2 compiled with Python support.

  2. filetype plugin on is in my .vimrc.

  3. When a .py file is open, :echo &omnifunc prints pythoncomplete#Complete.

  4. I'm working with a large project and I have a tags file generated with exhuberant-ctags. It's in Vim's ctags path. I can test it by typing ^] on a symbol and I'm then taken to the symbols' definition.

  5. Update 1: All of my project's code is in the python-in-Vim's path. I can :python import myproject successfully.

Now, anywhere I try C-x C-o, all I get is:

-- Omni completion (^O^N^P) Pattern not found

What am I doing wrong?

Update 2: When I type C-x C-o C-n at the module-level, Vim displays a completion popup with a few module-level constants from other modules in my project. But it's only constants (symbols capital letters) and the completion still doesn't work anywhere else.

Update 3: I've found that C-x C-o at the top of the file starts some kind of omnicompletion, and completion for pprint. brings up the menu and quick-reference of everything in the pprint module. However, none of my own module's imports are being completed.

Update 4, one year later: I gave up and learned Emacs. I have been to the dark side, the mystical land of intrigue and spice, and I say to thee that I have found The Way.

Update 5, two years later: I went back to Vim. Emacs is beautiful, but even after 1.5 years of Emacs, I'm still faster at getting work done in Vim. I've stopped writing Python for now, however, and can't test how well these suggestions work.

回答1:

What module contains the symbol you are trying to complete? Is it in the python stdlib? Or is it a third-party module?

Make sure that the module/package is in the PYTHONPATH.

In Vim, do:

:python import sys
:python print sys.path

To add the module's directory:

:python sys.path.append("/path/to/directory/")


回答2:

Sounds like the questioner has long since gone to the dark side*, but for what it's worth I've just had this symptom, and in my case the cause was that a module I was using relied on Python 2.7 but my version of Vim was compiled with Python 2.5.

To diagnose I tried :python import mymodule, which failed with an error about importing a dependent module. Then :python import dependentmodule which failed with the next step in the chain. And so on & so on, until it failed trying to import a system module that was new since Python 2.7. Problem found.

To solve, I just did sudo port install vim +python27. But that's for OSX. YMMV.

(* I'm kidding. Emacs users are our friends. It's the people programming in Notepad we all have to save...)



回答3:

Since you were prudent and made certain your code is reachable by the PYTHONPATH, per codeape's suggestion, is there a possibility that you are running into the import bug for Vim Python omni-complete? This bug still exists as of Vim 7.2.245.

Essentially, if any import statement fails in the file you are working in, regardless of whether it's wrapped in a Try-Except clause, it will completely break omni-completion. It should be fairly easy to check for this, since most imports occur at the very beginning of the file.

If you do decide that this bug is the cause of your troubles, your options include:

  • making sure that the modules you import are on the system path, not just the project files
  • commenting out any import statements that fail
  • fixing the bug
  • using ropevim as your completion method
  • using a different editor; Netbeans IDE has Python support, and the jVi plugin is rather good if you're a Vim addict like myself (don't let the 1990s look of the home page fool you)


回答4:

Having updated to Fedora 16 (but still compiling vim from source), omni completion stopped working with the same message as above. I "fixed" it by re-maping the keys.

inoremap <C-space> <C-x><C-o>

in ~/.vimrc and now it works again.



回答5:

I had a similar problem with omni completion not working. In my case it turned out that the minibufexpl.vim plugin was interfering with omni completion. Here is how I found out:

Normal keyword completion works. Omni complete does not work for any language, not just Python. omnifunc is set correctly. After I C-X C-O, nothing happens. I do ":py print globals()" and it is clear that the pythoncomplete was not loaded. I can ":call pythoncomplete#Complete(1, '')" and see it get loaded. To me this rules out it being a Vim issue. It seems something is interfering with keymapping or otherwise intercept the omni completion request. So I start to disable my plugins one by one. It turns out the culprit in my case is "minibufexpl". I have the Holgado version from github.

It looks like there are many open issues with MBE according to the issue tracker on github and there hasn't been any progress since early 2012. I am just going to disable it for now so I can use auto completion. Meanwhile I will just add the following to my vimrc to keep multiple modified buffers open at the same time and use a simple key sequence to cycle through them (MBE more intelligently select the buffers to cycle through but seems too heavy-handed for a simple problem):

set hidden
noremap <C-TAB> :bnext<CR>
noremap <C-S-TAB> :bprev<CR>


回答6:

Have you tried using <C_x><C-]> ?



回答7:

c-x c-n works to get the list of members of an object.



回答8:

I used supertab (http://www.vim.org/scripts/script.php?script_id=1643)

Since C-x C-o is a bit frustrating to use

in .vimrc:

let g:SuperTabDefaultCompletionType = "<c-x><c-o>"

then just use Tabb for omnicompletion



回答9:

If you are using python2, make sure you

sudo apt-get install vim.nox-py2 

and use vim.nox-py2 instead of vim.