4

I use vim to write my to-do list in markdown format.
To mark a certain task "done" I just make it strikethrough with two tildes(~).

Is there a way to automatically move all strikethrough text to a different section(#Things done, for example)?

And also is there a way to add a date to each task done?

5

Moving completed tasks

Presuming that a done line looks like this:

- ~A completed task~

Then you can do so with a :global command that performs a :move:

:g/^- \~.*\~$/m/^# Things done/+1
:g                                  # find lines that match
  /^- \~.*\~$/                      # the regular expression /^- \~.*\~$/
              m                     # and move them below
                               +1   # one line below
               /^# Things done/     # the line that matches /# Things done/

The regular expression:

/^- \~.*\~$/
 ^            # the start of the line
  -           # a markdown list item
    \~        # a tilde
      .*      # anything
        \~    # another tilde
          $   # the end of the line

You're probably not going to want to type this out every time, so why not make it into a mapping:

:nnoremap <leader>c :g/^- \~.*\~$/m/^# Things done/+1<CR>

Now you can move completed items just by pressing \c.

Appending the time

You can use a similar command to append the current date:

:g/^- \~.*\~$/call setline('.', getline('.') . ' ' . strftime('%x'))

This works very similarly, but instead of moving the lines, it appends the current date to them. (See :help strftime, :help getline, and :help setline.)

Doing both at once

If you don't want two separate mappings, you can wrap them in a function:

function CompleteTasks() abort
  g/^- \~.*\~$/m/^# Things done/+1
  g/^- \~.*\~$/call setline('.', getline('.') . ' ' . strftime('%c'))
endfunction

nnoremap <leader>c :call CompleteTasks()<CR>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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