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.