0

Short question i'm using this regex https://stackoverflow.com/a/11531819/4779063 to sort lines by lenght in vim but i modified it a little bit to make it work with windows and gawk so i end up having something like this:

  vmap <Leader>su ! gawk -f "{ print length(), $0 \| \"sort -n \| cut -d\\  -f2-\"}"<CR>

but everytime i try to use it it says that it could open a .tmp file in my C:\Users\Username\AppData\Local\Temp\VIi33.tmp

any ideas?

Community
  • 1
  • 1

2 Answers2

2

If you want you can try to do that by using only vim to sort your file by length:

:g/^/ s/^.*$/\=substitute(submatch(0),submatch(0),strlen(submatch(0)).'# '.submatch(0),'')/ 
:% sort! n
:% s/^\d\+# //
  • It kind of gets the jobs done (some lines for whatever reason are missplaced) but thing is; those commands are for the all file, and i'm looking to order just what i select on visual mode – Allan Alvarez Ortiz Jul 01 '16 at 05:07
  • If you want to do that you have only to select every time the block and execute every command. I don't figure out a better way to do that and I found it difficult to execute the three commands in pipelined one . – Meninx - メネンックス Jul 01 '16 at 13:12
1

This GNU AWK program sorts records or lines (via an array) based on their length and prints them out in sorted order. Could you use that?

function len_comp_func(i1,v1,i2,v2) {   # define length comparison function for for
  return(length(v1)-length(v2))
}
{
  arr[NR]=$0                            # populate array with all the records
}
END {
  PROCINFO["sorted_in"] = "comp_func"   # define order function
  for (i in arr)                        # traverse in length order
    print arr[i]
}

.

James Brown
  • 36,089
  • 7
  • 43
  • 59