Fast and easy to use python clipboard

How to create a clipboard with python - and use it in other applications(example: vim)


The computer clipboard is very easy to use
Ctrl+c and you are set to go with Ctrl-v whenever you want
But what happens when you want to have more than one value in your clipboard? When you have 3, 4 , 1000 values you need to use, one after the other and they are on different pages?
You have to do Ctrl+c Ctrl+v multiple times and switch between windows. Can be very frustrating.
So I created clipboard script that saves multiple values to a JSON file and you can load it wherever you want because its a file.
I called it zxclipboard, from the reason I explained in the post about creating command with autocompletion(Control the computer with one command).
If you just want to see the script click here.


Explanation:


SAVED_DATA = "/tmp/clipboard.json"
- This is the json file that the script is going to use
def empty_file(file_path):
with open(file_path, "w") as f:
json.dump({}, f)
- Creates empty json file
if len(sys.argv) == 2 or len(sys.argv) == 3:
command = sys.argv[1]
data = load_data(SAVED_DATA)
- Check if there are 2 or 3 arguments for the command. If 2, then its only the command and action. If 3 then the register that is being used is also mentioned. Then load the content of the file to a variable
if command == "save":
if len(sys.argv) == 3:
key = sys.argv[2]
else:
key = input("Enter a key:")

value = clipboard.paste().replace("\n", "")
if value in data.values():
print("Value \"" + value + "\" already exists so NOT saved again")
exit
else:
data[key] = value
save_data(SAVED_DATA, data)
print("Data saved in clipboard.json")
- If register is mentioned use it and save it in the file, else ask for a register and save it in the file

elif command == "load": for k,v in data.items():
print(k + ":", v)
key = input("Enter a key:")
if key in data:
clipboard.copy(data[key])
print("data is in clipboard")
else:
print("Key does not exist")
- Show all the keys and values, enter what key you want, if it exists, load it
elif command == "list"
- List all existing keys and values in clipboard
elif command == "insert"
- Insert a key and value(current clipboard content) to the file
elif command == "rm"
- Remove all file content - Erase all clipboard values

Usage Example - All the usages are in vim
1st Usage -
I wanted to be able to copy few things from text file to be used in other window. So I need to copy text from a file and be able to put it in the clipboard I built. So I put this command in vimrc file(actually a very complex vim files structures that will be explained in other post):
command! Zxclipboardsave :!zxclipboard save - Now when I copy something from the file to clipboard I can save it in a dedicated register which can be used anywhere else in the computer
command! Zxclipboardload :!zxclipboard load - I can load to vim things that I saved in the clipboard file.
I can create shortcut for those command like zxs or zxl and it will be very easy to use.

2nd Usage -
I can create a map to save the path of the file to the clipboard so it can be taken from the clipboard for other places that needs it -
nnoremap <leader>cfc :let @+=expand('%:p')<CR>:!zxclipboard save f<CR><CR>
The map name - cfc - Copy Filename Clipboard - should be easy to remember, as it self explanatory.
Explanation:
<leader>cfc - the character that you define as mapleader let mapleader="," in my case. and then the combination after it to trigger the command
:let @+= - define the register '+' which is the clipboard register. @ is for refering a register. You can put after it whatever character you want.
expand('%:p') - A way to print the file path.
<CR> - Indicate Carriage Return, in other words - Enter
:! - What comes after will be run as a shell command
zxclipboard save f - Use the command zxclipboard and put whats in clipboard (file path) in f key.If zxclipboard is not in $PATH env var, should write the whole path to it.
Two <CR>s to finishe the command.