Insert text using keyboard shortcut


#1

You can use this to insert text into documents, etc. using a keyboard shortcut.

Just make sure that subprocess.call contains whatever file manager you are using.

Is there an easier way to indent code other than manually?
It’s tedious doing so with a large file.

#!/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 Ctrl V”’

process user input

try:
choice = subprocess.check_output(["/bin/bash", “-c”, test]).decode(“utf-8”)
if “manage snippets” in choice:

IMPT!! Use whatever file manager I am using

    subprocess.call(["thunar", 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