0

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?

Jessica Pennell
  • 578
  • 4
  • 14

2 Answers2

1

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.

Jessica Pennell
  • 578
  • 4
  • 14
  • 1
    More compactly, and more generally: `q1:1m$q` (move line 1 to end). There is no need to move the cursor (`gg`) if you address the line; and `$` works better as the target because you want to avoid hardcoding where the end of file is. – Amadan Sep 17 '19 at 06:41
  • 1
    Thanks a ton :) I edited that in to my answer and credited you – Jessica Pennell Sep 18 '19 at 14:59
  • 1
    Instead of the macro: `:g/./m$` – D. Ben Knoble Sep 18 '19 at 15:53
  • @D.BenKnoble if you would like to make that an official answer I'd be happy to upvote it. The situation I came up with was a contrived situation demonstrating a limitation of macros. But your approach definitely does demonstrate another way to solve the same problem and I feel it would add value when people searched for this. – Jessica Pennell Sep 18 '19 at 16:48
1

This situation does not require a macro: the common idiom is

:global/^/move $
D. Ben Knoble
  • 4,273
  • 1
  • 20
  • 38