In eclipse you can hit Ctrl+Shift+o to automatically import all the libraries you reference in your code. Is there any similar plugin for vim to have this feature with python?
Asked
Active
Viewed 4,367 times
17
-
1Thanks for the Eclipse tip, didn't know about this. – Rafe Kettler Sep 30 '10 at 00:16
-
Are you looking to manipulate the libraries as objects or just access/read the files? For access there is ctags and I can also publish a getfile 'gf' enhancement script for python if that is what you are looking for. – michael Sep 30 '10 at 01:42
-
@michael: What I mean is that if I type "itertools.cycle(...)", it should look at the top of the file and automatically add "import itertools" if it is not already present. – Daenyth Sep 30 '10 at 01:48
-
It also removes unused imports. It could potentially arrange them in pep8 order too. Seems quite possible to implement via pyflakes. – Daenyth Sep 30 '10 at 01:54
-
ahh, I was looking for one as well at one stage but couldn't find anything. I ended up hacking one together based on your tags file here http://www.vim.org/scripts/script.php?script_id=2780. It works good for 3rd party source libs doesn't handle builtins. I was going to get around to writing a tags script for builtins to handle this. – michael Sep 30 '10 at 02:02
-
@michael: Hmm, builtins are some of the most useful. There must be an easy way... – Daenyth Sep 30 '10 at 02:06
-
See also: [Vim plugin for automatically generating Python import statements (without using Rope) - Stack Overflow](https://stackoverflow.com/questions/29580323/vim-plugin-for-automatically-generating-python-import-statements-without-using?noredirect=1&lq=1) – user202729 Dec 07 '21 at 12:52
3 Answers
8
There is ropevim. It is available on pypi as well
The autoimport (adds missing imports) and organizeimport (reorder imports) features work well, but it is a little invasive at times (it will create a .ropeproject folder in your project). Rope code completion is also quite good so I use standard code completion with tab, and when it's not enough, I use ctrl-space to use ropevim autocompletion.
Here are some of my mappings with ropevim:
" Rope AutoImport and OrganizeImport
nnoremap <C-S-o> :RopeOrganizeImports<CR>0<CR><CR>
nnoremap <C-S-i> :RopeAutoImport<CR>
" Rope AutoComplete
let ropevim_vim_completion = 1
let ropevim_extended_complete = 1
let g:ropevim_autoimport_modules = ["os.*","traceback","django.*","lxml.etree","lxml.*"]
imap <c-space> <C-R>=RopeCodeAssistInsertMode()<CR>
" Rope Menu
menu Python.Create\ Package :RopeCreatePackage<CR>
menu Python.Create\ Module :RopeCreateModule<CR>

Barthelemy
- 8,277
- 6
- 33
- 36
-
1Hi, I found autoimport doesn't work for django. For example, if I type
when cursor is on HttpResponse, ropevim wouldn't be able to find the name and import it. – faceclean Jan 28 '11 at 04:20 -
1
There is a command line tool called mr.igor
that you could install. There are instructions for hooking it up to vim on the pypi page:

claytron
- 1,575
- 13
- 20
0
I'm using https://github.com/mgedmin/python-imports.vim together with gutentags. Good enough for me (and better than nothing).

daliusd
- 1,025
- 11
- 15