Wiki: Super Retab


Note: You are viewing an old revision of this page. View the current version.


MetaCosm's Attempt (mine doesn't take a range and is put to shame by those below it, only reason it is left here is for history.)

function! SuperRetab(width)
        let n = 1
        while n <= line("$")
                "echo "Outer Loop:".n
                let i = 30 " hardcoded max of 30 deep indent
                while i >= 1
                        let numberOfSpaces = a:width * i
                        "echo "    Inner Loop".i." :: ".numberOfSpaces

                        let j = 1
                        let spaces = "^ "
                        while j < numberOfSpaces
                                let spaces = spaces . " "
                                let j = j + 1
                        endwhile

                        let k = 1
                        let tabs = ""
                        while k < numberOfSpaces
                                let tabs = tabs . "\t"
                                let k = k + a:width
                        endwhile

                        "echo "s/" . spaces . "/" . tabs . "/"

                        let l = substitute(getline(n), spaces, tabs, "")
                        call setline(n, l)
                        let i = i - 1
                endwhile
                let n = n + 1
        endwhile
endfunction

p0g's attempt: (I think we have a winner here! --MetaCosm)

function! SuperRetab(width) range
  exe a:firstline . ',' . a:lastline . 's/\v%(^ *)@<= {'. a:width .'}/\t/g'
endfunction
OR as a command:
:command! -nargs=1 -range SuperRetab <line1>,<line2>s/\v%(^ *)@<= {<args>}/\t/g

Cleaned up function by FallingCow (*Wow, much better --MetaCosm)

version 1

function! SuperRetab() range
        let x = 0
        while x < 10
                exe a:firstline . ',' . a:lastline . 's/^\([ ]*\)[ ]\{' . &sw . '}/\1\t\2/e'
                let x = x + 1
        endwhile
endfunction

version 2

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

version 3

function! SuperRetab(width) range
        let @" = ''
        redir @"
        while @" !~ 'E486:'
                silent! exe a:firstline . ',' . a:lastline . 'g/^\([ ]*\)[ ]\{' . a:width . '}/s//\1\t\2/'
        endwhile
endfunction

Tofer's Attempt

fun! F(len)
    let re = '^\(\( \{a:len}\)*\) \{a:len}'
    let n = 1
    while n <= line("$")
       let l = getline(n)
       while l = re
          let l = substitute(l,re,"\\1\\t","")
       endwhile
       call setline(n,l)
       let n = n + 1
    endwhile
endfunc

Sponsored by: Labrat Technology -- Need Consulting Services? Programming, Maintenance, Security, Database Analysis & Design, Hosting -- we do it all!
Copyright 2005 Robert Melton
Send questions and comments to
Last updated March 10th, 2005