2

I use vim for my todo list:

* [ ] A
    * [ ] B
    * [X] C
    * [ ] D
* [ ] E
    * [X] F
    * [X] G
    * [ ] H

In order to get the completed tasks out of the way, I can use :g/\[X\]/m$, which moves every task with [X] to the bottom of the list:

* [ ] A
    * [ ] B
    * [ ] D
* [ ] E
    * [ ] H
    * [X] C
    * [X] F
    * [X] G

It breaks the tree structure, though (C should still be in the A branch).

I'd like to move the completed tasks to the bottom of the current selection (e.g. line 1-4, in bold):

* [ ] 𝗔
    * [ ] 𝗕
    * [ ] 𝗗
    * [𝗫] 𝗖
* [ ] E
    * [X] F
    * [X] G
    * [ ] H

I tried :

:'<,'>g/\[X\]/m\`>

and

:'<,'>g/\[X\]/mo

but got "Invalid address".

I can specify a line number (:'<,'>g/\[X\]/m4), but I would like to find a command without hard-coded line number.

Is there any way to use :g to move lines to the end of the current selection?

Alternatively, it would be great to move matching lines to the last line with the same indent, but I have no idea at all how it could be written.

Note: the tree can have more than 2 levels, and once every leaf of a branch is completed, the branch itself is marked as completed (by vimwiki):

* [ ] A
    * [ ] B
    * [X] C
        * [X] D
        * [X] E
* [X] F
    * [X] G
    * [X] H
* [ ] I
* [X] J
2
  • Weird, :'<,'>g/\[X\]/m\'> seems to do what I want, even though `> and '> appear to be synonym according to :help `>. Apr 30, 2022 at 20:02
  • @Quasímodo: The tree can have more than 2 levels, and once every leaf of a branch is completed, the branch itself is marked as completed. I've added a small example. Apr 30, 2022 at 20:44

1 Answer 1

2

:'<,'>global/\M[X]/move '> worked for me. There is a difference between the use of backtick and ' in normal mode and between their use for an {address} (see :help :move, which links to :help {address}, where marks must use ').

3
  • Thank you very much for the answer. I'm not sure how to use the long command. Using it with the last example in my question, I get E554: Syntax error in \{...} E870: (NFA regexp) Error reading repetition limits E554: Syntax error in \{...}" I started /usr/bin/vim -u NONE, to make sure no plugin is started. May 2, 2022 at 18:01
  • :'<,'>global/\M[X]/move '> works fine, and answers the question in the title. May 2, 2022 at 18:03
  • 1
    Hm, it’s possible the command failed for lines whose indent is 0, which plugs in as -1 repetition. Didnt test that. I may just remove that section @EricDuminil
    – D. Ben Knoble
    May 2, 2022 at 19:23

Your Answer

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

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