DEV Community

Cover image for Breaking spaces in vim
Paul Cochrane 🇪🇺
Paul Cochrane 🇪🇺

Posted on • Originally published at peateasea.de on

Breaking spaces in vim

Originally published on peateasea.de.

Some text editing software packages seem to insert non-breaking spaces into files willy-nilly. This can be a pain if you don’t want them. Also, they’re very hard to find because–to the eye–they’re indistinguishable from a normal space. What to do?

If you’re like me and use vim as your day-to-day editor and want to break free from non-breaking spaces, you can replace them in the entire document (from normal mode) like so:

:%s/<Ctrl-V>u00a0/ /g
Enter fullscreen mode Exit fullscreen mode

or more compactly like so:

:%s/<Ctrl-V>xa0/ /g
Enter fullscreen mode Exit fullscreen mode

For those who might be interested in the details, here’s what’s happening:

  • when in normal mode, commands are prefixed with a colon :
  • the % means that the following command will be carried out for all lines in the buffer or file.
  • s denotes the start of a substitution.
  • the / characters delimit both the string to search for and the string to replace it with.
  • <Ctrl-V> means to enter the ^V control character by pressing the Ctrl key and v together.
  • u00a0 is the unicode hex sequence (in vim) for a non-breaking space. The ^V combined with the unicode hex sequence then produce a non-breaking space.
  • the trailing g means to carry out the substitution “globally” i.e. for all occurrences on the current line (the % then handles the case for all lines).

Happy hacking!

Top comments (0)