Wiki: Super Retab


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



The Challenge

The challenge is to write a function that redoes the tabbing only along the left side of the text. It is a ranged function that takes a single parameter which is the # of spaces used to represent a single indent in the file.

The differences between this and the standard retab are

  1. it takes a parameter for the # of spaces to equal a tab (doesn't use tabstop)
  2. it only replaces groups of spaces at the start of a line.

So it could take a file that looks like

  for {
    that;
  }

and turn it into (|------- represents a tab char) via :call SuperRetab(2)

|-------for {
|-------|-------that;
|-------}

and could create the same output from

     for {
          that;
     }

via :call SuperRetab(5)



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

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

<small>P webcam gay gratuit webcam gay gratuit gay webcam chat webcams gay live gay webcams gay webcam

sm studio bdsm whip japanese bondage new york bdsm piss slave chat bdsm

trample cruel cruel sex cruel facesitting cruel femdom cruel site mistress cruel

mature hairy women femme poilues hairy wife chatte poilue gratuit hairy twat atk pics

college drunk sex drunk girls passed out drunk flashing drunk party pictures drunken pissed

ebony babe ebony sex pictures black porno ebony hot ebony ass gallery ebony escorts

bums bisexual playground bisexual lady bisexual fuck free bisexual video bisexual tgp

pees pissvideos urolagnia urinsex peed hidden

oral sex movie mature oral oralsex anleitung female oral oral and sex oralsex anleitung

</small>


Copyright © 2007 RobertMelton.com