sponsor Vim development Vim logo Vim Book Ad

text.py : This is utility to easy access to vim buffer. For pythonic macrowriters

 script karma  Rating 1/1, Downloaded by 766  Comments, bugs, improvements  Vim wiki

created by
Eugene Minkovskii
script type
This is text.py module for easy association of vim buffer to python language. Module provides Text-calss, which has access vim buffer contents, and has many of string, sequense and file-object methods. You may use slice, may use assignment to slice of buffer, and you can use step argument in this assignment in python 2.3 notation. You can use all of string methods like .upper(), .lower(), .center() etc. You can use many of sequence methods like .append(), .extend(), .reverse() etc. You can use a lot of binary operators like +=, *=; membership test "in" etc. Moreover, Text-instance provide some facilities to work with regular expressions including situations when text in buffer is in multibyte encoding. Searching takes place in unicode, but module provides some new match object: "NoUniMatchObj". This object has all of original match objects attributes but when you expect to get a string, you get a string not unicode. And when you expect to get address (byte-offset) you get address in encoded string too.

Simple example:
import text
t = text.Text()
for i in t.re.finditer(my_regexp):    # search my_regexp in buffer
        t.replace_i(i.start(),            # interactive replace fragment of
                    i.end(),              #   buffer from i.start() to i.end()
                                          #   where i is an instance of
                                          #   NoUniMatchObj
                    ["replasement one",   # There are a lot of substitutions,
                     my_function(i), ...])#   user can choose one of them.
    except t.exceptions.CancelDialog:
        pass                            # handler of Cancel button
t.center(80)                            # centering contents of buffer
t.apply_to_lines(my_other_function)     # apply my_other_function line by line

Another powerful example you can see in vimscript#841

This utility very useful if you need to do python-substitution using confirmation dialogs, for example if you need to choose a lot of various replacements. This is useful too if you work with utf-8 encoding etc.

Russian mirror: http://python.anabar.ru/emin.htm#textpy
install details
1) Make in your harddrive some directory for python-vim programs (let it name   is /pythonvim/). And put text.py into this directory.
2) Instract python to use this directory like sourse of modules. One way to   do this is put following string into your .vimrc file:
:py import sys; sys.path.insert(0, r"/pythonvim/")
   NOTE: in some systems you must put absolute address into sys.path list.
   NOTE: be careful about slash direction, this is system-depended.

That is all. Happy vimming.

Compatibility note:

This program writen for Python 2.3. But it is already compatible with
Python 2.2. And for compatibility with Python 2.1 you may do some actions:
some code need to be comment and some to uncomment.

For compatibility with python 2.1 open this program in vim and execute
  following command:
:%s/^\(\s*\)\(.*#<p21c>\)$/\1## \2/|%s/^\(\s*\)##\s\+\(.*#<p21u>\)$/\1\2/|w!
Be careful, use copy&paste:
      1) put cursor after the colon and type y$
      2) Then type : to go into command line and type <C-R>"
This command comment section using 'yield' statement and import generators
  feature. Then it is uncomment compatibility blocks: raise instraction in
  finditer() method and import nested_scopes feature.
For undoing this changes execute in vim following command:
:%s/^\(\s*\)\(.*#<p21u>\)$/\1## \2/|%s/^\(\s*\)##\s\+\(.*#<p21c>\)$/\1\2/|w!

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
text104.zip 1.0.4 2003-12-08 6.0 Eugene Minkovskii 1.0.4 -- Some spell check of documentation. Thanks to all helpers.
         new raise instractions with some help for developers.
         Bugfix in re.subn() method.
text103.zip 1.0.3 2003-12-04 6.0 Eugene Minkovskii Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.