Sunday, March 22, 2020

CopyQ - The most useful commands

In this post, we will look at some of the most useful CopyQ commands that are either built-in, or that can be additionally imported from a dedicated project on GitHub. If you are unsure what CopyQ or its commands mean, or what are the various types of commands, I recommend you to read the quick introduction to CopyQ first.

Note: There are simply too many commands available to name them all, so what follows is just a selection of commands which I myself currently find generally the most useful. It's quite likely that depending on the stuff you do and applications you use, your selection of favorite commands might be different.

Built-in commands


Built-in commands are the commands which can be easily added to CopyQ via the "Add" button of the Commands window, as shown in the following screenshot:

CopyQ's "Add commands" dialog, listing all the built-in commands.

Command: Paste clipboard as plain text (global)


More often than not one needs to paste a clipboard content without any formatting. And that's exactly what the global command "Paste clipboard as plain text" does. This function is already built-in to some programs, e. g. into the LibreOffice suite, but many other programs don't have anything like that.

CopyQ also offers a similarly named command: "Paste as Plain Text". It does the same thing, but you run it upon a selected item from CopyQ's history instead of using the current clipboard content.

And that's all from built-in commands. Still, there are commands which you might find useful in specific scenarios, so you can surely give them a try as well.

Commands from the GitHub project


There is a dedicated copyq-commands project on GitHub, maintained by the CopyQ's author, that one can use to easily get to plenty of useful commands. Another pro of this project is that you can suggest or contribute new commands there.

Let's have a look of some handy commands from this project now. Note that there is a small gotcha that the commands are often called differently than in the project's documentation.

Command: Import Commands after Copied to Clipboard (automatic)


[documentation]

As described at the project's home page linked above, this command saves you time when adding other commands to CopyQ - so that you don't need to enter all the command attributes manually.

Warning: If the copied text is a link to a command on GitHub, the automatic import doesn't have to always succeed due to a general SSL issue #1177.

Command: Big Data Tab (automatic)


[documentation]

If you copy too big items, searching in the clipboard history might get slow. That's when this command steps in and moves an item into a dedicated tab if the item exceeds a predefined size (counted as a sum from all the item formats - e. g. plain text + html1).

Of course, you can change this limit - right at the beginning of the script. But how to find out how big an item is, without scripting? Simply select an item and select "Show Content..." from the context menu (or press F4) and you will see the size in bytes in the follow-up dialog.

Command: Replace All Occurrences in Selected Text (global)


[documentation]

This is a simple find-replace text function which is not that powerful and I can imagine it could be taken at least one step further. Yet, it nicely demonstrates some of the CopyQ's capabilities for those who want to write their own commands - like:
  • triggering copy and paste system events,
  • showing a user input dialog,
  • or using various JavaScript / ECMAScript constructs, including RegExp functions.

Command: Snippets (global)


[documentation]

This powerful command is my favorite one. In short, this command extends CopyQ so that it can be used as a simple tool for snippets, or templates, if you like.

This command is quite nicely documented, so I will just illustrate its usage on the following example:

Imagine you write a blog post and you want to insert a footnote and a corresponding footnote link into the post. As this doesn't have to be directly supported by your blogging system, you can use the Snippets command for generating relatively complex HTML code chunks:

An example of a dedicated tab with custom snippets.

An example of the snippet selection dialog when the Snippets command is launched.

An example of the final dialog where values for placeholders are to be filled in.


Customizing the command

You may want to add the resulting snippet text as another item to history and not to paste it immediately. In the simplest case, you would have to change the function pasteSnippet() of the command script from this:

function pasteSnippet(mime, content) {
  copy(mime, content)
  copySelection(mime, content)
  paste()
}

... to this:

function pasteSnippet(mime, content) {
  copy(mime, content)
  copySelection(mime, content)
  tab('clipboard') // change this to whatever default tab name you use
  add(unpack(content))
}

Of course, I can't give any warranty on this, but it is supposed to work at the time of writing.

Conclusion


As you can see, you can use CopyQ not only as a simple clipboard manager, but thanks to commands you can extend its functionality in many ways. Don't hesitate to share your own experience with CopyQ commands in the comments!

1 Note that for texts copied e. g. from MS Word the size of clipboard content in the HTML format can be surprisingly high (like several kilobytes) even for short texts. This is due to all the extra styling information added to clipboard by the source application.