1

You can add inline code and verbatim text in Org-mode using this syntax:

~<code>cd ~~</code>~

=verbatim /text/=

This is expected to display:

<code>cd ~</code>

verbatim /text/

If you copy the content in the Org-mode buffer and paste it into any other program, the content is pasted with the full markup.

Is it possible to copy and paste only what is displayed and not the markup?

1 Answers1

2

You can use the ASCII exporter if you customize the option org-ascii-verbatim-format to %s.
Export to a buffer and copy the code there.

The following command sets the options temporarily for you as needed and then exports directly into the kill-ring:

(defun org+-copy-code ()
  "Export code silently to kill-ring."
  (interactive)
  (require 'ox-ascii)
  (cl-letf (((symbol-function 'org-element-normalize-string) #'identity))
    (save-excursion
      (kill-new
       (org-export-as 'ascii
              nil nil t
              '(:ascii-verbatim-format "%s" :ascii-paragraph-spacing auto :ascii-headline-spacing nil :preserve-breaks t))
       ))))

You can try that command after evaluating the code above by selecting the region you want to copy and M-x org+-copy-code RET.

If you like what you see you can copy the code to your init file and bind org+-copy-code to some key, e.g., C-c c by the following line in your init file.

(with-eval-after-load 'org
    (define-key org-mode-map (kbd "C-c c") #'org+-copy-code))
Tobias
  • 32,569
  • 1
  • 34
  • 75
  • I had to add `:with-author nil` to the `org-ascii-export-as-ascii` options. But pasted text copied with this function has a trailing new line. How to get rid of it? – lecodesportif Jun 09 '21 at 18:29
  • @lecodesportif It is better to use the `BODY-ONLY` option of `org-ascii-export-as-ascii`. I modified the code accordingly. Furthermore, I just cut off the final newline which comes from exporting the paragraph and section. Check whether you can live with that. – Tobias Jun 10 '21 at 12:54
  • The new version removes all the new lines from copied non-empty lines, not just the final one. Ideally, ox would not add a final new line at all. I want to use a function like `org+-copy-copy-code` as my standard copy function and in that case I might also copy paste a text with a trailing new line (which should not be removed). – lecodesportif Jun 10 '21 at 15:20
  • @lecodesportif The culprit was an unconditional `org-element-normalize-string` in `org-export-as`. I've neutralized it with `letf`. As I understand you `org-element-normalize-string` would be bad anywhere during code export. – Tobias Jun 10 '21 at 17:36
  • The newline issue still remains. Try copying multiple a code block of multiple consecutive `~cd ~~~` lines with the function. For me they all paste on one line. – lecodesportif Jun 10 '21 at 18:09
  • @lecodesportif Ah, yes. There is `:preserve-breaks` which I have set thru `org-export-preserve-breaks` in my init file. That is why I did not notice. Now, I have tested with `emacs -Q` and it should work. – Tobias Jun 11 '21 at 03:27