Skip to main content
added 656 characters in body
Source Link

In this specific case, I'd use plain substitution with the sub-replace-\=, where you can enter an expression that is evaluated before doing the substitution. Things to keep in mind when using \= in the substitution are that special meaning for characters (in :h sub-replace special) does not apply and that text matched with \( \) need to be accessed with submatch(1) etc. submatch(0) contains the whole matched text. For full details, you can see :h sub-replace-\=.

Going from format HH:MM:SS to sum in seconds:

01:02:26 03:04 03:14 

can be done with

:%s/\(\(\d\+\):\)\?\(\d\+\):\(\d\+\)/\=submatch(2)*3600+submatch(3)*60+submatch(4) 

where the hours are optional. Now we have

3746 184 194 

which we can yank (clearing the register first) using the command

:let @a="" | %y A 

To calculate the sum to the last line, we can use :put in combination with the expression register @=

:$put =eval('0' . substitute(@a, '\n', '+', 'g') . '0') 

where we substitute newlines in the register with + signs using the function substitute(). The register also contains newlines in the beginning and at the end, so we need to pad it with zeroes on both ends. This results in

3746 184 194 4124 

To go in the other direction (from seconds to HH:MM:SS), you can do

:%s@\d\+@\=printf("%02d:%02d:%02d", submatch(0)/3600, submatch(0)%3600/60, submatch(0)%60) 

where you need to use some other character than / as a delimiter, since it is used in the expression. I chose to use @. In the end, we get

01:02:26 00:03:04 00:03:14 01:08:44 

In this specific case, I'd use plain substitution with the sub-replace-\=, where you can enter an expression that is evaluated before doing the substitution. Things to keep in mind when using \= in the substitution are that special meaning for characters (in :h sub-replace special) does not apply and that text matched with \( \) need to be accessed with submatch(1) etc. submatch(0) contains the whole matched text. For full details, you can see :h sub-replace-\=.

Going from format HH:MM:SS to sum in seconds:

01:02:26 03:04 03:14 

can be done with

:%s/\(\(\d\+\):\)\?\(\d\+\):\(\d\+\)/\=submatch(2)*3600+submatch(3)*60+submatch(4) 

where the hours are optional. To go in the other direction (from seconds to HH:MM:SS), you can do

:%s@\d\+@\=printf("%02d:%02d:%02d", submatch(0)/3600, submatch(0)%3600/60, submatch(0)%60) 

where you need to use some other character than / as a delimiter, since it is used in the expression. I chose to use @.

In this specific case, I'd use plain substitution with the sub-replace-\=, where you can enter an expression that is evaluated before doing the substitution. Things to keep in mind when using \= in the substitution are that special meaning for characters (in :h sub-replace special) does not apply and that text matched with \( \) need to be accessed with submatch(1) etc. submatch(0) contains the whole matched text. For full details, you can see :h sub-replace-\=.

Going from format HH:MM:SS to sum in seconds:

01:02:26 03:04 03:14 

can be done with

:%s/\(\(\d\+\):\)\?\(\d\+\):\(\d\+\)/\=submatch(2)*3600+submatch(3)*60+submatch(4) 

where the hours are optional. Now we have

3746 184 194 

which we can yank (clearing the register first) using the command

:let @a="" | %y A 

To calculate the sum to the last line, we can use :put in combination with the expression register @=

:$put =eval('0' . substitute(@a, '\n', '+', 'g') . '0') 

where we substitute newlines in the register with + signs using the function substitute(). The register also contains newlines in the beginning and at the end, so we need to pad it with zeroes on both ends. This results in

3746 184 194 4124 

To go in the other direction (from seconds to HH:MM:SS), you can do

:%s@\d\+@\=printf("%02d:%02d:%02d", submatch(0)/3600, submatch(0)%3600/60, submatch(0)%60) 

where you need to use some other character than / as a delimiter, since it is used in the expression. I chose to use @. In the end, we get

01:02:26 00:03:04 00:03:14 01:08:44 
added 20 characters in body
Source Link

In this specific case, I'd use plain substitution with the sub-replace-\=, where you can enter an expression that is evaluated before doing the substitution. Things to keep in mind when using \= in the substitution are that special meaning for characters (in :h sub-replace special) does not apply and that text matched with \( \) need to be accessed with submatch(1) etc. submatch(0) contains the whole matched text. For full details, you can see :h sub-replace-\=.

Going from format HH:MM:SS to sum in seconds:

01:02:26 03:04 03:14 

can be done with

:%s/\(\(\d\+\):\)\?\(\d\+\):\(\d\+\)/\=submatch(2)*3600+submatch(3)*60+submatch(4) 

where the hours are optional. To go in the other direction (from seconds to HH:MM:SS), you can do

