3

I try to use org-mode as a replacement for jupyter notebook, and I was faced with a problem: when I edit a python src block, newlines don't indent properly, so I have to press space 4 times after pressing RET.

I checked how the indentation works in elisp src blocks, it also works strange, see gif:

My org-mode config:

(use-package org
  :ensure nil
  :config
  ; (org-babel-jupyter-override-src-block "python")
  (add-hook 'org-babel-after-execute-hook 'org-redisplay-inline-images)
  (add-to-list 'org-structure-template-alist '("p" . "src python"))
  :custom
  (org-startup-indented t)
  (org-startup-with-inline-images t)
  (org-src-tab-acts-natively t)
  (org-src-preserve-indentation nil)
  (org-edit-src-content-indentation 4)
  (org-src-window-setup 'current-window)
  (org-babel-load-languages '((emacs-lisp . t) (python . t))))

(use-package org-superstar
  :hook (org-mode . (lambda () (org-superstar-mode 1))))

(use-package evil-org
  :after (org evil)
  :hook ((org-mode . evil-org-mode)
     (evil-org-mode . (lambda ()
                (evil-org-set-key-theme
                 '(navigation insert textobjects additional calendar))))))

1 Answer 1

3

Enter the block before editing it

To get proper indentation and other language features, you have to first enter the OrgSrc block with org-edit-special, which is by default bound to C-c '. See The Org Manual: Editing Source Code.

Also note you don’t have to type the #+begin_src and #+end_src manually, you can type <s then press TAB. See The Org Manual: Structure Templates.

Editing inline

If you want to be able to edit code blocks inline while having language features, you may want to look at this answer by malcook, which suggests it is possible with Polymode poly-org-mode. I haven’t tried this myself.

4
  • > You have to first enter the OrgSrc block with org-edit-special, which is by default bound to C-c '. Of course I can, but I don't want to do it always. It is like to open a text editor every time I edit a cell in Jupyter Notebook (certanly, it's faster but annoying anyway).
    – Alex
    Commented Apr 21, 2022 at 11:30
  • As I know it, there is a setting org-src-tab-acts-natively. I thought that it do what I want, but it looks like it don't.
    – Alex
    Commented Apr 21, 2022 at 11:34
  • It allows you to press TAB instead of entering 4 spaces manually, but that is all as far as I know
    – Preminster
    Commented Apr 21, 2022 at 11:35
  • 1
    Thank you! I've just tried polymode + poly-org. The indentation works well, but an another problem emerged: the syntax highlighting doesn't work. Maybe I'll make an issue on poly-org github page.
    – Alex
    Commented Apr 21, 2022 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.