To remove whitespace characters, such as spaces and/or tabs from
a line while editing a file in the vi and
Vim text editor you can use a
regular expression (regexp) that incorporates \s
(lowercase letter "s"), which respresents a white space character. E.g.,
supposing the lines below appear in a file:
450 SN/GN Tech Edit Delete ACE Edit Delete ADO Edit Delete AGO Edit Delete AGS Edit Delete AIM Edit Delete ASF Edit Delete
I could hit the
colon key and type s/\s*Edit Delete//
to
delete the white space after the project name that appears at the
beginning of the line and the "Edit Delete" at the end of the line, so that
only the project name remains. To perform the substitution for all lines,
I could use 1,$ s/\s*Edit Delete//
.
The 1,$
represents every line from the 1st to the last
and 1,$ s/old_pattern/new_pattern/
would indicate
that a substitution is to be performed on every line with new_pattern
replacing old_pattern on each line.
The \s
represents any whitespace character and the
asterisk
after it indicates to look for zero or more occurrences of any whitespace
character. So s/\s*Edit Delete//
indicates to delete white
space characters followed by the words Edit Delete
.
I could also use \W
, instead; \W
represents
any non-alphanumeric character.
Related articles:
Reference:
-
Lesson 9: All this
whitespace
RegexOne - Learn Regular Expressions with simple, interactive exercises.