sponsor Vim development Vim logo Vim Book Ad

NumUtils : calculator with regex support

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

created by
Laszlo Bimba
script type
INTRO                                                         *NumUtils-intro*

    Did you ever want to manipulate a lot of data in text files? Add an offset
    to the axes in G-code, calculate the finances, or just renumber the
    chapters in the table of contents? With this plugin you can do the
    calculations with regular expression support!

    Please feel free to send me a mail if you have a good idea, or found a
    bug. If you think the plugin does something else that you want, send me
    the lines to manipulate, the task that the plugin should do and the
    command that you used for, and i try to find and fix the bug.

COMMANDS                                                   *NumUtils-commands*

    *NumUtilsAdd* *NumUtilsSub* *NumUtilsMul* *NumUtilsDiv*

        With the built-in commands you can do the basic stuff: addition,
        subtraction, multiplication and division. By default the commands
        will be called on the line where the cursor stays, but you can use
        |:range|, |:global|, or |linewise-visual| to give a range as you do
        when you using |:subtitute| for example.

        The using of the commands are same for all:

            :NumUtilsCOMMAND {valueORcolumns}[, {labels}]

        The first argument is a {value} or a List of {column} numbers (see
        below).  The {label} is a regular expression which will be used as a
        prefix to the values which we want to modify. The default {label} is
        '^\|\D', thus if you don't give this argument, then the command will
        modify all of the numbers separated by non-digit characters.

        For example let's increase all numbers in this line with 10, then add
        20 to the values prefixed by the string 'Y: ': >

                X: 10.5, Y: 20, Z: 30,40 - watch to the decimal points!

            :NumUtilsAdd 10

                X: 20.5, Y: 30, Z: 40,50 - watch to the decimal points!

            :NumUtilsAdd 20, 'Y: '

                X: 20.5, Y: 50, Z: 40,50 - watch to the decimal points!
        As you can see, at the first addition the number 30,40 became
        40,50 instead of 40,40 because the default settings uses '.' as
        decimal point. (see |NumUtils_formatPoint|)

        The {labels} can be any regular expressions with some restrictions.
        You can use the '!NUM!' pattern to define the location of the searched
        value and ':NUM:' to match a value with the requested format set by

        For example try this after you select these lines: >

                .star_10 {
                    background: url(stars.png) no-repeat 0 0;

            :NumUtilsAdd 2, 'star_!NUM!', ':NUM: !NUM!;$'

                .star_12 {
                    background: url(stars.png) no-repeat 0 2;
        There is another useful feature: >


            :NumUtilsAdd [1,2], '\(:NUM:\)|\(:NUM:\)|!NUM!'

        We called the NumUtilsAdd command with {columns} instead of {value},
        thus the command adds the values of first and second submatch to the
        !NUM!. And here comes the restrictions:

            * !NUM! can be present only once in the expression
            * do not surround !NUM! with parenthesis (submatch)
            * maximum 6 submatch allowed


        You can write your own functions if you need it. It has to return a
        Number or Float and the definition have to be in the form:

            function FUNCNAME( {args}, {NUM}, {matches} )

        The {args} can be anything that you want to pass to your function. The
        {NUM} is the value that matches to !NUM!. The {matches} is a

            {matches}.label : the actual {label}
                     .sub   : a List of submatches like |matchlist()| returns

        The command to call your function is:

            :NumUtilsCall 'FUNCNAME', {args}, {labels}

        For example let's calculate the C side of a triangle by the A and B
        side using the Pythagorean theorem (a^2 + b^2 = c^2): >

                a=3 b=4 c=0

            function MyFunc( args, NUM, matches )
                return sqrt(   pow( str2float(a:matches.sub[1], 2 ))
                \            + pow( str2float(a:matches.sub[2], 2 ))
                \          )

            NumUtilsCall 'MyFunc', '', 'a=\(:NUM:\) b=\(:NUM:\) c=!NUM!'

                a=3 b=4 c=5
install details
Just open in Vim then press :so %

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
NumUtils.vmb 1.0 2013-06-25 7.3 Laszlo Bimba 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.
SourceForge.net Logo