pycalc


Ⅰ. 插件描述

An python calculator for Vim, modify your text directly with python.

Ⅱ. 基本信息

创建日期:  2009-07-21
使用用户:  1
Github星:  1
插件作者:  Kearn Holliday

Ⅲ. 安装方法

使用Vundle管理器安装

在你的.vimrc下添加:
Plugin 'kearnh/pycalc'
… 然后在Vim中运行以下命令:
:source %
:PluginInstall

对于Vundle版本 < 0.10.2,请用上面的Bundle替换Plugin。

使用NeoBundle管理器安装

在你的.vimrc下添加:
NeoBundle 'kearnh/pycalc'
… 然后在Vim中运行以下命令:
:source %
:NeoBundleInstall

使用VimPlug管理器安装

在你的.vimrc下添加:
Plug 'kearnh/pycalc'
… 然后在Vim中运行以下命令:
:source %
:PlugInstall

使用Pathogen管理器安装

在终端中运行以下命令:
cd ~/.vim/bundle
git clone https://github.com/kearnh/pycalc

Ⅳ. 文档说明

PyCalc

Introduction

This plugin, which I call PyCalc, is a python calculator inside of Vim. It
allows operations directly on text within the current buffer, using Python
one-liners. The plugin can use the word under the cursor as either a "number" or
a string in a Python expression. Additionally a whole line of text can be used
as a list of "numbers" or strings in a Python expression. The result can be
inserted back into the current buffer. The insertion is actually performed after
a specified Vim normal command is run (see the help file for details), allowing
for insertion at arbitrary locations in the current buffer.

The idea for this plugin came from a Vim tip, on the Vim tips wiki, at
http://vim.wikia.com/wiki/VimTip1235. The idea presented there was to use Python
inside of Vim as a calculator. I thought that this idea was really neat, and set
about using it. However, fairly soon it's shortcomings became apparent. In
particular, there was no way to directly insert the result of a calculation.
More irritatingly, switching to insert mode to manually insert the result meant
that the answer of the calculation, shown on the Vim status line, was lost,
being replaced by "-- INSERT --" to indicate the mode switch. This required me
to recall the calculation, which is fine for a short result, but gets
increasingly difficult as results get longer. So I decided to extend the
original Vim tip into a (hopefully) useable script, which lead to PyCalc :).

Some Simple Examples

Note that the default behaviour is to replace the word (or line) the cursor
is on. The following examples assume this behaviour, however it can be changed,
even for single invocations. See the help file for more details.

bold denotes cursor location

.. +-------------------+-------------------------+---------------------------+------------------------------------------+

