1

Given the following text:

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

How can each line be shifted left/right an amount related to their line number? Desired outputs:

1 2 3 4 5
  1 2 3 4 5
    1 2 3 4 5
      1 2 3 4 5
        1 2 3 4 5

and

        1 2 3 4 5
      1 2 3 4 5
    1 2 3 4 5
  1 2 3 4 5
1 2 3 4 5

I've tried setting up a macro with visual block mode with varying degrees of success, but I'd ideally like a one liner that lets me select a block and execute it.

1 Answer 1

4

vimscript option

For the result:

1 2 3 4 5
  1 2 3 4 5
    1 2 3 4 5
      1 2 3 4 5
        1 2 3 4 5

Next "commands" solves it:

:for i in range(6)|call setline(i, repeat(' ', (i-1)*2).getline(i))|endfor

For the result:

        1 2 3 4 5
      1 2 3 4 5
    1 2 3 4 5
  1 2 3 4 5
1 2 3 4 5

Not really sure about the "rule" :)

Another option with "macro"

  1. step one, create macro

    qq:exe "normal ".((line('.')-1)*2)."I "

  2. step two apply to 2nd to the last row (visually select it first)

    '<,'>norm @q

PS, as asked in comment

to make it work for other line range apart started from line 1:

:for i in range(5,9)|call setline(i, repeat(' ', (i-1)*2).getline(i))|endfor

enter image description here

6
  • Nice. I've never heard of vim scripts before, so this is pretty cool. I also modified your range call to get the second result: for i in range(6)|call setline(i, repeat(' ', (5-i)*2).getline(i))|endfor. Only problem is the number of lines has to be hardcoded and start on the top of the file. I'll keep searching for a way to do this using visual line mode, but definitely helps in the meantime. Dec 19, 2018 at 18:31
  • Not necessary, It is possible to change it to operate on any line range.
    – Maxim Kim
    Dec 20, 2018 at 5:43
  • Could you edit your answer to show that to me? Dec 20, 2018 at 12:34
  • well, have you looked into :h range()?
    – Maxim Kim
    Dec 20, 2018 at 13:21
  • My dude, this is awesome. I didn't know about range OR :h. Can you show me how to learn about binding selections from VISUAL LINE mode? It looks like they turn into '<,'>, but I'm not sure how to search for help on those characters. Thanks again for this. Dec 20, 2018 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.