/impsort.vim

Sort and highlight Python imports in Vim

Primary LanguageVim ScriptMIT LicenseMIT

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