John Beckett wrote:
> I need some help trying to clean up this tip:
> http://vim.wikia.com/wiki/Super_Retab
>
> The tip offers several solutions to the following problem (in the
> following, each '.' represents a space, and '|-------' represents a
> tab):
For retabbing, I've used:
command! -range=% -nargs=0 Tab2Space exec
"<line1>,<line2>s/^\\t\\+/\\=substitute(submatch(0), '\\t',
repeat(' ', ".&ts."), 'g')"
command! -range=% -nargs=0 Space2Tab exec"<line1>,<line2>s/^\\(
\\{".&ts."\\}\\)\\+/\\=substitute(submatch(0), ' \\{".&ts."\\}',
'\\t', 'g')"
(those should all be on one line, and that 2nd one should have
one space in the "s/^\\( \\{" bit, in case email mungs it between
here and there)
This allows me to use
:%Space2Tab
or
:'<,'>Tab2Space
(define whatever range you like). It automatically sniffs the
tabstop (&ts) setting to determine how many spaces to use. You
can use shiftwidth (&sw) instead if you prefer. This only
changes leading spaces/tabs, and doesn't touch literal
tabs/spaces that aren't at the beginning of the line.
> function! SuperRetab(width) range
> let x = 0
> while x < 10
> exe a:firstline . ',' . a:lastline . 's/^\(~[ ]*\)~[ ]\{' .
> \ a:width . '}/\1\t\2/e'
> let x = x + 1
> endwhile
> endfunction
>
> If we assume firstline = 11, lastline = 19 and width == 2, the function
> executes the following 10 times:
>
> 11,19s/^\(~[ ]*\)~[ ]\{2}/\1\t\2/e
>
> Can someone please decode this for me? What is the tilde doing (match
> last substitute?)? Is it a mistake?? Why is there a \1 and \2, yet I can
> only see one grouping?
My guess would be that it's mis-primed -- that it _should_ do one
substitution first, and then the "~" refers to that previous
pattern, so you're issuing
s/^\(pattern\)/\1
s/^\(pattern\)\(pattern\)/\1\2
s/^\(patternpattern\)\(pattern\)/\1\2
It seems *awfully* convoluted and troublesome in my book.
> I'd also like ideas for what to do with the tip!
My vote is to scrap it and use my above commands :)
-tim
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---