4

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.)

2 Answers 2

2

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 $

3
  • Yes! Amazing, exactly what I was hoping for, thanks a lot! Also, thank you for the explanation. :)
    – jonny
    Oct 30, 2011 at 22:45
  • 1
    Also consider a shorter regular expression: \(^ $\)\@!\&\s\+$.
    – ib.
    Oct 31, 2011 at 8:02
  • @ib. - nice, I was playing around with @ and & and couldn't get them working right.
    – evan
    Oct 31, 2011 at 8:34
1

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

match /\s\s\+$/
1
  • I love the idea but this will also hide single spaces at the end of regular lines that consist of more than a space.
    – jonny
    Oct 30, 2011 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.