| PyCalc Expression | Current Line            | Result                    | Notes                                    |
+===================+=========================+===========================+==========================================+
|                   |                         |                           | An ordinary python function call. For    |
|                   |                         |                           | convenience [*]_ parenthesis are         |
|                   |                         |                           | inserted by PyCalc. Note that the        |
|                   |                         |                           | function returns a list, which is space  |
|                   |                         |                           | delimited by default; "[,,]" are         |
|                   |                         |                           | stripped. The same is true when          |
|                   |                         |                           | interpreting the current line as a list  |
|  range 10         |  *an empty line*        | 0 1 2 3 4 5 6 7 8 9       | (see examples below).                    |
+-------------------+-------------------------+---------------------------+------------------------------------------+
|                   |                         |                           | Operation performed on the current WORD  |
|                   |                         |                           | as a "number". The # symbol acts as a    |
|                   |                         |                           | placeholder for a "number". The # can be |
|                   |                         |                           | left out if the expression then starts   |
| #**2              | 0 1 2 3 **4** 5 6 7 8 9 | 0 1 2 3 **16** 5 6 7 8 9  | with an operator.                        |
+-------------------+-------------------------+---------------------------+------------------------------------------+
|                   |                         |                           | Operate on a list of "numbers". The      |
|                   |                         |                           | symbol ## acts as a placeholder for a    |
|                   |                         |                           | list of "numbers". The whole line is     |
|                   |                         |                           | taken as the list, therefore the whole   |
| sum ##            | 0 1 2 3 4 5 6 7 8 9     | 45                        | line is replaced.                        |
+-------------------+-------------------------+---------------------------+------------------------------------------+
|                   |                         |                           | For convenience #i expands to a list     |
|                   |                         |                           | comprehension using a list of "numbers". |
|                   |                         |                           | Ie.: #i**2 exands to [i**2 for i in ##]  |
| #i**2             | 0 1 2 3 4 5 6 7 8 9     | 0 1 4 9 16 25 36 49 64 81 | before further expansion.                |
+-------------------+-------------------------+---------------------------+------------------------------------------+
|                   |                         |                           | Operate on the current WORD, treating it |
|                   |                         |                           | as a Python string. The $ symbol acts as |
| $[::-1]           | Hello **World!**        | Hello !dlroW              | placeholder for the string.              |
+-------------------+-------------------------+---------------------------+------------------------------------------+
|                   |                         |                           | Operate on a list of strings. The $$     |
| $$[::-1]          | Hello **World!**        | World! Hello              | symbol acts as placeholder for the list. |
+-------------------+-------------------------+---------------------------+------------------------------------------+
|                   |                         |                           | Operate on the whole line taken as one   |
|                   |                         |                           | string, spaces and all. The symbol $l    |
|                   |                         |                           | acts as placeholder for the string       |
|                   |                         |                           | (which is the current line). This        |
| $l[::-1]          | Hello **World!**        | olleH !dlroW              | obviously has no "number" equivalent.    |
+-------------------+-------------------------+---------------------------+------------------------------------------+

(sorry but github doesn't seem to display the following table correctly)

.. list-table::
:widths: 10 30 30 30
:header-rows: 1

    • PyCalc Expression
    • Current Line
    • Result
    • Notes
    • range 10
    • an empty line
    • 0 1 2 3 4 5 6 7 8 9
    • An ordinary python function call. For convenience [*]_ parenthesis
      are inserted by PyCalc. Note that the function returns a list,

    which is space delimited by default; "[,,]" are stripped. The same
    is true when interpreting the current line as a list (see examples
    below).

    • *2

    • 0 1 2 3 4 5 6 7 8 9
    • 0 1 2 3 16 5 6 7 8 9
    • Operation performed on the current WORD as a "number". The # symbol
      acts as a placeholder for a "number". The # can be left out if the

    expression then starts with an operator.

    • sum ##
    • 0 1 2 3 4 5 6 7 8 9
    • 45
    • Operate on a list of "numbers". The symbol ## acts as a placeholder
      for a list of "numbers". The whole line is taken as the list,

    therefore the whole line is replaced.

    • i**2

    • 0 1 2 3 4 5 6 7 8 9
    • 0 1 4 9 16 25 36 49 64 81
    • For convenience #i expands to a list comprehension using a list of
      "numbers". Ie.: #i2 exands to [i2 for i in ##] before further

    expansion.

    • $[::-1]
    • Hello World!
    • Hello !dlroW
    • Operate on the current WORD, treating it as a Python string. The $
      symbol acts as placeholder for the string.
    • $$[::-1]
    • Hello World!
    • World! Hello
    • Operate on a list of strings. The $$ symbol acts as placeholder for
      the list.
    • $l[::-1]
    • Hello World!
    • olleH !dlroW
    • Operate on the whole line taken as one string, spaces and all. The
      symbol $l acts as placeholder for the string (which is the current

    line). This obviously has no "number" equivalent.

.. [*] Which, I feel, is a priority in vim, as opposed to actual Python code

   where readability is valued.

... I hope this illustrates the basic idea.

Additional features include:

  • Visual mode support, including line and column visual mode (summing columns in
    tables etc.).
  • Evaluating an expression on a line in the current buffer. Makes lengthy
    calculations easy inside a scratch buffer.
  • Replaying a pycalc command via a vim macro.
  • Ability to assign values (possibly from the vim buffer) to variables, so that
    the variables can be used in later expressions.
  • Saving the last pycalc command for easy repitiiton.

You can reference the full documentation in ./doc directory.

添加新评论