Skip to content

Paste formatted text custom tool

Diogo Tito Victor Marques edited this page Oct 12, 2022 · 2 revisions

Instead of this custom tool, you can install the pastefromhtml plugin which doesn't depend on Pandoc and is able to paste images!
https://github.com/cetinkaya/pastefromhtml

Zim doesn't support pasting formatted text from other applications at the moment.

As a workaround, we can use Pandoc to convert the clipboard content to Zim's wiki syntax and insert it in the active note with a Custom Tool, which can be bound to a Key Binding like Ctrl+Shift+V

Creating the custom tool

  1. In the menu bar, click ToolsCustom Tools → the [+] on the right
  2. Fill the dialog like this:
Edit Custom Tool
Name: Paste from HTML
Description: Gets the clipboard contents as HTML and converts it to Zim's syntax with Pandoc before inserting it in the note
Command:

🐧 On Linux:
sh -c "xclip -selection CLIPBOARD -t text/html -o | pandoc -f html -t zim"

🪟 On Windows:
powershell -c '$ofs = "`n"; (((gcb -t h) -join "`n") -split "|")[1] | pandoc -f html -t zimwiki'

🔲 Command does not modify data
☑️ Output should replace current selection
☑️ Show in toolbar

🐧 Linux setup

Just install the pandoc package with your distro's package manager. Make sure the xclip command is available on your system as well.

# Debian, Ubuntu, etc.
$ sudo apt install pandoc xclip

# Arch, Manjaro, etc.
$ sudo pacman -S pandoc xclip

🪟 Windows setup

Pandoc is available in winget, Scoop and Chocolatey:

# winget
> winget install pandoc

# Scoop
> scoop install pandoc

# Chocolatey
> choco install pandoc

Otherwise, you can install Pandoc by following the instructions on the website: https://pandoc.org/installing.html

Older instructions with a html-to-zim.ps1 script Then, somewhere on your PC, create a file html-to-zim.ps1 with this code in it:
$ofs = "`n"

$clipboard = Get-Clipboard -TextFormatType Html
$html_fragment = ("$clipboard" -split '<!--StartFragment-->|<!--EndFragment-->')[1]

$html_fragment | pandoc --from html --to zimwiki

If it is the first time you're running PowerShell scripts on your system, you might need to change the execution policy by opening a Windows PowerShell session and running

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

After having Pandoc installed and PowerShell configured to allow running arbitrary scripts, this Custom Tool's command should now be able to run.

It's probably possible to reduce this script to a one-liner. That would simplify the setup on Windows. (DONE)

Assign a Key Binding to this Custom Tool (Ctrl+Shift+V)

  1. Open the Preferences window (EditPreferences on the menu bar)
  2. Go to the Key bindings tab
  3. There is an action for the Custom Tool you just created. It's probably the 2nd or 3rd one in the list and the name might look like
    <Actions>/custom_tools/paste from html-usercreated
  4. Double click the Key Binding cell (it probably says "Disabled")
  5. Press Ctrl+Shift+V
  6. Click OK

Now test this new Custom Tool:

Copy some formatted text from Writer/Word or the web, then jump back to Zim and try pasting it with either ToolsPaste from HTML on the menubar, the toolbar button or Ctrl+Shift+V.

The clipboard was converted to Zim's wiki syntax and inserted in the cursor's position!

Now press Ctrl+R for Zim to redisplay it as formatted text.

Clone this wiki locally