Deleting spaces at the beginning of lines in vim
Sometimes I'll copy the text from a web page and paste it into a local text file
so that I can manipulate it, search through it, etc. But when pasted into the
text file it sometimes gets mis-formatted, with a bunch of spaces preceding some
lines, such as this
Ampan Victor Speech 6698
Anderson Mike Orchestra 6660
Anderson Bob Pupil Support 6668
Bancroft Mark Grade 4 6672
Hartee Jeanette Pupil Support 6680
Henshoof Harry Building PC/LAN Tech 6705 7130
Herg Sara Social Worker 6676
Looks Dean Pupil Support 6673
Murkstrand Lance Special Education 6673 7171
To eliminate those spaces in vim (may or may not work in vi, depending on version), use this command:
:%s/^\s\+//
Translation:
- % - Apply the command to all lines (same as 1,G)
- s - substitute what's between slash one and two with what's between
slash two and three. In this case, there's nothing between slash two and
three, so nothing is substituted, effectively deleting what's between slashes
one and two.
- ^ - The caret means starting at the beginning of the line
- \s - This is a metacharacter meaning white space, e.g., a space or a tab
- \+ - Another metacharacter meaning one or more of the preceding characters;
without this, only the first space or tab at the beginning of the line would be
selected and deleted.
10/27/2006