Preserve Formatting in Org Mode
This document explains how to preserve formatting for code like when literal examples are used inside Org Mode ,i.e they should not be subjected to markup. How i used this feature is explained in a later section of this page.
Displaying source code
An example without specifying langugage appears as follows:
Some example from a text file.
If the source code is from a Programming language , the usual facilities offerred by Emacs like Font Lock are applied, so that the example looks like fontified emacs buffer. One needs to mention the major Mode.
(defun org-xor (a b)
"Exclusive or."
(if a (not b) b))
(defun my/doasedit ()
(interactive)
(if (buffer-file-name)
(let ((cursor-pos (point))
(truename (file-truename (buffer-file-name))))
(find-alternate-file (concat "/doas::" truename))
(goto-char cursor-pos))
(message "This buffer is not visiting a file!")))
Because the ‘#+BEGIN’ … ‘#+END’ patterns need to be added so often, a shortcut is provided C-c C-,.
Structure templates can be used to define additional shortcuts for needed languages. An example can be found in the config file .
The electric-indent-mode needs to be disabled to avoid interfering with src blocks as shown below.
Editing the quoted source code
The source code can be edited using the shortcut “C-c ‘” (org-edit-special) to open src block in dedicated buffer. Once edited use the same shortcut “C-c ‘” to close.
C-c ’ (org-edit-special)
Source: https://orgmode.org/manual/Literal-Examples.html↗
Steps to move an init.el file to org mode
Here are the steps to migrate a pre-existing init.el file to org mode version .
- Copied my original init.el to an org mode file.
- Define filename where org-babel must tangle the file to as shown
below just below the property drawer in Org Roam
. I started with a
test file.
#+PROPERTY: header-args:emacs-lisp :tangle /tmp/test-init.el :lexical t - Added the following code-snippet to let org-babel tangle the file.
(when (string-equal buffer-file-name (expand-file-name "20260510115218-emacs_config_file.org" org-roam-directory)) (add-hook 'after-save-hook #'org-babel-tangle nil t)) - Add necessary formatting as explained in the above sections for few sections first. Check whether the export to test file works for few exported sections first.
- Do repeated testing with diff between actual init.el and test-init.el until no code-snippet is lost.
- In my case, all the previous outshine
headlines at ;;; and ;;;;
levels were moved to * and ** in Org Mode
. For example, the below
becomes in org mode.;;; Initialization ;;;; files and folders* Initialization ** files and folders - When the code snippets are inside a Org
file, the default
electric-indent-mode needs to be disabled in Org Mode
using a
hook. Othewise, every change and enter will keep moving the code
inside the block.
(add-hook 'org-mode-hook (lambda () (electric-indent-local-mode -1))) - Some useful commands to check the progress are listed below:
$ diff /tmp/test-init.el ~/.config/emacs/init.el > /tmp/diff_output.txt $ diff .config/emacs/init.el /data/myhome/prabu/org/Resources/20260510115218-emacs_config_file.org - Once all the testing is over, then change to the actual filename
after taking backup of the original file:
#+PROPERTY: header-args:emacs-lisp :tangle ~/.config/emacs/init.el :lexical t
© Prabu Anand K 2020-2026