jonny
jonny

Reputation: 4673

Vim: Match spaces at end of line but not lines consisting of a single space

I realise that in vim, I can highlight trailing spaces at the end of a line using

match /\s\+$/

Now I would like to exclude those lines that contain exactly one space from being matched. How do I go about doing this? (It does not need to be a single line/regex.)

Upvotes: 4

Views: 952

Answers (2)

evan
evan

Reputation: 12543

match /\(\S\zs\s\+$\)\|\(^\s\{2,}$\)/

This should work - breaking it down into 2 sections

Part 1 - search for spaces at the end of a line that has other stuff on the line: \(\S\zs\s\+$\)
not a space \S,
then start matching \zs,
1 or more spaces at the end of the line \s\+$

OR match \|

Part 2 - Search for more than one space which is the entire line: \(^\s\{2,}$\)
start at the beginning of the line ^
search for at least 2 spaces \s\{2,}
at the end of the line $

Upvotes: 2

Kai Sternad
Kai Sternad

Reputation: 22840

This matches all lines that contain more than one space, leaving out lines that contain one space.

match /\s\s\+$/

Upvotes: 1

Related Questions