0

The only answer I could look online was to use Ctrl+Shift+V but that answer was almost a decade old and doesn't work anymore. I'm trying to copy text from the Evince PDF reader into other applications (Anki or gedit) but that it usually shows up with formatting which is annoying.

By formatting, I meant that it copies the text while preserving it's original format. For example, if I copy this text from Evince.

enter image description here

And then try to paste it into another application, it shows up like this. enter image description here

What I'm trying to achieve is to paste in without the formatting (indentations in this case), to make it look somewhat like this.

Another Image

I'm on Ubuntu 21.10.

8
  • Hello. Some more precise info is needed. What is the name of the default PDF reader? What do you mean by formatting, some examples please. Maybe cut and paste something into the question.
    – David
    Apr 6, 2022 at 10:56
  • @David Okay, I've edited the question. It would be really helpful if you can look into it now.
    – Heisenberg
    Apr 6, 2022 at 11:07
  • 2
    OK I can see what you are talking about but I do not think it is formatting. What I see are line feeds at the end of each line in the picture. I do not see how you could simply cause them not to happen. If you make the first screen shot text wider does it still line feed at the same word?
    – David
    Apr 6, 2022 at 11:15
  • @David, yeah, I think you're right. But I don't really get what you mean by " does it still line feed at the same word.
    – Heisenberg
    Apr 6, 2022 at 11:20
  • 1
    See superuser.com/questions/796292/…
    – pLumo
    Apr 6, 2022 at 13:44

1 Answer 1

3

What you define as "formatting", i.e., newlines or white space because of spaces or tabs, is not formatting. These are represented by characters such as carriage returns, space characters or tab characters.

When pasting to gedit or to the terminal, these characters are preserved. Formatting codes that may be included by word processors are not preserved.

Your only option is to change the text before pasting it in the destination. In the text editor, you can replace tabs, multiple spaces, line feeds. This being Linux, it could also be automated (with thanks to Lumo for the link).

A script that can do this boils down to:

#!/bin/bash
SelectedText="$(xsel)"
ModifiedText="$(echo "$SelectedText" | \
     sed 's/\.$/.|/g' | sed 's/^\s*$/|/g' | tr '\n' ' ' | tr '|' '\n')"
echo "$ModifiedText" | xsel -bi

First line copies current text in the clipboard to the clipboard. Second line modifies it (in this script, only new lines are addressed, no spaces or tabs), third line puts the result back in the clipboard, ready to paste.

Place such script in your ~/.local/bin, make it executable then bind it to a shortcut key. After copying text from a PDF, remember to hit that shortcut key before pasting.

Current example will only work on Xorg. Ubuntu 21.10 defaults to Wayland. xclip or xsel do not work on Wayland so should be replaced with wl-copy and wl-paste from the wl-clipboard package.

You must log in to answer this question.

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