How can one quickly remove ancillary snippets of code in Python scripts under Emacs?

For example, for debugging purposes, I often add some print statements to print out the values of some variables. When I finish coding, I would like to remove the print statements in a quicker way than manually selecting and deleting one by one.


  • 2
    Even if you are not using git for your project, you can git init and create a commit for state of files to which you want to revert to. Then make the debugging related changes. When done, discard the changes to revert to the last commit. This whole process is very easy using magit. Mar 19 '15 at 16:05
  • but usually after adding those print and debugging, the code will be corrected or improved, if then revert back to the state before adding print, the correction to the code will be lost.
    – Tim
    Mar 19 '15 at 16:07
  • I usually use the git solution or this other solution (which has nothing to do with emacs). I use a global variable called something like my_debug and wrap the debug statements inside "if (my_debug==1)" (psuedo-code). When I am debugging I set that var to 1 else keep it 0. Mar 19 '15 at 17:01
  • A Python solution (rather than an Emacs one) is to use the logging module and print your debug output with logger.dubug("mesage"). The debug messages will only appear if the logging level is set to DEBUG, so you can easily toggle them.
    – erikstokes
    Mar 20 '15 at 0:20

Mark the region where your print statements are and perform this:

M-xflush-lines ^ *print.* enter

Which will delete all the lines in your region that match a line that has zero or more spaces followed by the word print, followed by anything.

You can wrap this up in a function if you'd like:

(defun delete-print-lines (beg end)
  "Delete all lines in the selected region that start with the word print."
  (interactive "r")
  (flush-lines "^ *print.*" beg end))

I usually do this using multiple-cursors by selecting the give languages print function and using mc/mark-next-like-this to select a bunch of them and deleting each line.

Similar to what Kaushalmodi was describing, if you are using git, and magit, you can leave them in and individually revert each printing line from your changes from the status buffer, or even by using ediff.

There are really countless ways to do this, you just need to find the one that fits you.

  • I was thinking along the same lines but what if the (message ..) is multiple lines long? Might need something like (looking-back "( *message".. followed by mark-sexp and then delete? Mar 19 '15 at 17:30
  • 1
    In that case I'd use multiple-cursors, I'd mark all messages then move use sexp based functions to mark then entire message expressions across lines, and delete them all. This would also be a great time to use the expand-region package. Mar 19 '15 at 18:24
  • Multiple cursors and expand region in action: i.imgur.com/VBdJbWc.gif Mar 19 '15 at 18:30

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.