MoonPoint Support Logo


Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals

Advanced Search
Sun Mon Tue Wed Thu Fri Sat

Tue, Jun 06, 2017 9:22 pm

Removing whitespace from lines in Vi

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:

  1. gVim Portable for Windows


  1. Lesson 9: All this whitespace
    RegexOne - Learn Regular Expressions with simple, interactive exercises.

[/software/editors/vi] permanent link

Once You Know, You Newegg AliExpress by

Shop Amazon Local - Subscribe to Deals in Your Neighborhood

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo