Skip to content
Sort and highlight Python imports in Vim
Vim script Python
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
after Highlight imports on decorator lines. Oct 12, 2016
autoload Don't emit empty error message Jan 30, 2018
bin Updated submodules Jan 30, 2018
doc Added option - g:impsort_lines_after_imports Sep 28, 2016
plugin Only apply highlighting to visible buffers. Jul 28, 2016
.gitignore Ignore doc/tags Apr 14, 2017
.gitmodules Updated submodules Jan 30, 2018
LICENSE Updated README Jul 5, 2016
README.md Updated README Jul 5, 2016

README.md

impsort.vim

Vim utility for sorting and highlighting Python imports.

impsort

Installation

Any modern plugin manager will work. If you are installing manually, you will have to run git submodule update --init since Jedi is included as a submodule.

Usage

:ImpSort[!]

The :ImpSort command accepts a range and can be used with visual selections. Using :Impsort! (with bang) will separate from ... import groups with a blank line.

You could also have it sort on save:

autocmd BufWritePre *.py ImpSort!

Or use a keymap:

nnoremap <leader>is :<c-u>ImpSort!<cr>

The sorting method is configurable. Be sure to read :h impsort!

There is another command :ImpSortAuto which will automatically sort imports on InsertLeave. Definitely read :h impsort if this interests you.

Highlighting

By default, imported objects will be highlighted. If your version of Vim is capable of asynchronous calls (Neovim and Vim 8), the highlighting will distinguish imported classes and functions. You can read about customizing the colors in the documentation.

Rationale

I wanted to be able to keep my import lines organized, but didn't want to spend the time sorting them by hand. Using this plugin, you can add a new import and let the sort do its thing.

I also wanted something more forgiving than isort. This plugin will not move imports out of their original placement in the script. For example:

import sys
import os

sys.path.insert(0, 'special/path')

import special

With isort:

import os
import sys
import special

sys.path.insert(0, 'special/path')

With impsort.vim:

import os
import sys

sys.path.insert(0, 'special/path')

import special
You can’t perform that action at this time.