アプリケーションにテキストスニペットを挿入するツール

アプリケーションにテキストスニペットを挿入するツール

ドイツ語では、メールや手紙の冒頭に「Sehr geehrter Herr ....」と書きます。

これを何度も入力するのはうんざりです。また、このようなテキスト ブロックを挿入するためのショートカットを提供するようにアプリケーションを構成するのもうんざりです。

デスクトップ環境でコメントテキストブロックを挿入する方法はありますか?

この方法で、vi、thunderbird、firefox、libreoffice などにテキスト ブロックを挿入できるようになりました。

別の例: ssh-pub-key をどこかに挿入する必要があることがよくあります。ssh-copy-id の使い方はわかっていますが、やはり、テキスト ブロックの構成可能なリストにアクセスできるデスクトップ ソリューションがあれば便利です。

答え1

私が使うオートキーUbuntuソフトウェアセンターからインストールします

本当に使いやすい

メールアドレスのような「フレーズ」を入力[email protected]し、gmタブキーを押して追加しました<tab>

ここに画像の説明を入力してください

ここに画像の説明を入力してください

答え2

以下のスクリプトは役に立ちます使用するアプリケーションで Ctrl+V テキストを貼り付けるgnome-terminalたとえば、*では動作しないことを知っておくことが重要です。
私は、Firefox、Thunderbird、Libreoffice、Sublime Text、Gedit で問題なくテストしました。

使い方

スクリプトが呼び出されると、定義したスニペットをリストするウィンドウが表示されます。項目を選択するか、その番号を入力すると、テキスト フラグメントがCtrl+ V"-compatible" であるすべてのアプリケーションの最前面のウィンドウに貼り付けられます。

ここに画像の説明を入力してください

スニペットの追加/編集

を選択するとmanage snippets、スクリプトのフォルダが~/.config/snippet_pasteNautilus で開きます。新しいスニペットを作成するには、スニペットのテキストを含むテキスト ファイルを作成するだけです。ファイルに付ける名前は気にする必要はありません。プレーン テキストであれば問題ありません。スクリプトはファイルの内容のみを使用し、見つかったすべてのファイル ('コンテンツ) の番号付きリストを作成します。

ここに画像の説明を入力してください

スニペット ディレクトリ ( ~/.config/snippet_paste) が存在しない場合は、スクリプトによって作成されます。

使い方

  • システムにインストールされていない場合は、まずxdotoolとをインストールします。xclip

    sudo apt-get install xdotool
    

    そして

    sudo apt-get install xclip
    
  • 以下のスクリプトをコピーし、 として保存しpaste_snippets.py、次のコマンドで実行します。

    python3 /path/to/paste_snippets.py
    

スクリプト

#!/usr/bin/env python3

import os
import subprocess

home = os.environ["HOME"]
directory = home+"/.config/snippet_paste"
if not os.path.exists(directory):
    os.mkdir(directory)
# create file list with snippets
files = [
    directory+"/"+item for item in os.listdir(directory) \
         if not item.endswith("~") and not item.startswith(".")
    ]
# create string list
strings = []
for file in files:
    with open(file) as src:
        strings.append(src.read())
# create list to display in option menu
list_items = ["manage snippets"]+[
    (str(i+1)+". "+strings[i].replace("\n", " ").replace\
     ('"', "'")[:20]+"..") for i in range(len(strings))
    ]
# define (zenity) option menu
test= 'zenity --list '+'"'+('" "')\
      .join(list_items)+'"'\
      +' --column="text fragments" --title="Paste snippets"'
# process user input
try:
    choice = subprocess.check_output(["/bin/bash", "-c", test]).decode("utf-8")
    if "manage snippets" in choice:
        subprocess.call(["nautilus", directory])
    else:
        i = int(choice[:choice.find(".")])
        # copy the content of corresponding snippet
        copy = "xclip -in -selection c "+"'"+files[i-1]+"'"
        subprocess.call(["/bin/bash", "-c", copy])
        # paste into open frontmost file
        paste = "xdotool key Control_L+v"
        subprocess.Popen(["/bin/bash", "-c", paste])
except Exception:
    pass

nautilusを使用していない場合

別のファイルブラウザを使用している場合は、行 (29) を置き換えます。

subprocess.Popen(["nautilus", directory])

による:

subprocess.Popen(["<your_filebrowser>", directory])

スクリプトをショートカットキーの組み合わせの下に置く

より便利に使用するために、スクリプトを呼び出すショートカットを作成できます。

  • システム設定→キーボードショートカットカスタムショートカット

  • クリックして+コマンドを追加します:

    python3 /path/to/paste_snippets.py
    

スクリプトは以下でも公開されていますギスト


*編集

以下のバージョンでは、(gnome-)ターミナルが最前面のアプリケーションであるかどうかを自動的にチェックし、貼り付けコマンドをCtrl+ではなくShift+に自動的に変更します。VCtrlV

使用方法とセットアップはほぼ同じです。

スクリプト

#!/usr/bin/env python3

import os
import subprocess

home = os.environ["HOME"]
directory = home+"/.config/snippet_paste"
if not os.path.exists(directory):
    os.mkdir(directory)
# create file list with snippets
files = [
    directory+"/"+item for item in os.listdir(directory) \
         if not item.endswith("~") and not item.startswith(".")
    ]
# create string list
strings = []
for file in files:
    with open(file) as src:
        strings.append(src.read())
# create list to display in option menu
list_items = ["manage snippets"]+[
    (str(i+1)+". "+strings[i].replace("\n", " ").replace\
     ('"', "'")[:20]+"..") for i in range(len(strings))
    ]
# define (zenity) option menu
test= 'zenity --list '+'"'+('" "')\
      .join(list_items)+'"'\
      +' --column="text fragments" --title="Paste snippets" --height 450 --width 150'

def check_terminal():
    # function to check if terminal is frontmost
    try:
        get = lambda cmd: subprocess.check_output(cmd).decode("utf-8").strip()
        get_terms = get(["xdotool", "search", "--class", "gnome-terminal"])
        term = [p for p in get(["xdotool", "search", "--class", "terminal"]).splitlines()]
        front = get(["xdotool", "getwindowfocus"])
        return True if front in term else False
    except:
        return False

# process user input
try:
    choice = subprocess.check_output(["/bin/bash", "-c", test]).decode("utf-8")
    if "manage snippets" in choice:
        subprocess.call(["nautilus", directory])
    else:
        i = int(choice[:choice.find(".")])
        # copy the content of corresponding snippet
        copy = "xclip -in -selection c "+"'"+files[i-1]+"'"
        subprocess.call(["/bin/bash", "-c", copy])
        # paste into open frontmost file
        paste = "xdotool key Control_L+v" if check_terminal() == False else "xdotool key Control_L+Shift_L+v"
        subprocess.Popen(["/bin/bash", "-c", paste])
except Exception:
    pass

関連情報