How to transpose the contents of lines and columns in a file in Vim?

前端 未结 6 640
既然无缘
既然无缘 2021-02-02 16:49

I know I can use Awk, but I am on a Windows box, and I am making a function for others that may not have Awk. I also know I can write a C program, but I would love not to have s

6条回答
  •  失恋的感觉
    2021-02-02 17:38

    The following function performs the required editing operations to “transpose“ the contents of the current buffer, regardless of the number of lines in it.

    fu!T()
    let[m,n,@s]=[0,line('$'),"lDG:pu\r``j@s"]
    g/^/let m=max([m,col('$')])
    exe'%norm!'.m."A \e".m.'|D'
    sil1norm!@s
    exe'%norm!'.n.'gJ'
    endf
    

    For the sake of “golfing”, here is its one-line version:

    let[m,n,@s]=[0,line('$'),"lDG:pu\r``j@s"]|exe'g/^/let m=max([m,col("$")])'|exe'%norm!'.m."A \e".m.'|D'|exe'sil1norm!@s'|exe'%norm!'.n.'gJ'
    

提交回复
热议问题