:%s@\d\+@\=printf("%02d:%02d:%02d", submatch(0)/3600, submatch(0)%3600/60, submatch(0)%60) 

where you need to use some other character than / as a delimiter, since it is used in the expression. I chose to use @.

In this specific case, I'd use plain substitution with the sub-replace-\=, where you can enter an expression that is evaluated before doing the substitution. Things to keep in mind when using \= in the substitution are that special meaning for characters (in :h sub-replace special) does not apply and that text matched with \( \) need to be accessed with submatch(1) etc. submatch(0) contains the whole matched text. For full details, you can see :h sub-replace-\=.

Going from format HH:MM:SS to sum in seconds:

01:02:26 03:04 03:14 

can be done with

:%s/\(\(\d\+\):\)\?\(\d\+\):\(\d\+\)/\=submatch(2)*3600+submatch(3)*60+submatch(4) 

where the hours are optional. To go in the other direction (from seconds to HH:MM:SS), you can do

:%s@\d\+@\=printf("%02d:%02d:%02d", submatch(0)/3600, submatch(0)%3600/60, submatch(0)%60) 

where you need to use some other character than / as a delimiter, since it is used in the expression.

In this specific case, I'd use plain substitution with the sub-replace-\=, where you can enter an expression that is evaluated before doing the substitution. Things to keep in mind when using \= in the substitution are that special meaning for characters (in :h sub-replace special) does not apply and that text matched with \( \) need to be accessed with submatch(1) etc. submatch(0) contains the whole matched text. For full details, you can see :h sub-replace-\=.

Going from format HH:MM:SS to sum in seconds:

01:02:26 03:04 03:14 

can be done with

:%s/\(\(\d\+\):\)\?\(\d\+\):\(\d\+\)/\=submatch(2)*3600+submatch(3)*60+submatch(4) 

where the hours are optional. To go in the other direction (from seconds to HH:MM:SS), you can do

:%s@\d\+@\=printf("%02d:%02d:%02d", submatch(0)/3600, submatch(0)%3600/60, submatch(0)%60) 

where you need to use some other character than / as a delimiter, since it is used in the expression. I chose to use @.

Elaborated the use of \= and submatch()
Source Link

In this specific case, I'd use plain substitution with the sub-replace-\=, where you can enter an expression that is evaluated before doing the substitution. Things to keep in mind when using \= in the substitution are that special meaning for characters (in :h sub-replace special) does not apply and that text matched with \( \) need to be accessed with submatch(1) etc. submatch(0) contains the whole matched text. For full details, you can see :h sub-replace-\=.

Going from format HH:MM:SS to sum in seconds:

01:02:26 03:04 03:14 

can be done with

:%s/\(\(\d\+\):\)\?\(\d\+\):\(\d\+\)/\=submatch(2)*3600+submatch(3)*60+submatch(4) 

where the hours are optional. To go in the other direction (from seconds to HH:MM:SS), you can do

:%s@\d\+@\=printf("%02d:%02d:%02d", submatch(0)/3600, submatch(0)%3600/60, submatch(0)%60) 

where you need to use some other character than / as a delimiter, since it is used in the expression.

In this specific case, I'd use plain substitution with the sub-replace-\=, where you can enter an expression that is evaluated before doing the substitution.

Going from format HH:MM:SS to sum in seconds:

01:02:26 03:04 03:14 

can be done with

:%s/\(\(\d\+\):\)\?\(\d\+\):\(\d\+\)/\=submatch(2)*3600+submatch(3)*60+submatch(4) 

where the hours are optional. To go in the other direction (from seconds to HH:MM:SS), you can do

:%s@\d\+@\=printf("%02d:%02d:%02d", submatch(0)/3600, submatch(0)%3600/60, submatch(0)%60) 

In this specific case, I'd use plain substitution with the sub-replace-\=, where you can enter an expression that is evaluated before doing the substitution. Things to keep in mind when using \= in the substitution are that special meaning for characters (in :h sub-replace special) does not apply and that text matched with \( \) need to be accessed with submatch(1) etc. submatch(0) contains the whole matched text. For full details, you can see :h sub-replace-\=.

Going from format HH:MM:SS to sum in seconds:

01:02:26 03:04 03:14 

can be done with

:%s/\(\(\d\+\):\)\?\(\d\+\):\(\d\+\)/\=submatch(2)*3600+submatch(3)*60+submatch(4) 

where the hours are optional. To go in the other direction (from seconds to HH:MM:SS), you can do

:%s@\d\+@\=printf("%02d:%02d:%02d", submatch(0)/3600, submatch(0)%3600/60, submatch(0)%60) 

where you need to use some other character than / as a delimiter, since it is used in the expression.

Source Link
Loading