Skip to content

tarmack/vim-python-ftplugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vim file type plug-in for the Python programming language

The Python file type plug-in for Vim helps you while developing in Python by providing the following features:

  • Automatic syntax checking using pyflakes pyflakes.
  • Syntax based folding for classes, functions, comment blocks and multi line strings (the fold text includes the docstring if found).
  • You can use gf to jump to imported files (searches the Python path).
  • You can search imported files using mappings such as [i.
  • Control-X Control-U completes all available module names.
    • Module name completion starts automatically after typing import or from (automatic completion can be disabled if you find it is too intrusive).
  • Control-X Control-O completes variable names, for example after os or os. it would complete os.path (and others). Be aware that this imports modules to perform introspection and assumes that importing a module does not have serious side effects (although it might, however it shouldn't).
    • You can enable automatic variable completion after typing a dot or if you type a space after import on a from <module> import <variable> line. (this is not enabled by default because of the side effect issue mentioned above).

Experimental features:

  • The plug-in now comes with a Python script that uses the AST module in the Python standard library to implement a type inference engine which can be used to suggest completion candidates. The type inference engine has two nice properties that the other completion methods don't have: It has a lot more information to go by and it works by parsing the source code so it's safe to use on code with side effects.

Installation

To use the plug-in, simply drop the files into your Vim profile directory (usually this is ~/.vim on UNIX and %USERPROFILE%\vimfiles on Windows), restart Vim and execute the command :helptags ~/.vim/doc (use :helptags ~\vimfiles\doc instead on Windows).

To use the syntax check, please make sure pyflakes pyflakes is installed. It can be found in most Linux distributions, e.g. on Debian/Ubuntu Linux it can be installed using the following command:

$ sudo apt-get install pyflakes

To enable folds for classes and functions defined without leading whitespace, make sure your Python syntax file uses a match rather than a keyword statement for the def and class keywords.

Change the line to say something like:

:syntax match [group] "\<\%(def\|class\)\>" [options]

I can recommend you [Dmitry Vasiliev's] extsyntax adaptation of the default Python syntax file. In this file you will need to replace the following line:

:syntax keyword pythonStatement def class nextgroup=pythonFunction skipwhite

with:

:syntax match pythonStatement "\<\%(def\|class\)\>" nextgroup=pythonFunction skipwhite

Options

All options are enabled by default. To disable an option do:

:let g:OPTION_NAME = 0

The g:python_syntax_fold option

Enables syntax based folding for classes, functions and comments.

The g:python_fold_strings option

Enables syntax based folding for strings that span multiple lines.

The g:python_docstring_in_foldtext option

To display the docstring of a class/function in the fold text.

The g:python_decorators_in_foldtext option

To display the decorators in the fold text. Currently arguments to decorators are not shown.

The g:python_decorator_labels option

This is a dictionary mapping decorator names to short labels. By default it is empty.

The g:python_check_syntax option

Enables automatic syntax checking when saving Python buffers. This uses pyflakes pyflakes when available but falls back on the standard Python compiler for syntax checking.

The g:python_auto_complete_modules option

Controls automatic completion of module names after typing import<Space> or from<Space>. Enabled by default.

The g:python_auto_complete_variables option

Controls automatic completion of variables after typing a dot or from <module> import<Space>. Disabled by default.

Contact

If you have questions, bug reports, suggestions, etc. you can contact Bart at bart@tarmack.eu or Peter at peter@peterodding.com. The latest version is available at http://peterodding.com/code/vim/python-ftplugin and https://github.com/tarmack/vim-python-ftplugin.

License

This software is licensed under the MIT license.
© 2013 Peter Odding <peter@peterodding.com> and Bart Kroon <bart@tarmack.eu>.