Here's a portable-ish solution using Java
The first step is, of course, installing Java
Next, paste the following into a file named Trimmer.java
in your user directory (C:\Users\your_name)
import java.awt.*;
import java.awt.datatransfer.*;
public class Trimmer{
public static void main(String... lines) throws Exception{
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
String str = (String)clip.getContents(null).getTransferData(DataFlavor.stringFlavor);
lines = str.split("\n");
str = "";
for(String line: lines) str += line.trim() + '\n';
StringSelection selection = new StringSelection(str.substring(0,str.length()-1));
clip.setContents(selection,selection);
}
}
Then, open up a command prompt and type
javac Trimmer.java
You should now have a Trimmer.class file in the same folder. You can delete the Trimmer.java file now.
At this point, we have a program that will remove all leading and trailing white spaces from each line in the clipboard, but we still need a way to run it conveniently. Let's make a hotkey for it!
First, create a shortcut to the command prompt (right-click C:\Windows\System32\cmd.exe)
Then, right-click this shortcut and select "Properties." There are a few things we need to do in this window. In the "Shortcut" tab, there should be a "Target" option that says something like
%windir%\system32\cmd.exe
Change it to say
%windir%\system32\cmd.exe /C "java Trimmer"
This will cause the command prompt to simply run our program and exit when we use this shortcut.
Next, click on the "Shortcut key" box and press the hotkey you'd like to trigger the text replacement. I recommend Ctrl+Shift+C for ease of use!
Finally, change the "Run" option to "Minimized" since we don't want a window blinking onto the screen every time we make use of the shortcut.
After all this, you should be able to use the following workflow:
- Copy text using CtrlC
- Trim text using Ctrl+Shift+C
- Paste trimmed text using CtrlV
:set paste
.cat
heredocs - It is.