How to do math in vim ?

1,940 views
Skip to first unread message

Jeri Raye

unread,
May 19, 2013, 1:00:03 PM5/19/13
to vim...@googlegroups.com
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.
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


Boyko Bantchev

unread,
May 19, 2013, 1:12:23 PM5/19/13
to vim...@googlegroups.com
On 19 May 2013 20:00, Jeri Raye <jeri...@gmail.com> wrote:
> Can you do subtractions?

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

Tony Mechelynck

unread,
May 19, 2013, 1:47:48 PM5/19/13
to vim...@googlegroups.com
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.
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é

LCD 47

unread,
May 19, 2013, 1:57:28 PM5/19/13
to vim...@googlegroups.com
On 19 May 2013, Jeri Raye <jeri...@gmail.com> 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
Message has been deleted

John Little

unread,
May 19, 2013, 5:21:12 PM5/19/13
to vim...@googlegroups.com

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

Charles Campbell

unread,
Jun 21, 2013, 9:59:24 AM6/21/13
to vim...@googlegroups.com
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

Reply all
Reply to author
Forward
0 new messages