I'm currently building my dotfiles and I want to list all TODO: ... comments in my project's directory, I created a bash alias to do this:

 alias mytodo='grep --recursive "TODO: "'

It works well, however, it also returns the alias definition. this is the sample output.

 devs@dotfiles$ mytodo

 bash/profile.d/aliases/: alias mytodo='grep --recursive "TODO: "'
 bin/git_branch_status/: # TODO: Add checking of remote branch status.
 tools/setup/: # TODO: Add search for existing symlinks.

how can I specifically exclude that line with the alias definition?

  • 1
    Consider also —exclude=glob as a way to exclude files named alias – Jeff Schaller Sep 20 '18 at 12:02
  • @JeffSchaller i don't want to exclude the file as I may write todo comments in it, I just wanna exclude that specific line with the alias definition. – cevhyruz Sep 20 '18 at 16:39

One way to prevent a regexp from matching itself is to enclose a single character in a character class:

alias mytodo='grep --recursive "TOD[O]: "'

Alternatively (hat-tip to Stéphane Chazelas), you could save the alias with extra quote-marks in the pattern:

alias mytodo='grep --recursive "TO''DO: "'

Then the line still won't be found, but the alias contains the original pattern. This is helpful when you want to use eg. a fixed-string match (grep -F).

  • it works now! Thanks. I will accept this as the answer.. – cevhyruz Sep 20 '18 at 9:40
  • 2
    Or alias mytodo='grep --recursive "TO''DO: "' which still creates a grep --recursive "TODO: " alias. – Stéphane Chazelas Sep 20 '18 at 10:24
  • 4
    Most used case: ps <some-option> | grep "[p]rocess" to avoid showing the grep process every time – Giacomo Alzetta Sep 20 '18 at 13:14

You can simply pipe it into grep -v 'alias' to get all lines which don't contain alias.

  • it works with grep -v 'alias' | grep -r 'TODO: ' but for some reason it messed up the colors of the grep output.. any idea why? – cevhyruz Sep 20 '18 at 9:34
  • @JohnFredFadrigalan Do you already have a grep alias? That's typically where the color option is set. – JigglyNaga Sep 20 '18 at 9:38
  • I already aliased the grep with --color=auto so i think its because of the piping.. I tried to reverse the order of the command and it works with colored output but it freezes. – cevhyruz Sep 20 '18 at 9:47

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.