Giter Site home page Giter Site logo

python-imports.vim's Introduction

Overview

Vim script to help adding import statements in Python modules.

You need to have a tags file built (:!ctags -R ., be sure to use exuberant-ctags or Universal Ctags). You can use Gutentags plugin for automatic tags management.

Type :ImportName [<name>] to add an import statement at the top of the file.

Type :ImportNameHere [<name>] to add an import statement above the current line.

I use the following mappings to import the name under cursor with a single keystroke:

map <F5>    :ImportName<CR>
map <C-F5>  :ImportNameHere<CR>

Needs Vim 7.0, preferably built with Python support.

Tested on Linux only.

Installation

I recommend Vundle, pathogen or Vim Addon Manager. E.g. with Vundle do

:BundleInstall "mgedmin/python-imports.vim"

Manual installation: copy plugin/python-imports.vim to ~/.vim/plugin/.

Configuration

In addition to the tags file (and builtin logic for recognizing standard library modules), you can define your favourite imports in a file called ~/.vim/python-imports.cfg. That file should contain Python import statements like

import module1, module2
from package.module import name1, name2

Continuation lines are not supported. Parenthesized name lists are partially supported, if you use one name per line, i.e.

from package.module import (
    name1,
    name2,
)

Special Paths

Aside from the project root path, some projects auto-import its sub-folders also in the Python path (e.g. apps or conf folders) which is usually done to avoid repetitive or lengthy import names. For instance, a project that is located in ~/my_project could have an apps folder which has this logical structure

from apps.alpha import bravo
from apps.charlie import delta

But, the project team might decide to auto-import the apps folder in the environment setup, so that the code will have this import format for convenience

from alpha import bravo
from charlie import delta

To resolve these special imports correctly, the pythonPaths global variable could be used

let g:pythonPaths = [
    \ expand('~/my_project/apps'),
    \ expand('~/my_project/conf'),
    \ ]

Note that the expand() is used here so that the Home directory (~) will be interpreted correctly.

Copyright

python-imports.vim was written by Marius Gedminas <[email protected]>. Licence: MIT.

python-imports.vim's People

Contributors

daliusd avatar mgedmin avatar ranelpadon avatar ross-sharma avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.