1

I'm very new to programming in Elisp so maybe the mistake is there but I'm completely stuck right now.

I wanted to make a small, simple Elisp function to remove the AUTOs from my Verilog files so I don't commit those lines to our git repository since the rest of my team doesn't use Emacs. I thought this would be a very simple task but for the life of me, I can't get flush-lines to work properly.

The regular expression I'm trying to use is \/\*AUTO\w+\*/. When I do M-x flush-lines RET \/\*AUTO\w+\*/ RET this works. So I would think the regular expression works. But when I try to do it in either my .emacs or by doing M-: (flush-lines "\/\*AUTO\w+\*/" nil (buffer-size) t) RET, it just outputs a nil. I also tried (flush-lines "\/\*AUTO\w+\*/") and (flush-lines "\/\*AUTO\w+\*/" nil (buffer-size)) and they both just output nil and don't remove the matching lines. My point is set to the top of the buffer, so I don't understand what the issue is. I can't seem to find anywhere any resources that could point me in the right direction. The closest I found was this post from a couple years ago but it goes far beyond the simple task I'm trying to do, and I seem to be suffering from a different issue than that user was.

Drew
  • 75,699
  • 9
  • 109
  • 225
Rice
  • 13
  • 3
  • 2
    After successfully completing your interactive command, you can try `M-x command-history` to see what Emacs thinks you did in Elisp terms. In your case, it shows the command as `(flush-lines "\\/\\*AUTO\\w+\\*/" nil nil t)` – gregoryg Aug 12 '20 at 17:39
  • https://emacs.stackexchange.com/tags/elisp/info – Drew Aug 12 '20 at 17:41
  • 1
    @gregoryg: Please consider developing your comment into an answer. Bonus points for linking to the doc (about doubling backslashes in Lisp). – Drew Aug 12 '20 at 17:44
  • 1
    Does this answer your question? [re-search-forward Regex behavior for OR boolean](https://emacs.stackexchange.com/questions/55601/re-search-forward-regex-behavior-for-or-boolean) – Drew Aug 12 '20 at 17:47

1 Answers1

2

The syntax for escaping elements of regular expressions can be confusing.

Once you find an interactive solution that works, a handy tip is to look at Emacs' command history:

M-x command-history

With the command given in the question, the command history shows:

(flush-lines "\\/\\*AUTO\\w+\\*/" nil nil t)

That gives the Elisp that will work. The reason all the extra escaping is necessary can be found in the following doc: Special Characters in Regular Expressions

The character \ has special meaning both in regexp and in the Lisp reader.

gregoryg
  • 915
  • 6
  • 8