Loading ...
Sorry, an error occurred while loading the content.

Re: How to do math in vim ?

Expand Messages
  • Boyko Bantchev
    ... In Vim, do ... and see if this is what you want. -- -- You received this message from the vim_use maillist. Do not top-post! Type your reply below the
    Message 1 of 7 , May 19 10:12 AM
    • 0 Attachment
      On 19 May 2013 20:00, Jeri Raye <jeri.raye@...> wrote:
      > Can you do subtractions?

      In Vim, do
      :he ^x
      and see if this is what you want.

      --
      --
      You received this message from the "vim_use" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php

      ---
      You received this message because you are subscribed to the Google Groups "vim_use" group.
      To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
      For more options, visit https://groups.google.com/groups/opt_out.
    • Tony Mechelynck
      ... Sure you can do math, including subtractions and even much more complicated stuff than that -- on integers or on floating-point numbers but not on
      Message 2 of 7 , May 19 10:47 AM
      • 0 Attachment
        On 19/05/13 19:00, Jeri Raye wrote:
        > Hi,
        >
        > Can you do math in Vim?
        > Can you do subtractions?

        Sure you can do math, including subtractions and even much more
        complicated stuff than that -- on integers or on floating-point numbers
        but not on hour-minute-second times. For the latter, you have to convert
        them first to integers or floating-point numbers. In your case, to
        integral milliseconds (which I think would be best) or to floating-point
        seconds. (There are 86400000 milliseconds in a day, and IIUC the highest
        positive 32-bit signed integer is 2147483647 which leaves quite a bit of
        room.)

        See
        :help split()
        :help expr5
        :help join()
        :help printf()

        Beware that in Vim, the decimal point is always a dot, never a comma,
        regardless of locale. You may want to use substitute() (q.v.) to replace
        the comma by a dot in your seconds-and-milliseconds part, or else, split
        at the comma (as well as at both colons) and compute
        ((((((hours*60)+minutes)*60)+seconds)*1000)+milliseconds) giving you an
        integral time in milliseconds. To convert the integral milliseconds back
        to hours-minutes-seconds-milliseconds, see the integral / (quotient) and
        % (remainder) binary operators.

        >
        > I have a srt file where I want to subtract 7 from all the seconds in the
        > file.
        > So this:
        > +------------------------------------+
        > 24
        > 00:07:55,641 --> 00:07:58,393
        > You can't do that to us!
        > We'll tell you a secret.
        >
        > 25
        > 00:07:58,603 --> 00:07:59,769
        > A secret? Who by?
        > +------------------------------------+
        >
        >
        > becomes:
        > +------------------------------------+
        > 24
        > 00:07:48,641 --> 00:07:51,393
        > You can't do that to us!
        > We'll tell you a secret.
        >
        > 25
        > 00:07:51,603 --> 00:07:52,769
        > A secret? Who by?
        > +------------------------------------+
        > The changes in this are:
        > (55 becomes 48)
        > (58 becomes 51)
        > (59 becomes 52)
        >
        >
        > Meaning:
        > - search for the lines with -->
        > - search for the number before the first , (comma)
        > - subtract that number with 7
        > - search for the number before the second , (comma)
        > - subtract that number with 7
        >
        >
        > I don't know if vim can subtract it also to negative numbers?
        > In case you have this:
        > +------------------------------------+
        > 27
        > 00:08:01,898 --> 00:08:04,316
        > She's visiting--
        >
        > 28
        > 00:08:04,525 --> 00:08:07,277
        > Who wants to know about her?
        > +------------------------------------+
        >
        > becomes:
        > +------------------------------------+
        > 27
        > 00:08:-06,898 --> 00:08:-03,316
        > She's visiting--
        >
        > 28
        > 00:08:-03,525 --> 00:08:00,277
        > Who wants to know about her?
        > +------------------------------------+
        > The changes in this are:
        > (01 becomes -06)
        > (04 becomes -03)
        > (07 becomes 00)
        >
        > Although not correct for srt syntax, these are manual fixable .
        >
        > Rgds,
        > Jeri

        Best regards,
        Tony.
        --
        Science is facts; just as houses are made of stones, so is science made
        of facts; but a pile of stones is not a house and a collection of facts
        is not necessarily science.
        -- Henri Poincaré

        --
        --
        You received this message from the "vim_use" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php

        ---
        You received this message because you are subscribed to the Google Groups "vim_use" group.
        To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
        For more options, visit https://groups.google.com/groups/opt_out.
      • LCD 47
        ... [...] You d probably save a lot of time by using a SRT editor, f.i.: http://home.gna.org/gaupol/ /lcd -- -- You received this message from the vim_use
        Message 3 of 7 , May 19 10:57 AM
        • 0 Attachment
          On 19 May 2013, Jeri Raye <jeri.raye@...> wrote:
          > Hi,
          >
          > Can you do math in Vim?
          > Can you do subtractions?
          >
          > I have a srt file where I want to subtract 7 from all the seconds in
          > the file.
          [...]

          You'd probably save a lot of time by using a SRT editor, f.i.:

          http://home.gna.org/gaupol/

          /lcd

          --
          --
          You received this message from the "vim_use" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php

          ---
          You received this message because you are subscribed to the Google Groups "vim_use" group.
          To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
          For more options, visit https://groups.google.com/groups/opt_out.
        • John Little
          ... Surely. Floating point, trigonometry even. I would: 1. Write a regex that matches the numbers you want to manipulate, and not anything else. Maybe
          Message 4 of 7 , May 19 2:18 PM
          • 0 Attachment
            On Monday, May 20, 2013 5:00:03 AM UTC+12, Jeri Raye wrote:
            >
            > Can you do math in Vim?
            > Can you do subtractions?

            Surely. Floating point, trigonometry even. I would:

            1. Write a regex that matches the numbers you want to manipulate, and not anything else. Maybe

            /\d\d:\d\d:\d\d,

            2. Put capturing parentheses around the numbers that might change. In vim these are \( and \):

            /\d\d:\(\d\d\):\(\d\d\),

            3. For convenience, isolate the parts that will change with \zs and \ze:

            /\d\d:\zs\(\d\d\):\(\d\d\)\ze,

            4. Use :substitute with \= and submatch() to change the minutes and seconds to seconds, with markers. Also, now is a good time to do the arithmetic. Note the tricky precedence of string concatenation

            :%s//\='##'.(submatch(1)*60+submatch(2)-7).'###'/g

            5. Use :substitute with \=, submatch(), and printf()

            :%s@@\=printf('%02d:%02d',submatch(1)/60,submatch(1)%60)@g

            Here the replace expression has a /, so :s uses @ instead.

            That, which flowed off my fingers as I went along, didn't end up all that simple, I'm sorry. Straightforward if one is a C programmer, and so familiar with printf and integer arithmetic with / and %. It would be better to write a function that does the manipulation:

            function! Sub(in)
            let minutes = a:in[0:1]
            let seconds = a:in[3:4]
            let time = minutes * 60 + seconds

            let time -= 7

            let minutes = time / 60
            let seconds = time % 60

            return printf('%02d:%02d', minutes, seconds)
            endfunction

            Put that in a file, say sub.vim, source it:

            :so sub.vim

            Then
            :%s/\d\d:\zs\d\d:\d\d\ze,/\=Sub(submatch(0))/g

            Regards, John Little

            --
            --
            You received this message from the "vim_use" maillist.
            Do not top-post! Type your reply below the text you are replying to.
            For more information, visit http://www.vim.org/maillist.php

            ---
            You received this message because you are subscribed to the Google Groups "vim_use" group.
            To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
            For more options, visit https://groups.google.com/groups/opt_out.
          • John Little
            ... Surely. Floating point, trigonometry even. I would: 1. Write a regex that matches the numbers you want to manipulate, and not anything else. Maybe
            Message 5 of 7 , May 19 2:21 PM
            • 0 Attachment
              On Monday, May 20, 2013 5:00:03 AM UTC+12, Jeri Raye wrote:
              >
              > Can you do math in Vim?
              > Can you do subtractions?

              Surely. Floating point, trigonometry even. I would:

              1. Write a regex that matches the numbers you want to manipulate, and not anything else. Maybe

              /\d\d:\d\d:\d\d,

              2. Put capturing parentheses around the numbers that might change. In vim these are \( and \):

              /\d\d:\(\d\d\):\(\d\d\),

              3. For convenience, isolate the parts that will change with \zs and \ze:

              /\d\d:\zs\(\d\d\):\(\d\d\)\ze,

              4. Use :substitute with \= and submatch() to change the minutes and seconds to seconds, with markers. Also, now is a good time to do the arithmetic. Note the tricky precedence of string concatenation

              :%s//\='##'.(submatch(1)*60+submatch(2)-7).'###'/g

              5. Use :substitute with \=, submatch(), and printf()

              :%s@##\(\d\+\)###@\=printf('%02d:%02d',submatch(1)/60,submatch(1)%60)@g

              Here the replace expression has a /, so :s uses @ instead.

              That, which flowed off my fingers as I went along, didn't end up all that simple, I'm sorry. Straightforward if one is a C programmer, and so familiar with printf and integer arithmetic with / and %. It would be better to write a function that does the manipulation:

              function! Sub(in)
              let minutes = a:in[0:1]
              let seconds = a:in[3:4]
              let time = minutes * 60 + seconds

              let time -= 7

              let minutes = time / 60
              let seconds = time % 60

              return printf('%02d:%02d', minutes, seconds)
              endfunction

              Put that in a file, say sub.vim, source it:

              :so sub.vim

              Then
              :%s/\d\d:\zs\d\d:\d\d\ze,/\=Sub(submatch(0))/g

              Regards, John Little

              --
              --
              You received this message from the "vim_use" maillist.
              Do not top-post! Type your reply below the text you are replying to.
              For more information, visit http://www.vim.org/maillist.php

              ---
              You received this message because you are subscribed to the Google Groups "vim_use" group.
              To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
              For more options, visit https://groups.google.com/groups/opt_out.
            • Charles Campbell
              ... You can also do (some) matrix work with the following plugin: http://www.drchip.org/astronaut/vim/index.html#CECMATRIX: * multiply / divide (strictly
              Message 6 of 7 , Jun 21, 2013
              • 0 Attachment
                John Little wrote:
                > On Monday, May 20, 2013 5:00:03 AM UTC+12, Jeri Raye wrote:
                >> Can you do math in Vim?
                >> Can you do subtractions?
                > Surely. Floating point, trigonometry even. I would:
                > [snip]

                You can also do (some) matrix work with the following plugin:
                http://www.drchip.org/astronaut/vim/index.html#CECMATRIX:

                * multiply
                / divide (strictly speaking, multiply by the inverse)
                + add
                - subtract
                () group operation(s)
                ' transpose

                I probably won't try to do eigenvalues, singular value decomposition,
                etc anytime soon, though.

                Regards,
                C Campbell

                --
                --
                You received this message from the "vim_use" maillist.
                Do not top-post! Type your reply below the text you are replying to.
                For more information, visit http://www.vim.org/maillist.php

                ---
                You received this message because you are subscribed to the Google Groups "vim_use" group.
                To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
                For more options, visit https://groups.google.com/groups/opt_out.
              Your message has been successfully submitted and would be delivered to recipients shortly.