Skip to content

Instantly share code, notes, and snippets.

@Jacob-Vlijm
Last active September 22, 2015 23:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jacob-Vlijm/cdb543d370c2ea4197cc to your computer and use it in GitHub Desktop.
Save Jacob-Vlijm/cdb543d370c2ea4197cc to your computer and use it in GitHub Desktop.
Script to paste textual snippets in applications that past with the key combination Ctrl+V. It needs xsel and xdotool to be installed
#!/usr/bin/env python3
"""
Copyright (C) 2014 Jacob Vlijm
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or any later version. This
program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details. You
should have received a copy of the GNU General Public License along with this
program. If not, see <http://www.gnu.org/licenses/>.
"""
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
@Jacob-Vlijm
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment