Let's say I have ordered TODO list with priorities, sorted already with :sort n similar to :

1. shopping
3. cooking.
D 5. car repair.
D 11. tap fix.
12. project start

D means done.

I am using :sort n function to sort this list, but all done (D) things should go at the end of list. Result should be like this:

1. shopping
3. cooking
12. project start. 
D 5. car repair. 
D 11. tap fix.

How to do that?

2 Answers 2


Try adding this to your .vimrc:

nnoremap <leader>td :sort n<CR>:sort n /[D]/<CR>

Then in Normal mode, you can type \td (assuming you haven't changed the default leader key to something else) to sort your to-do list.

  • this solution gives me error message when I type it in normal mode: E10: \ should be followed by /,? or &
    – lluke
    Commented Feb 15, 2018 at 7:05
  • Did you type in :sort n, Enter, :sort n /[D]/, Enter exactly? If you add my line above exactly to .vimrc, it can be done in a couple keystrokes rather than typing out both lines.
    – Kevin
    Commented Feb 15, 2018 at 7:23
  • yea, it is working, very fast solution :)
    – lluke
    Commented Feb 16, 2018 at 20:59

If your list is already sorted with :sort n, you can move the done D items to the bottom of the list with a global command:


This works through the file from top to bottom, moving each line that starts with a D. Because the D lines are already sorted when you run the command, they remain sorted after it.

If you want to perform both steps in a single go, you could use a mapping similar to the one suggested by Kevin or perhaps a custom command:

" Run this command with :TodoSort
:command! TodoSort sort n | g/^D/m$

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.