问题
I have this text with numbers:
My numbers are 04, and 0005
My numbers are 05, and 0006
My numbers are 06, and 0035
My numbers are 07, and 0007
My numbers are 08, and 0009
This is the code I always used to increment or decrement numbers in a selection/block selection/column: p.e. increment the last 4 numbers in above text with 8:
'<,'>s/\%V\<\d\{4}\>/\=submatch(0)+8/g
but I noted today that it does strange things. This is the output:
My numbers are 04, and 13
My numbers are 05, and 14
My numbers are 06, and 37 <---
My numbers are 07, and 15
My numbers are 08, and 17
- it removes the leading zero's ( I would like to keep them if there are leading zero's and not add them if there aren't leading zero's)
- it added 8 to all numbers except 37, where it added 2. (why?)
Can anyone help me to find a regex to add /subtract numbers from selection (or block selection) without losing the leading zero's?
note:
I noted that Control A + Control x keeps the leading zero's and does the work as I want but:
- I've seen that it can not be used in a substitute command ('<,'>s/)
- and I don't know how to add p.e. 200 to a list of numbers (200 x ?)
回答1:
leading zero makes number 8-based
.
(0035)8 == (29)10
You can test in vim:
:echo 0035 == 29
It'll print 1
(true).
Try this:
:%s/\(\<[0-9]\{4}\>\)\@=0*\([0-9]*\)/\=submatch(2)+8/
回答2:
this?
:%s/\v[0-9]{4}/\=printf("%04d", substitute(submatch(0), '^0\+', '', 0)+8)/
EDIT
@Remonn, you should give the right sample input in your question. the line above works on your example.
for your new requirement, take a look the line below:
input:
40
20
120
0500
230
0000000020
00000000000005000
vim cmd:
:%!awk '{if($0~/^0/){f="\%"length($0)"d";$0+=8; s=sprintf(f,$0); gsub(/ /,"0",s);}else s=$0+8;print s}'
will change the file to:
48
28
128
0508
238
0000000028
00000000000005008
in my cmd, I used '%', you can select (v) the numbers you want to do tricks with, then try with my cmd.
good luck
回答3:
By default, Vim treats numbers beginning with 0
as octal.
For this reason I have this in my .vimrc:
set nrformats-=octal " Don't bother with octal, I never use it
来源:https://stackoverflow.com/questions/10194978/increment-and-decrements-numbers