Search code examples
vimvim-plugin

How to make Vim give a warning or popup when opening a file with "TODO" comments


Here are two scenarios which make this feature useful:

  • Scenario 1: We have some unfinished code which might be more than 200 lines. The unfinished code can run but only give wrong results. After reopen the file days later, it is easy to forget the unfinished work, get undesired results and waste time debugging.
  • Scenario 2: We write some temporary code which might be valid only for test phase. For example, save a matrix as image file for debugging. It is also easy to forget to comment the debugging code and run the script without changing the code back to normal.

If we put TODOs among the attention-needed code, it would be great that Vim gives some popups or warnings when it opens a file with these TODOs. I am quite a novice on Vim. Could anyone give some help? Thank you!


Solution

  • You could listen for the event BufReadPost where you should search() for "TODO", and there generate a message. Either with :echomsg or :call confirm(), if you're using gvim.

    " untested
    aug notify_TODO
      au!
      au BufReadPost * if search('TODO') > 0 | call confirm('Need to fix this', '&OK', 1, "warning") | endif
    aug END
    

    You could also populate the quickfix list with all occurrences of "TODO".

    :call setqflist(filter(map(getline(1,'$'), '{"bufnr": '.bufnr("%").', "lnum": v:val =~ "TODO" ? v:key : -1, "text": v:val}'), 'v:val.lnum>0'))
    :copen
    

    (best put in a function)

    Or simply check for TODO in every, or specific, files with :vimgrep.