William Chung
William Chung

Reputation: 35

vim remove whitespaces after preceding tabs

Can I remove tabs except indentations in vim? I.e. do not delete tabs on the beginning of each lines.

Here is an example where \t represents a tab:

SELECT \t\t col1, col2 \t
\t  SELECT  \t\t\t col1 \t\t col2
\t\t    SELECT  \t\t\t col1 \t\t col2

I want to delete all white spaces in my file except the tabs which is used for indentations as follows:

SELECT col1, col2
\t  SELECT col1, col2
\t\t    SELECT col1, col2

Upvotes: 0

Views: 197

Answers (2)

Nikita Kouevda
Nikita Kouevda

Reputation: 5756

:%s/[^\t]\zs\t\+//g

[^\t] matches anything other than a tab, \zs resets the start of the match, and \t\+ matches 1 or more tabs. Thus, all tabs following any non-tab character (i.e. not at the beginning of the line) are removed.

Upvotes: 2

Don Reba
Don Reba

Reputation: 14061

\(^\t*\)\@<= \s*

Here is what this means:

  • \(^\t*\) is a series of indenting tabs
  • \@<= means that what follows should be preceded by a series of indenting tabs
  • \s* is a space optionally followed by more whitespace

Upvotes: 0

Related Questions