117
votes

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

3
was going to ask similar question. thanks :Dalamin
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

3 Answers

138
votes

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.

139
votes

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.

0
votes

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