Vim plugin to sort python imports using isort
NOTE:
darrikonn/vim-isort is a fork from fisadev/vim-isort. The base logic is the same, with small additional changes/features.
How darrikonn/vim-isort differs from fisadev/vim-isort:
- darrikonn/vim-isort works with vim-rooter.
- darrikonn/vim-isort works with monorepos + with/without vim-rooter.
- darrikonn/vim-isort works with nested isort configurations.
Just call the :Isort
command, and it will reorder the imports of the current python file.
Or select a block of imports with visual mode, and press Ctrl-i
to sort them.
You can also configure isort options, check them on the isort docs.
- Install isort:
pip3 install isort
- Add the plugin using your favourite package manager, e.g.:
Plug 'darrikonn/vim-isort'
Plugin 'darrikonn/vim-isort'
(Or if you don't use any plugin manager, you can just copy the python_vimisort.vim
file to your .vim/ftplugin
folder)
- First you need to allow vim to keep you in the root inside your repo, regardless of where you open your file
" CWD to the root of git repo when opening file inside repo
let g:gitroot=system("git rev-parse --show-toplevel")
let g:is_gitrepo = v:shell_error == 0
silent! cd `=gitroot`
You can configure the default mapping for the visual mode sorter, like this:
let g:vim_isort_map = '<C-i>'
Or disable the mapping with this:
let g:vim_isort_map = ''
Your virtual environment might confuse the configuration path, which results in your repo based isort configuration not working (even though it's loaded). The fix is to tell isort the name of your virtual environment directory in either .isort.cfg or setup.cfg:
virtual_env = venv
not_skip = __init__.py