REAL-WORLD CHALLENGES

Whitespace cleanup

Practical substitutions: collapse space runs and strip trailing whitespace.

Replace every run of two or more spaces with a single space. The pattern matches the run, the replacement is one space. ` {2,}` reads as "a space, two or more times" - so "a b" (three spaces) becomes "a b".

Why two or more, not one or more? Replacing every single space with a space works too, but it touches text that is already fine. Precise patterns make precise edits - it matters once you run substitutions over real files.

PRACTICE - 2 DRILLS 0/2 DONE
DRILL 1/2

Collapse every run of multiple spaces into one space.

s/
->
too   many    spaces
must become:
too many spaces
already clean
must become:
already clean
a  b   c
must become:
a b c
DRILL 2/2- Strip trailing spaces

Remove trailing spaces and tabs at the end of every line. Replace them with nothing.

s/
->
line one   
line two	 
clean   
must become:
line one
line two
clean
alpha  
beta
must become:
alpha
beta
nothing to trim
must become:
nothing to trim