Delete whitespace in Xcode?

I don’t know if KM can do something like this or not. I want a hotkey that deletes whitespace up to the next, well, non-whitespace. Is there any way to accomplish this? Can I read the character at the cursor, or something?


Perhaps Script Editor > Open Dictionary >

(Or, of course, the Script Debugger Explorer Panel)

Dang, didn’t even think of that. My brain still doesn’t automatically think the Mac way. Too many years of exposure to Windows has eroded the brain cells.

Thanks! I can totally get there from that!

1 Like

Though to fair, and probably risk charges of blasphemy here – I do think the VBA editor and library browser work better :slight_smile:

I know what you’re saying. All I really meant was, I didn’t even think of Automation.

Hey Dan,

Xcode has very poor text automation, but you can do a few things.

Use Keyboard Maestro to set the Find Pasteboard to a space and then.

Get the current selection range.

tell application "Xcode"
   tell front text document
      # Get
      get selected character range
      set selected character range to {1, 10}
   end tell
end tell

Use Keyboard Maestro to find the next space forward or backward via G or G.

Get the selection range.

Calculate the range you want to delete.

Set the selection range, and delete it.

Something like this which selects text to the right.

Generic-Test 01.kmmacros (2.3 KB)



I think that’s kind of backwards, isn’t it? Here’s a regex of what I want to find and delete, starting at the current cursor’s position: [^ \n\t]*

Does that make sense?

  • Option-Shift-Right Arrow
  • Option-Shift-Left Arrow
  • Delete

Should work if your cursor is before white space and a word follows which is a fairly normal case. But wont work and can fail badly otherwise.


  • Command-Shift-Right Arrow
  • Copy
  • Search & Replace Clipboard with regex
  • Paste

Moves the selection point, though you can possibly save and restore it with AppleScript as @ComplexPoint described? Or Insert Text by Pasting “%|%%CurrentClipboard%” - but that might be slow.

Xcode also has lots of editing commands, many of which are not bound to keys, one of them might do what you want, or alternatively might paste in a different way or some such that you could use.

I tried that. It deletes things like colons.[quote=“peternlewis, post:8, topic:4056”]

Xcode also has lots of editing commands, many of which are not bound to keys, one of them might do what you want,

Yeah, I searched, but I don’t think so.

This looks like the best solution. I was going to try that next. Thanks!

Hey Dan,

No. Did you try it?

It will find from the current cursor position to the next space character.

Hit it again, and it will expand to the next space character.

How is that backwards?


Hey Dan,

Did I read it? Yes I did. But you’re right I did misread it.

Must be getting a bit dyslexic in my old age…

So Peter’s right, the only real way is to select some text, copy, and find/replace the clipboard.

You could plot the location of the next non-whitespace character and then use AppleScript to make a selection and then delete that, but it seems like overkill.

Or you could do your editing in BBEdit.


Hey Dan,

It turns out that Xcode supports regular expressions.

Pull down the magnifying glass menu in the search field and change the find options.

Then you can do something like this:

Generic-Test 01.kmmacros (1.6 KB)

Xcode is a little weird though. If you start with the cursor in the whitespace (at the beginning or otherwise) it skips the first space. (Surely this is a bug.)

You can easily fix that with AppleScript techniques similar to what I used in my backwards macro.

It's awkward and stupid, but you know (it's Apple).


Chris - You’re a rock star! Thanks for working on this. Here’s some things I didn’t know, and why I never would have come up with this myself:

  1. “Find Pasteboard”. Never heard of this, until this thread. It’s obvious what it does from the context of our discussion, but nothing like this exists in Windows. Very cool.

  2. I figured Xcode supported regex, but I just assumed I’d have to do some UI manipulation to get it to work (check a checkbox in a dialog, something like that, and that’s just too fugly for this type of thing). I would never have imagined, not ever, that it would work the way you showed it.

Truly awesome. Thanks so much!

Hmm, I do actually have to set a find option, but I can live with it being set all the time.

Hey Dan,

Not all apps support the Find Pasteboard, but all Apple apps do and some other (some like Script Debugger let you toggle Find-Pastboard-Sharing on/off).

Find-Pastboard-Sharing can be quite vexing, because you might have a complex regular expression in Xcode – switch to Safari – search for something in a page – switch back to Xcode – and find your regEx missing…

<Curses elided>

Fortunately there’s a recent items menu in the Find Field.

I think it’s great that the Find Pasteboard can be set, but I think that sharing it between apps was one of Apple’s dumber ideas.



By the way, in the end, I couldn’t get it to work this way. It sort of worked the first time, then it wouldn’t work correctly at all after that.

I’ll do the cut/replace. Assuming I can figure out how to limit the replace to the first match.

This works well enough for me:

Hey Dan,

That works.

However I'm just slightly stubborn.  :sunglasses:

This works reliably for me.

Generic-Test 01.kmmacros (3.5 KB)

You do have somewhat better control over what you find/replace with your method though.


No matter what I do, I cannot get the AppleScript to refer to the actual front document.

I have several windows open with several tabs in each across several monitors, and document 1 and front document and any combination of file or text document etc etc do not actually give me the currently selected front document.

So if you have a single project window, this may work, but it seems to me if you have multiple windows on multiple monitors, then you may get completely rubbish results.



I only have 1 monitor, and don’t seem to have any problems with several windows open.