vimvim-macros

How can you create a vim macro that reorders lines?


Let's start off with the text

1 The
2 Quick
3 Brown
4 Fox
5 Jumps
6 Over
7 The
8 Lazy
9 Dog

Let's then say that you want to make the first line the last line repeatedly with a macro. That is, this is the goal state after 1 run

1 Quick
2 Brown
3 Fox
4 Jumps
5 Over
6 The
7 Lazy
8 Dog
9 The

Use case : I want to apply a longer macro with the word The the first time, Quick the 2nd time, etc.

The naive approach works exactly once : q11Gdd8Gpq

@1 <- This works

@1 <- This breaks

This breaks when repeated. I've tried other approaches which avoid dd (e.g. making a new line above the 1st line, d1j, returning to the 8th line, paste, J to join lines). Everything I try works when run once, but something is changing the macro buffer during this run.

How do you make a macro that does this that can be run multiple times?


Solution

  • This page has the answer, https://vim.fandom.com/wiki/Moving_lines_up_or_down

    Outside my specific application (thanks @Amadan in the comments) this is

    q1:1m$<cr>q
    

    For me, where I am rotating items in a list with contents after the list, the entire solution ended up being

    q1:1m8<cr>q
    

    However for the problem as stated, $ rather than a line number is correct.