Skip to main content

A Python-to-Vim bridge decorator that allows transparent calls to Python functions in native Vim scripts.

Project description

What is it?

vim_bridge is a Python-to-Vim bridge decorator that allows transparent calls to Python functions in native Vim scripts.

Installation

Simply install the vim_bridge Python package, using setuptools, easy_install, or pip.

Usage

In a Vim script, decorate your Python functions as follows to expose them as native Vim callables. Both arguments and return values are casted so it should be transparent:

python << endpython
from vim_bridge import bridged

@bridged
def SayHello(first, last):
    return "Hello, %s %s!" % (first, last)

endpython

" Now call directly into the Python function!
echo SayHello("John", "Doe")
           " prints "Hello, John Doe!"

Supported

The following data types have proven to work:

  • Strings

  • Integers

  • Lists

  • Exceptions

More examples

Passing in a list:

python << endpython
from vim_bridge import bridged

@bridged
def GetLongest(list):
    return max(map(lambda s: len(s), list))

endpython

echo GetLongest(['one', 'two', 'three', 'four'])
            " returns 5 (because "three" is 5 chars long)

Catching exceptions:

python << endpython
from vim_bridge import bridged

@bridged
def WillCauseException():
    raise Exception("Oops")

endpython

" This will throw an error to the user...
echo WillCauseException()

" But here's how you can catch that in Vim
try
    echo WillCauseException()
catch
    echo "Something went wrong. Aborting."
finally
    echo "Cleaning up."
endtry

Using Python stdlib functions to do work that would be more difficult using pure Vim scripting:

python << END
import os.path
from vim_bridge import bridged

@bridged
def NormalizePath(path):
    return os.path.realpath(path)
END

echo NormalizePath("/this/../or/./.././that/is/./a/.//very/../obscure/..//././long/./../path/name")
echo NormalizePath("..")

You can use the bridged function definitions within a Python block itself, or from inside Vim, it does not matter. In this example, NormalizePath is called from both Python and Vim:

python << END
import os.path
from vim_bridge import bridged

@bridged
def NormalizePath(path):
    return os.path.realpath(path)

@bridged
def RealPath(path):
    # It does not matter if you call NormalizePath from here...
    return NormalizePath(path)
END

" ...or from here
echo NormalizePath("/this/../or/./.././that/is/./a/.//very/../obscure/..//././long/./../path/name")
echo RealPath("..")

Since vim_bridge 0.4, the function name casing convention is automatically converted to match Vim’s conventions (and requirement even, since function names must start with a capital letter). Besides casing, prefixing the Python function with an underscore will lead to the function being defined in the Vim context as a <SID>-prefixed function (i.e. a “private” function that cannot be called from outside the script):

python << eop
import os
import vim
from vim_bridge import bridged

@bridged
def public():
    return "I am public."

@bridged
def _private():
    return "I am private (available in the current script only)."

@bridged
def my_name_is_auto_converted():
    return "In Python, I'm called my_name_is_auto_converted, " + \
           "but in Vim, I'm called MyNameIsAutoConverted :)"

@bridged
def _long_private_name():
    return "I'm private, and my case is converted automatically."
eop

echo Public()
echo s:Private()
echo MyNameIsAutoConverted()
echo s:LongPrivateName()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vim_bridge-0.5.tar.gz (3.7 kB view details)

Uploaded Source

Built Distributions

vim_bridge-0.5-py3.3.egg (5.6 kB view details)

Uploaded Source

vim_bridge-0.5-py2.7.egg (5.3 kB view details)

Uploaded Source

File details

Details for the file vim_bridge-0.5.tar.gz.

File metadata

  • Download URL: vim_bridge-0.5.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for vim_bridge-0.5.tar.gz
Algorithm Hash digest
SHA256 d2cd47ac8e6b052add7b51b2a964e46c6f7696018545c2bf0ad8a3fb16b431e4
MD5 bb96c4cb0e64279a71368d5625dbe495
BLAKE2b-256 38d1bc58e7dc4f98457593b90f1e6b04b0a1f58875a47a4ce0da29714c4b9b52

See more details on using hashes here.

File details

Details for the file vim_bridge-0.5-py3.3.egg.

File metadata

  • Download URL: vim_bridge-0.5-py3.3.egg
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.4

File hashes

Hashes for vim_bridge-0.5-py3.3.egg
Algorithm Hash digest
SHA256 de68882f7d12f2291c80d1189ac3fd1c69eabe32a03c05ec51b39996c1d8fdf0
MD5 fa1132d1b80443cf539f9043a05099c1
BLAKE2b-256 b7069f3db8318cd83303a88023196ed6eb9e0b98877791f508e5391d062f579e

See more details on using hashes here.

File details

Details for the file vim_bridge-0.5-py2.7.egg.

File metadata

  • Download URL: vim_bridge-0.5-py2.7.egg
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.4

File hashes

Hashes for vim_bridge-0.5-py2.7.egg
Algorithm Hash digest
SHA256 e1de7bf3687117c6f3854d0881fd8b8e1efd4a84a1adcc956b2bbe753fdee05e
MD5 217cb14dd44b942327987c8267d7bb18
BLAKE2b-256 a2301aa9acdbcaa4465ce903e1176228b80354c7127919386b60068da04cf920

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page