I'm trying to work out if it's possible to refer to 'the previous line' in a regex.
e.g.  if the first 8 characters of a line are blank,
    ^\s{8}
replace them with the 8 characters at the start of the previous line.

Ideally it would handle a line a time, thus multiple blank line starts would be filled in with the last non blank start.


Something like the following (untested) regexp should do the trick:

   :%s/^\(.\{8}\).*\n\zs\s\{8}/\1

or

   :%s/^\(.\{8}\)\(.*\n)\s\{8}/\1\2\1

Playing around with this a little more ("how did you spend your morning, dear?" "Oh, just playing around with some regular expressions for a guy on a email list, one could expand it from 8 spaces to N spaces with something like

:g/^\s\+/k a|?^\S?y|'a|s/^\s\+/\=strpart(@", 0, strlen(submatch(0)))

It tromps on your scratch register, and your "a" mark (the "k a" and "'a" bits) but it is a little more flexible.

If you need multiples of a given number of spaces, you could alter it to

 :g/^\(\s\{4}\)\+/...

which would be multiples of 4 whitespace characters.

-tim


Reply via email to