Remove all Double Spaces in Vim

Feb 18, 2020

If you’re like me and have the joy of a keyboard that doesn’t work on your expensive MacBook Pro, and your keyboard insert as many spaces as it feels like at random times. And if you are also a vim user, I have a hack that might help.

I use the following command to find all places with one or more spaces and replace them with a single space. It’s worth noting that in vim, you need to escape things you normally wouldn’t, like the { and }

Also worth noting that I am using the c modifier so I don’t make changes that I don’t want to.

:%s/\s\{2,\}/ /gc

If you would like to be notified the next time I write something, you can subscribe with your choice of RSS or Email.

I promise not to spam you and you can unsubscribe at anytime.

Also, here are some other items you might like longer longer:

or go see everything in the archive.