Remove blank lines from regex search and replace

Hello there.

I got some help from Peter a while ago for a regex search and not replace to get the lines I needed after the # sign. That works well. I am trying to improve on it by removing the blank lnes that get injected into the clipboard.

I've muddled with it for some time and I can't wrap my head around it. I can see the variables changing and such but I'm sure I'm missing some syntax or I'm realy out to lunch. Any help would be apreciated.

Thanks :slight_smile:
PT Clean Paste .kmmacros (5.9 KB)

I hate to suggest something stupid, and I'm not sure of your entire macro so I don't know how it'd fit, but what about Filter: Trim Whitespace as an action after the regex?


1 Like

Thanks! I tried it but perhaps I don't know what I am doing. I scratched my current macro and decided to go back to what Peter had working for me. So if I try the filter and apply it to the source, it doesn't seem to be doing much .

I am brand new to KM - I'll learn the ropes.. ha.

Can you show the contents (Display Window) of the clipboard, both before and after your manipulation?


Your macro starts with "if the clipboard contains a $" but I don't see any "$" in the example you provided so that could be why your macro "doesn't seem to be doing much."

Just an idea. I don't know how your clipboard is generated, so my idea may not be correct.

OK that's probably because I don't really know how to handle my situation and use cases: Let me try and explain.
I've got this one macro that also is being triggered with a clipboard changed - that one pulls out all the dollar amounts and puts it back to the clipboard nice and clean. Not sure how I managed to pull through that one but it's using regex groups and actually doing what I want. See here:
Capture $ amounts.kmmacros (6.5 KB)
But then I also want to do a regex search adn replace so I get my clean clipboard with removing everything before the # sign.
PT Clean Paste .kmmacros (3.7 KB)
So depending on what I am doing I would like to both cleean the clipboard with the various macros. One to extract all the currencies which is working but then if I copy cisco cli commands I want to clean or trim up to and including the # sign.

So two macros triggering - I had to put a $ to stop the one from going as I know that copy from cisco cli stuff doesn't include a $ and would stop from doing anything with the clipboard.

Now probably the bigger problem is I am not up to snuff in all the ways I can actually trigger macros and there's probably better ways to handle the ability to have multiple clipboard triggers (or other ways to trigger things like this?)

Thanks muchly for the assistance.

These are screen caps in case it's more helpful then the pictures. I am obviously very green in KM but I am attempting to figure things out as quick as I can. Thank you kindly!

So you said the Capture $ amounts macro is working. What specifically is the PT Clean Paste macro not doing that you want it to do?

Give us an example of the input text and the output you are getting and the output you want.

You can do that in a number of ways

Thanks Peter. yeah the Capture $ amounts macro is working as intended - it finds all the currency amounts and lists them so I can just copy them specifically.

The PT Clean paste macro is all about switch configs (many of them have the same initial config) and right now that regex search and replace gives me all the lines such as:

interface vlan 1

ip address


This was the copy from text:

It can be any copy of various lines of config - some samples to play with are here:

What I want is this:
interface vlan 1
ip address

Notice I had to backspace on each line once but that might not happen in certain apps - I think that is just how it came from that link. In packet tracer there's no leading space with each line of command.

The extra line is just redundant. I've tried to do another search and replace on the replaced clipboard after the intial one. I've tried also somehow doing a foreach loop for each line in clipboard and search for whitespace with another regex.....

That's my goal.

1 Like

I have a method that doesn't involve regex:

OK, so you are wanting the blank lines in the middle removed, which is not what the Trim Whitespace does (it removes white space from either end of the string.

Use a Search and Replace action, with regex \R+ and replace \n and that will replace any sequence of one or more line endings with a single linefeed.

Worked beautifully. Thanks Peter.

KM is magical - now to just keep learning and playing with it. Thank you kindly. :innocent: :innocent: :innocent:

My next question - related to this whole project is - how do you have all kinds of different macros all involving a clipboard change event?
My currency task, this task for example, all relate to the clipboard change event.
I'm preventing one from occuring if I see $, for example, but that doesn't seem sustainable over the course of KM career.
I'm reviewing documentation but I see various things like window title etc which I've used in some macros already.


I think you need to create some sort of "context switch" for this. One form of switch that's built-in to Km is the idea that the group that any macro is in can be automatically activated and deactivated depending on which app is currently in focus. So that way you can have two different macros triggered by the same "clipboard change" trigger and they do things appropriate to the context you are in. Another form of switch is creating a variable that can change its state based on some user action. If the KM solution isn't sufficient for you, I can go into more details about using a variable to implement a context switch.

For example, you could let the user use a hotkey to specify a "context" like this:


And then in your macro which triggers on a clipboard change, you could do this:


I consider this to be a clean and simple implementation of a context switch. I do things like this all the time.


That makes sense. Can you go into more detail on the context switch? I got to this link which he triggers a command palette and chooses which one - maybe that works. Show the command pallete to perform one of the two macros while in the browser for example?

Less friction the better but that might be a solution. I like your variable idea - something I sort of implemented with that $ match or the url match in the currency macro.

Appreciate the help as I learn... Cheerio!

Sorry I was replying via email as oppoosed to the forum post and did not see the graphics! Apologies, thank you for the info. Reviewing now.

It's probably my fault. I have not read any personal messages sent to me in weeks or months. That's because I sometimes receive emails that are unpleasant to read (criticism of my style) and I prefer to stick my head in the sand rather than deal with people who criticize my responses. I guess I don't have a very thick skin. Most people are much more polite on public forums, which is why I restrict all my business to the forums.

Ha you've been super helpful and I've learned much from you already! Won't be long until I've got another question I'm knee deep in pain with that you can assist with lol :smile: