135

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 ...

blueFast
  • 41,341
  • 63
  • 198
  • 344
  • 1
    was going to ask similar question. thanks :D – alamin May 29 '17 at 03: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 '19 at 12:00

3 Answers3

168

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.

Lou Zell
  • 5,255
  • 3
  • 28
  • 23
François Févotte
  • 19,520
  • 4
  • 51
  • 74
  • 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 '13 at 09: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... – François Févotte Apr 24 '13 at 10:22
  • 7
    actually the manual just says ~code~ i.e. surrounded with ~. – squid Aug 05 '15 at 07: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. – Ken Williams Sep 30 '15 at 20:15
  • 4
    I found ~ ugly. Is there a workaround to use the grave accent instead? – Joseph Tesfaye Dec 31 '19 at 09:59
164

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.

Tom Regner
  • 6,856
  • 4
  • 32
  • 47
  • 7
    This is the best answer here, I think. The relevant part in the manual is [14.1 Structure of code blocks](http://orgmode.org/manual/Structure-of-code-blocks.html). – Arne Babenhauserheide Apr 07 '14 at 06: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). – Fernando Basso Aug 11 '16 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. –  Oct 10 '16 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 http://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.) – Supernormal Sep 12 '17 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 '19 at 16:44
  • @james: that's exactly what this is about, code blocks have a different syntax altogether. – Tom Regner Feb 18 '19 at 11:48
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).
Raoul HATTERER
  • 522
  • 5
  • 5