1

In Python mode (python.el), I am in the habit of hitting C-j to return and indent properly. However, when I am in an inferior python buffer, this command only inserts a newline and does not indent. Is there any way to get the inferior-python-mode to work more like python-mode in this respect, or maybe even set up auto-indent the way it normally works in ipython? I have read through the documentation for inferior-python-mode, but can't find anything useful. I love using Emacs for Python, but there are a few pain points, and I am having trouble finding good documentation. Thanks in advance for any advice!

1

The problem is that newline-and-indent (the function that C-j is bound to) inserts a literal newline then whatever would be inserted by TAB. What you want is to send the current input:

(defun send-input-and-indent ()
  (interactive)
  (comint-send-input)
  (indent-for-tab-command))

Next, bind send-input-and-indent to C-j only when in inferior-python-mode:

(define-key inferior-python-mode-map (kbd "C-j") 'send-input-and-indent)

Put both snippets in your initialization file and you're good to go.

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.