Breaking spaces in vim

1 minute read

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?

non-breaking space HTML entity with right-pointing arrow to whitespace character escape sequence.

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

or more compactly like so:

:%s/<Ctrl-V>xa0/ /g

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!


If you liked this post and want to see more like this, please buy me a coffee!

buy me a coffee logo