How to use VI to search for lines less than a certain length?
Asked Answered
B

2

36

I know how to use VI to find lines longer than a certain number of characters:

/\%>80v.\+

But how do I search for lines shorter than a certain number of characters?

Boutwell answered 11/5, 2011 at 14:28 Comment(0)
H
48

Use

/^.\{,25}$/

to find lines shorter than 25 characters.

Hash answered 11/5, 2011 at 15:18 Comment(7)
Great, if you appreciate the answer, you should up vote it and/or accept it. see the faq.Hash
This finds lines less than or equal to 25 characters in my copy of Vim.Yellowthroat
A brief explanation on whats going on would be nice ! :)Motivate
\{n,m} matches from n to m of the preceding characters. \{,m} matches at most m (from 0 to m) of the preceding characters. . matches any character. ^ matches the beginning of a line. $ matches the end of a line. hence: match a line which contains from beginning to end up to m characters of any type.Extirpate
Great answer! However I think the second curly brace should be escaped too: /^\{,25\}$/.Chartres
@Chartres the second backslash is allowed but not required.Hash
@BasBossink Thanks for the reply. I didn't know that. I always escape both curly braces, it is more intuitive and comprehensible in my opinion.Chartres
G
0

Tried this on a large file and had to kill -9 vim after it didn´t finish after a few minutes (!). I love vim but this disappointed me somewhat. I wonder if this is implemented as a trendy new python plug-in or something. To find short lines in a more time-effective way I find that it is better to use

awk  '{ if(length<YOURLENGTH) { print $0 }}'
Graecoroman answered 25/8, 2023 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.