1

I would like to replace all floating point numbers within given block with white spaces so that the number of the latter corresponds to the "length" of the floating point number. For instance, I want to change

$S_{1}$ &  2.3  & 3.14159 &  2.73 & 

into

$S_{1}$ &       &         &       &

so I go into block mode with CTRL+v and enter

'<,'>s/[0-9]\{1,}\.[0-9]\{1,}/????

and what should I provide in place of ???? so that the number of white spaces fits the number of digits and decimal separator of my number, please?

  • 1
    Possible duplicate of Find and replace all numbers with 0? – Herb Wolfe Feb 21 '17 at 13:25
  • @HerbWolfe not really, since you need to maintain the length of 0s. I think there's a dupe for this around here, but that's not the one. – muru Feb 21 '17 at 15:14
2

You'll probably have to do a double substitution:

s/\v\d+\.\d+/\=substitute(submatch(0),'.',' ','g')/g

This uses an expression for the replacement (:h sub-replace-expression), which simply replaces every character in the matched string (submatch(0)) with a space (subtitute(..., '.', ' ', 'g')).

Or, repeat :

s/\v\d+\.\d+/\=repeat(' ', len(submatch(0)))/g

Perhaps simplest of all would be to use visual selection based on searching. First, search for /\v\d+\.\d+ to get the floating point numbers, then press gn to visual select the next matching one. Then do (r, then space) to replace all the selected characters with spaces. Use as a macro.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.