Remove multiple spaces

Double spaces in text. Suppose it is after the period at the end of a sentence, or a double space after a colon. I don’t see the extension, but was wondering if anyone could throw one together.

Do you want double spaces, or do you want to remove them?

Remove double spaces and replace them with a single space.

I have posted a similar request today. In my case, spaces can occur anywhere, including between words in a sentence. There should never be a space before a punctuation mark (except in French) as far as I know. Sometimes there are two but it can also be three spaces. Anything more than one is out of the question. It’s always about typos, sloppiness when typing a text quickly or editing (replacing words e.g.). In just about all the texts I receive, there are too many spaces.

This is a nice easy one! Couldn’t think of anything good for an icon.

// #popclip
// name: Remove multiple spaces
// icon: monospaced _
// after: paste-result
// language: javascript
return popclip.input.text.replace(/ +/g, " ");

(The above block is an extension snippet — select it then click “Install Extension” in PopClip.)

1 Like

Ahhhh! RegEx… Duh! Thanks man.

1 Like

Great! Thanks!

Just did a quick test. Spaces before punctuation are not removed. Perhaps because there was only one. But spaces before punctuation marks only occur in French as far as I know. Can this be implemented in the same extension?

Let’s get our thinking caps on. We’ll need to be a bit more specific about which punctuation we want to disallow spaces before. For example, we don’t want to allow spaces before a . but to do want to allow one before ".

I’m afraid the rules for this are language-specific and thus vary from one language to another. I can give them for Dutch but know they are different in French and German. So this is going to be a complex story.
I will attempt to list the rules for Dutch as soon as possible. I can also do this for German and Russian. Other languages I like to leave to native speakers of those languages. Does that make sense?

Yeah, let’s keep this in a reasonable scope :slight_smile:

Okay. Let’s start with English, Dutch, German, Russian… But not French. They share the same rules.

  1. One space following periods, commas, semicolons, colons, exclamation points, question marks, and quotation marks. No space before these.
  2. No spaces on either side of a hyphen (-).
  3. Spaces around Em-dashes (–) in a sentence.
  4. Quotation marks (all of them): get a space before the start of a quote and after the end of a quote.

The difficulties start when punctuation marks are combined with each other. For example, a full stop and an quotation mark. There is then no space between these characters and so the general rule (space after a full stop etc.) does not apply. Also before a footnote (i.e. a superscript number), there is no space after the punctuation mark… I don’t know if we can implement all these possibilities in the extension?

No one interested in such an extension? :frowning_face:

It’s interesting as a coding challenge, just, quite hard … for example how to detect which quote mark is the start of a quote and which is the end. Nontrivial.

Yes, that’s a tough one! But, of course, they always come in pairs. All word processors (e.g. Pages, Word…) offer the option to change them automatically (auto-correction, smart quotes). So, as a non-developer, I imagine that would involve looking at the previous quotes each time (within the boundaries of a text segment marked by PopClip). The first one should then always mark the opening of the quote and the next one its closing. Does that make sense?

It’s not so much difficult as fiddly, and there’s always going to be another edge case (for example ‘6"’ for six inches). And that could probably be detected too, but then you realise you forgot that decimal numbers exist, so you have to make exception to the “space after” rule for periods between digits, and the then someone reminds you that some countries use commas for the decimal separator, and pretty soon you are into the 4th hour of coding with no end in sight :slight_smile:

Yes, you are absolutely right. The number of difficulties and combinations that are virtually unforeseeable is almost endless. I can also imagine how many such requests you get all the time… But then, PopClip is a very special app. You don’t understand how there are people who can do without it! At least not me! Concluding: maybe still restrict yourself to simply removing all double spaces and replacing them with single ones?