vim

Breaking spaces in vim

peateasea

Paul Cochrane đŸ‡ȘđŸ‡ș

Posted on May 1, 2024

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!

💖 đŸ’Ș 🙅 đŸš©
peateasea
Paul Cochrane đŸ‡ȘđŸ‡ș

Posted on May 1, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Baby Steps with Rust
programming Baby Steps with Rust

November 29, 2024

OOP Bootcamp 2: The Why of OOP
oop OOP Bootcamp 2: The Why of OOP

November 29, 2024

The Story of Nel
satire The Story of Nel

November 29, 2024