py-vterm-interaction.el/README.org

129 lines
6 KiB
Org Mode

# -*- eval: (visual-line-mode 1) -*-
#+STARTUP: showall
*This is a straight port of [[https://github.com/shg/julia-vterm.el][julia-vterm.el]].* I only modified the code slightly to deal correctly with python environments
and play nicely with =ipython=.
* python-vterm
Python-vterm provides a major-mode for an inferior Python process (or REPL) that runs in vterm, and a minor-mode that extends python-mode with the ability to interact with the inferior Python process. This is particularly useful for *fancy repls like =ipython= or =ptpython=*.
The functionalities required for typical REPL interaction have been implemented. While I would like to keep this package simple, suggestions are always welcome.
** Installation
Currently you can only install the package from github. With
~use-package~ and ~straight~ it looks as follows:
#+begin_src elisp
(use-package python-vterm
:straight (:host github :repo "vale981/python-vterm.el")
:hook (python-mode . python-vterm-mode)
:config
(setq-default python-vterm-repl-program "ipython")
(setq-default python-vterm-silent-cells t))
#+end_src
For manual installation, download =python-vterm.el= into somewhere in your local directory and use =package-install-file= command. Please make sure [[https://github.com/PythonEditorSupport/python-emacs][python-mode]] and [[https://github.com/akermu/emacs-libvterm][emacs-libvterm]] are installed and configured correctly.
Turn on =python-vterm-mode= in a =python-mode= buffer to use this package. A symbol “PY” in the mode line indicates that the python-mode buffer is ready to interact with the python-vterm REPL. Add the following line to your init file to enable =python-vterm-mode= in =python-mode= buffers automatically.
#+BEGIN_SRC emacs-lisp
(add-hook 'python-mode-hook #'python-vterm-mode)
#+END_SRC
By default, the command named =python= in your =PATH= is used. You can use a Python executable in any path by setting the =python-vterm-repl-program= variable to its absolute path. The variable can contain switches for the =python= command. For example, you can use a =python= executable at a certain path, with 4 threads enabled, by the line like the following.
#+BEGIN_SRC emacs-lisp
(setq python-vterm-repl-program "ipython -i")
#+END_SRC
** How to use
=M-x python-vterm-repl= (or =M-x python= if no other packages define it before python-vterm is loaded) opens an inferior Python REPL buffer.
In a python script buffer with =python-vterm-mode= on, you can open a Python REPL with =M-x python-vterm-switch-to-repl-buffer= (or =C-c C-z=). See below for other commands.
Both of the above operations open a REPL with the default session name =main=. You can specify a different session name by using the prefix argument =C-u=. A new session will be created and opened if there is no REPL with that session name.
You can also specify a session name by defining a file local variable =python-vterm-session= (or =python-session= if no other packages pre-define it). If the variable is defined, =C-c C-z= will open a REPL with that session name.
The package also contains a clone of [[https://docs.spyder-ide.org/3/editor.html#defining-code-cells][spyder]]'s code cells. Consider the following.
#+begin_src python
print("block 1")
# %% <optional title>
print("block 2")
# %% <optional title>
print("block 3")
#+end_src
Moving the point between any two cell markers (or between the buffer
beginning/end and a marker) and executing
~python-vterm-send-current-cell~ (=C-c C-j=) will send the contents of the
block to the REPL. When setting ~python-vterm-silent-cells~ to ~t~, the
cell content is written to a temporary file which is then executed in
the REPL with the ~%run~ magic command.
** Key bindings
*** python-vterm-mode
#+begin_example
Key Command / Description
------------------------------------------------------------------------------------------
C-c C-z python-vterm-switch-to-repl-buffer
Switch to the paired REPL buffer or to the one with a specified session name.
With prefix ARG, prompt for session name.
C-c C-c python-vterm-send-region-or-current-line
Send the content of the region if the region is active, or send the current
line.
C-c C-b python-vterm-send-buffer
Send the whole content of the script buffer to the Python REPL line by line.
C-c C-j python-vterm-send-current-cell
Send the current code "cell" to the Python REPL.
Each block is delimited by `# %% <optional name>`.
If no marker is present before the point, the cell is assumed to
begin with the buffer. Likewise, if there is no marker after the
point, the cell is assumed to end with the buffer.
C-c C-r python-vterm-send-run-buffer-file
Send a line to evaluate the buffer's file using ipython %run magic.
This is only useful when using ipython.
C-c C-d python-vterm-send-cd-to-buffer-directory
Send %cd function call to the Python REPL to change the current working
directory of REPL to the buffer's directory.
#+end_example
*** python-vterm-repl-mode
#+begin_example
Key Command / Description
------------------------------------------------------------------------------------------
C-c C-z python-vterm-repl-switch-to-script-buffer
Switch to the script buffer that is paired with the current Python REPL buffer.
M-k python-vterm-repl-clear-buffer
Clear the content of the Python REPL buffer.
C-c C-t python-vterm-repl-copy-mode
Enter copy mode.
#+end_example
*** python-vterm-repl-mode (copy mode)
#+begin_example
Key Command / Description
------------------------------------------------------------------------------------------
C-c C-t python-vterm-repl-copy-mode
Exit copy mode.
<return> python-vterm-repl-copy-mode-done
Copy the region to the kill ring and exit copy mode.
C-c C-r vterm-reset-cursor-point
Call the vterm command that moves point to where it should be.
#+end_example