0

Data format:

ab b c
ab b
abc d e
abcd e

Each row could be 2 or 3 columns, delimited by a white space. I want to match any row in which the first column length is 4. How do I do this?

I've tried to do the following:

/.... \{_}

But VIM reports a syntax error. Why is that?

wxz
  • 2,254
  • 1
  • 10
  • 31
marlon
  • 6,029
  • 8
  • 42
  • 76
  • 1
    I don't quite understand how your version is supposed to work, but `/^[^ ]\{4} .*$` should work. The first `^` matches the start of a line (so we know we're looking at the first column), the `[^ ]\{4}` matches 4 characters that aren't spaces (so we don't match if it's fewer than 4 characters), the space after that matches a space (so we don't match a column with more than 4 characters), and the `.*$` makes sure we match all the way to the end of the line (not sure if you actually care about that... if you're just seeking you won't, but if you're replacing or something you might). – EdmCoff Aug 06 '21 at 18:13
  • @EdmCoff could you make that an answer? – wxz Aug 06 '21 at 18:17
  • Also, this is an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You want to know how to do a vim search on a 4 letter word, but you ask about why your solution doesn't work. – wxz Aug 06 '21 at 18:20
  • 1
    @wxz Sure, done. – EdmCoff Aug 06 '21 at 18:20

1 Answers1

3

Moving my comment into an answer. I suggest using /^[^ ]\{4} .*$

The first ^ matches the start of a line (so we know we're looking at the first column), the [^ ]\{4} matches 4 characters that aren't spaces (so we don't match if it's fewer than 4 characters), the space after that matches a space (so we don't match a column with more than 4 characters), and the .*$ makes sure we match all the way to the end of the line (not sure if you actually care about that... if you're just seeking within the file it won't matter, but if you're replacing or something you might want it).

EdmCoff
  • 3,506
  • 1
  • 9
  • 9
  • 1
    I tried this just now on a random file I have, tab/indents are not considered a space so this search still came up with a line starting with a tab for me, so I added the tab character `\t` as one of the characters to not match any whitespace including spaces and tabs. `/^[^ \t]\{4} .*$` – wxz Aug 06 '21 at 18:23
  • What about if you want to use a non-greedy version of the command? I find often I need to use non-greedy search, but didn't get it worked. – marlon Aug 06 '21 at 18:32
  • @marlon What do you mean "greedy" vs "non-greedy"? – wxz Aug 06 '21 at 18:33
  • https://stackoverflow.com/questions/1305853/how-can-i-make-my-match-non-greedy-in-vim. Following this example, I still didn't get it to work. – marlon Aug 06 '21 at 18:34
  • @marlon Are you referring to `\{-}`? If so, what you wrote in your question was an underscore `\{_}` and not a dash. – wxz Aug 06 '21 at 18:37
  • 1
    I don't understand what non-greedy means if you're matching exactly 4 characters at the start of a line followed by a space. You're either matching the first column or the whole line (based on first column). There's no ambiguity where greediness would affect whether you get a longer or shorter match. – EdmCoff Aug 06 '21 at 18:39