Sid*_*Man 9 bash scripts clipboard xclip xsel
将某些内容复制到剪贴板(使用 ctrl+c)后,我想要一个脚本(bash、python 或任何其他语言)自动检测新条目已添加到剪贴板,更改其内容并将其放回剪贴板,以便在粘贴时我得到修改后的文本。该脚本应不断在后台运行并监视剪贴板的变化。
以下脚本描述了所需的修改:
来源:https : //superuser.com/questions/796292/is-there-an-efficient-way-to-copy-text-from-a-pdf-without-the-换行
#!/bin/bash
# title: copy_without_linebreaks
# author: Glutanimate (github.com/glutanimate)
# license: MIT license
# Parses currently selected text and removes
# newlines that aren't preceded by a full stop
SelectedText="$(xsel)"
ModifiedText="$(echo "$SelectedText" | \
sed 's/\.$/.|/g' | sed 's/^\s*$/|/g' | tr '\n' ' ' | tr '|' '\n')"
# - first sed command: replace end-of-line full stops with '|' delimiter and keep original periods.
# - second sed command: replace empty lines with same delimiter (e.g.
# to separate text headings from text)
# - subsequent tr commands: remove existing newlines; replace delimiter with
# newlines
# This is less than elegant but it works.
echo "$ModifiedText" | xsel -bi
Run Code Online (Sandbox Code Playgroud)
我不想使用快捷键绑定来运行脚本。
幸得肯恩。
我根据我的要求修改了脚本,并添加了检测剪贴板复制事件并修改其内容的功能。
来源。
这个 bash 脚本在从 PDF 复制文本时删除换行符。它适用于 Linux 的主要选择和剪贴板。
#!/bin/bash
# title: copy_without_linebreaks
# author: Glutanimate (github.com/glutanimate)
# modifier: Siddharth (github.com/SidMan2001)
# license: MIT license
# Parses currently selected text and removes
# newlines
while ./clipnotify;
do
SelectedText="$(xsel)"
CopiedText="$(xsel -b)"
if [[ $SelectedText != *"file:///"* ]]; then
ModifiedTextPrimary="$(echo "$SelectedText" | tr -s '\n' ' ')"
echo -n "$ModifiedTextPrimary" | xsel -i
fi
if [[ $CopiedText != *"file:///"* ]]; then
ModifiedTextClipboard="$(echo "$CopiedText" | tr -s '\n' ' ' )"
echo -n "$ModifiedTextClipboard" | xsel -bi
fi
done
Run Code Online (Sandbox Code Playgroud)
sudo apt install xselsudo apt install git build-essential libx11-dev libxtst-dev
git clone https://github.com/cdown/clipnotify.git
cd clipnotify
sudo make
Run Code Online (Sandbox Code Playgroud)
chmod +x "copy_without_linebreaks.sh".\copy_without_linebreaks.sh