1

In markdownmode, or while asking questions in stackoverflow, when we do,

`<code>`

or

```<code>``` 

The background become changes into gray.

Would it be possible to apply same in orgmode, where it also change background color or change its forecolor?


Example: ![enter image description here

5
  • M-x describe-face org-block and customize at will (but I have not tested anything).
    – NickD
    Aug 17, 2021 at 14:39
  • If you wanted some inline code highlighting, you might define a face (defface my-code-face '((t :foreground "black" :background "gray")) "Your code face.") and then use font-lock-keywords (font-lock-add-keywords 'org-mode '(("```.+```" . 'my-code-face))) - someone with more regexp experience in emacs would have to help me out for a regexp over multiple lines. You might also be able to do this with org emphasis markers. Aug 17, 2021 at 15:52
  • A few tweaks might make it more similar to your example: (defface my-code-face '((t :foreground "aquamarine")) "Your face.") and (font-lock-add-keywords 'org-mode '(("`+\\(.+?\\)`+" 1 'my-code-face))) Also check out: emacswiki.org/emacs/AddKeywords Aug 17, 2021 at 16:20
  • @kozina-adjacent if works for ```hellow``` but it does not work when there is new line after ```. It detect ` in between ` ` and does not check further
    – alper
    Aug 17, 2021 at 16:29
  • @alper correct - it's the multiline regexp that's the issue for me. Maybe someone with some more regexp under their belt can help us out. But see this, cause it might be a little bit more difficult than my simple code can handle: gnu.org/software/emacs/manual/html_node/elisp/… Aug 17, 2021 at 18:50

1 Answer 1

1

You can add support to highlight fenced code blocks exactly like in Markdown mode, as several comments have already suggested. However, if you're using orgmode already, you have far more powerful and flexible features already built in.

Orgmode comes with markup for code blocks:

* My Org File

** Code blocks
   
#+begin_src elisp
  (defun my-function ()
  "This is my special function."
    (interactive)
    (message "Hello from Emacs"))
#+end_src

By default, the text in the src block has syntax highlighting applied according the code language of the block, in this case it's elisp:

enter image description here

You can modify the way this appears with themes, or by directly modifying the faces (org-block being one of the basic ones).

Similarly, inline code snippets in org mode are marked up as:

Here's a bit of inline code: ~x = x + 1~

The default colouring for this is:

enter image description here

This just scratches the surface. Org-mode provides a lot of features for working with inline code, and especially code blocks. If you're writing documents with included code snippets, you should consider using the features org mode already provides.

Working with Source Code (org manual)
Markup for inline code (org manual)

1
  • Can ``` be alias for #+begin_src and #+begin_end. Like I was writing my questions in org-mode and copy them into stackoverflow to as questions. I was just trying to find a way both formats can detect the coding format.
    – alper
    Aug 21, 2021 at 11:59

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.