133

Markdown allows for embedded code. How can this be done in org-mode?

I know about source-code blocks:

#+begin_example
blah-blah
#+end_example

But what I want is something like this (obviously, with the right syntax, which I do not know):

This is `embeded code`.

Can this be done in org-mode? Not possible to find that in the documentation ...

2
  • 1
    was going to ask similar question. thanks :D
    – alamin
    May 29, 2017 at 3:46
  • In markdown we can make a code block by a enclosing lines of code in two lines of `````, which is quite neat IMO. Is there a neat way to do this in org-mode? I don't want to see +BEGIN_SRC... everytime.
    – Student
    Oct 18, 2019 at 12:00

3 Answers 3

162

You can enclose the text within = or ~ signs to have it typeset in monospaced font and export it verbatim (which means it is not processed for org-specific syntax):

This is =verbatim text= or ~code~.

You'll find all information about org-mode markup elements in the relevant section of the manual.

5
  • 5
    Thanks. Having the keywords is being half-way there. If I had known that org-mode talks about this in terms of monospace and emphasis I would have found this myself. :( I was looking for inline and embedded, to no avail.
    – blueFast
    Apr 24, 2013 at 9:02
  • Yes, I recall having been disturbed by the "emphasis" term myself too, when looking for a feature allowing to hide org markup. Once you know the terminology, you immediately find org-hide-emphasis-markers, but I've searched for ages before finally finding it almost accidentally... Apr 24, 2013 at 10:22
  • 7
    actually the manual just says ~code~ i.e. surrounded with ~.
    – squid
    Aug 5, 2015 at 7:48
  • 1
    This answer just makes code look monospaced, it doesn't actually endow it with the executable properties a real BEGIN_SRC block has. @Tom's answer below is better if you want that. Sep 30, 2015 at 20:15
  • 4
    I found ~ ugly. Is there a workaround to use the grave accent instead? Dec 31, 2019 at 9:59
162

While monospaced is good enough for most cases, inline code blocks have the form src_LANG[headers]{your code}. For example, src_xml[:exports code]{<tag>text</tag>}.

Edit: Code highlighting of inline code is certainly possible, albeit with patching org.el itself: The answer given here https://stackoverflow.com/a/20652913/594138 works as advertised, turning

- Inline code src_sh[:exports code]{echo -e "test"}

Into

enter image description here

in html-export. And the winning answer in this post, https://stackoverflow.com/a/28059832/594138, achieves the same without the need to patch org.el, but you will have to adapt it if you don't like the optics during editing.

6
  • 7
    This is the best answer here, I think. The relevant part in the manual is 14.1 Structure of code blocks. Apr 7, 2014 at 6:43
  • 1
    And the manual says things like “live code blocks require...” What does "live" mean here? Also, I whish the docs had more examples instead of focusing mainly on formal syntax definitions (but that is not a problem with org-mode only; it is more or less a general issue in my opinion). Aug 11, 2016 at 14:28
  • Live code blocks can be evaluated in org-mode (or during export) and their results get inserted into the file. I use this for UML diagrams with plant-uml and sometimes with Clojure code blocks.
    – user73774
    Oct 10, 2016 at 17:55
  • 4
    Nice, but we still can't get syntax highlighting this way, can we? And I find it strange that [:exports code] is required, because the docs at orgmode.org/manual/exports.html#exports say that it is the default. (But without it, nothing gets exported when I export the org contents to markdown, for example.) Sep 12, 2017 at 20:38
  • I don't think of this as 'inline code', but rather a 'code block'. For example, I think of inline code as a file name, a short (< 1 line) code string, usually embedded in other text.
    – James
    Jan 21, 2019 at 16:44
1

I wrote a function which I hope will be useful to help manage the code inline.

  1. You put this code in your init file
(defun org-insert-inline-code()
  "This function insert inline code `src_lang{inline code}' \nYour buffer must contain  '#+PROPERTY: header-args:lang    :exports code' where `lang` can be python or an other programming language."
  (interactive  (if (use-region-p)
            (progn
              (setq start (region-beginning))
              (setq end (region-end))
              (goto-char start)
                      (if (re-search-backward "^#\\+PROPERTY: header-args:[^[:blank:]]*" 1 t 1)
                          (progn
                            (forward-char 24)
                            (setq org-inline-lang (word-at-point))
                    (goto-char start)
                    (insert (concat "src_" org-inline-lang "{"))
                    (goto-char (+ 11 end))
                    (insert "}")                            
                            )))
          (progn
                    (setq start (point))
                    (if (re-search-backward "^#\\+PROPERTY: header-args:[^[:blank:]]*" 1 t 1)
                        (progn
                          (forward-char 24)
                          (setq org-inline-lang (word-at-point))
                  (goto-char start)
                  (insert (concat "src_" org-inline-lang "{} "))
                  (backward-char 2)
                            ))))))

(define-key org-mode-map (kbd "C-M-,") 'org-insert-inline-code)
  1. You put this kind of PROPERTY in the org-file
#+PROPERTY: header-args:python    :exports code

The required [:exports code] is given that way and the programming language can be identify by the function too.

  1. Insert the code in line with C-M-, (the function then search back to read the language in the PROPERTY line and insert the correct command).

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.