0

Suppose, we have a buffer with Python code in it. I want to evaluate it, switch to Python buffer and see the results. Then easily switch back to code buffer.

I tried the following:

(defun my-python-mode ()
  (local-set-key (kbd "<kp-insert>")
    (lambda () (interactive)
      (elpy-shell-send-buffer-and-go)
      (elpy-shell-switch-to-shell-in-current-window)
      (let ((py-buffer (get-buffer "*Python*")))
        (message (buffer-name py-buffer))
        (if py-buffer
          (progn
  ;          (other-window 1)
            (switch-to-buffer py-buffer)
            (end-of-buffer))))))))

I added a callback to kp-insert but my function doesn't switch to python buffer when I click kp-insert. If there is no python buffer previously, it runs python interpreter, but doesn't switch to it.

(add-hook 'inferior-python-mode-hook
  (lambda ()
    (local-set-key (kbd "<kp-add>")
      lambda () (interactive)
      message "aaa")))

I tried this to switch back from interpreter, but it doesn't work.

How can I set up an ergonomic python environment in Emacs?

Update 1

I created a function that splits the window, runs the code and switches to Python buffer. Unfortunately, when there is no Python buffer, it just splits the window, but doesn't switch to buffer and doesn't send my code to Python. So I have to click the key twice. Is it possible to fix this?

(defun aaa ()
  (interactive)
  (if (= (length (window-list)) 1)
    (split-window-right))

  (elpy-shell-send-buffer-and-step)
  (let ((py-buffer (get-buffer "*Python*")))
    (other-window 1)
    (if py-buffer
      (progn
        (switch-to-buffer py-buffer)
        (end-of-buffer)
        ))))
1

Suppose, we have a buffer with Python code in it. I want to evaluate it, switch to Python buffer and see the results. Then easily switch back to code buffer.

It's unclear whether you're using a split window. I suggest doing that if you aren't (e.g. split-window-right). You can then use other-window (C-x o) to switch between windows. There are tons of packages to facilitate switching between windows (e.g. ace-window).

For sending Python to the interpreter, I suggest using elpy-shell-send-statement-and-step. This will keep your point within the Python source code buffer. If your window is split with the Python interpreter in the adjacent window, you will see the output.

How can I set up an ergonomic python environment in Emacs?

This is a super broad question. There are tons of Python related packages out there. You can check MELPA or browse Github. Personally, I just use elpy and roll the rest myself.

For example, I use this function to switch to the most recently used window:

(defun my-switch-to-last-window ()
  "Switch to the most recently used window.

See URL `https://emacs.stackexchange.com/a/7411/15177'"
  (interactive)
  (let ((win (get-mru-window t t t)))
    (unless win (error "Last window not found"))
    (let ((frame (window-frame win)))
      (raise-frame frame)
      (select-frame frame)
      (select-window win))))
2
  • I did some experimentation today and created a function that sort of works, but there are minor issues. Can you take a look at update 1? – user4035 Jan 31 at 21:34
  • I don't think this is the forum for these types of exchanges (I'm not sure how to enable chat and unfortunately don't have time at the moment to). At a quick glance, I suggest thinking about the order if execution and, specifically, when are things created. elpy-shell-send-buffer-and-step will create a *Python* buffer, but there's no guarantee it will exist when you try to get it. I notice there exists elpy-get-or-create-process which takes a delay parameter to ensure startup. These are not thoroughly thought out suggestions, merely nudges. Hopefully they get you un-stuck :) – Lorem Ipsum Feb 2 at 15:39

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.