17

I'm about to get involved in some collaborative prose writing with a friend of mine via email. As a regular person, he will of course be using MS Word document formats; as a massive nerd, I prefer to use markdown when writing on a computer.

What I need is the exact opposite of this question about converting rich text copied from a webpage to markdown. My naive worst-case workflow would be:

  1. Write up the document in markdown
  2. Use pandoc -S file.mkd -o temp.html
  3. Open up temp.html in a web browser
  4. Copy & paste from the page to the open document in libreoffice

I am certain that this can be optimised.

(Google Docs is not an option in this case).

Even though I've answered the main question, I still feel that this could be optimised further. If there is any way to concatenate .doc files (which I will be receiving and which pandoc can write to), perhaps with the libreoffice command-line interface, then I suppose it would be possible to construct an overly-complicated one-liner and avoid having to leave the terminal at all. If anyone finds a way to do that, I will happily accept that answer over my own.

2

2 Answers 2

21

As it turns out, the link in the question hinted at a working solution in the form of xclip:

pandoc -S file.mkd | xclip -t text/html

...and then I can paste it straight into the document in libreoffice, properly formatted. This works with the versions of the programs in the Ubuntu 13.04 repositories (pandoc 1.10.1 and xclip 0.12) -- the -t option for xclip especially is only in version 0.12 or above. The -S option of pandoc makes it produce 'typographically correct output', so -- is turned into an en-dash, --- is turned into an em-dash, and a few other things.

If you want to use the ctrl-v clipboard, use:

pandoc -S file.mkd | xclip -t text/html -selection clipboard

Edit: if you're running OS X (with pbcopy rather than xclip), use:

pandoc -S file.mkd | textutil -stdin -format html -convert rtf -stdout | pbcopy

To transform selected text without creating a file, you can use:

xclip -o | pandoc -S | xclip -t text/html

...this can, of course, be mapped to a keyboard shortcut.

This can work well with a number of text markup formats as input, see the pandoc guide for some more information on how to accomplish this (you might need to use the -f/--from/-r/--read option, especially if you're using the xclip|pandoc|xclip version).

As a side note, you can also read an already-existing HTML file into xclip:

xclip -t text/html <file.html

or

<file.html xclip -t text/html
3
  • I can't seem to get this to work with xclip 0.12 (Arch Linux), as it doesn't seem to support -t. How do you get a version that works?
    – PythonNut
    Jan 6, 2016 at 20:10
  • 3
    Love this answer, but found that textutil doesn't work great here because it defaults to Times New Roman for the rtf output, as well as running into text encoding issues. It's also an unnecessary step, since pandoc can output directly to rtf, with, in my experience, better results. I used pandoc -sf markdown+smart -t rtf file.md | pbcopy
    – Igid
    Feb 13, 2019 at 22:06
  • Note: to get the contents of the clipboard in HTML format, run xclip -o -t text/html -selection clipboard. To get the contents of X selection in HTML format, run xclip -o -t text/html
    – Flimm
    May 11, 2021 at 5:33
3

If you use Vim

Great solution, since I use neovim, I used the answer given above on the following command:

:w ! pandoc -s | xclip -t text/html -selection clipboard

  • :w ! {cmd} pipes the buffer to the shell command
  • The rest is found in the answer above
2
  • And, for using (Neo)Vim on Mac OS: :w ! pandoc -sf markdown+smart -t rtf | pbcopy. (Given that this question is about helping people skip steps for cutting and pasting, I thought it would be useful to make it possible to copy and paste to your .vimrc in one step.) Feb 5 at 21:02
  • EDIT: to make it work in the .vimrc (on a Mac), change it to nnoremap <leader>o :w ! pandoc -sf markdown+smart -t rtf \| pbcopy<CR><CR>. Feb 6 at 4:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .