I am loving my elpy setup in emacs, but I have one question. When I put in a ipdb.set_trace() for the ipython shell I want to send code snippets to the iPython shell. Right now my workaround is using the isend minor mode to highlight my code and send it to the iPython shell. The problem is that I kept getting IndentationError. Now, I can just put a %cpaste and -- before and after my highlighted code and hit control-enter to send it to the ipython shell and run it.

My question is, is there a way to write a wrapper around the isend command so that it automatically adds the %cpaste in front of the highlighted code, and a -- after it when sending it to the iPython shell?

I think it might even be a cool feature to add to the elpy package.


Since you seem to already use isend, you might be interested to know that it supports ipython's %cpaste...-- construct out of the box:

(add-hook 'isend-mode-hook 'isend-default-ipython-setup)

With this setup, every region that you send will automatically be wrapped in a %cpaste/-- block. Sending single lines (i.e. when no region is active) will not be affected.


This isn't a complete solution but I think it is related and might help... This comes from https://github.com/jorgenschaefer/elpy/issues/191#issuecomment-42474850 and is a work-around to make IPython work with Org-mode.

  (defadvice org-babel-python-evaluate
    (around org-python-use-cpaste
            (session body &optional result-type result-params preamble) activate)
    "Add a %cpaste and '--' to the body, so that ipython does the right thing."
    (setq body (concat "%cpaste -q\n" body "\n--"))
    (if (stringp ad-return-value)
        (setq ad-return-value (replace-regexp-in-string "\\(^Pasting code; enter '--' alone on the line to stop or use Ctrl-D\.[\r\n]:*\\)" ""
  • Yeah, I came across that, but to be honest I can't follow the function. I did however find that i-send supports wrapping Python code in the %cpaste and -- – statmobile Jan 24 '15 at 21:56

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.