0

I want to search in Vi for two tabs but not for three or more tabs. I tried several things like this:

/\t\t\(\t\)@!

But nothing works. Any idea?

3

:/\t\t(\t)@!

Your regular expression is almost valid, except that you need to use \@ instead of @.

However first note, that using :/ means the cursor will be put on the line matching the regular expression (this is usually used as an address, see :help :/), which is different from searching. For searching use the / command.

Second, using \t\{2\}\t\@! should work, but note, that this will match lines having more than two tabs, because the match would simply start at a later \t. So you need to anchor your match at the beginning to match non-tabs as well. Something like this might work:

/\%([^\t]\|^\)\zs\t\{2\}\t\@!

Which comes down to:

/         - start a search command
\%(...\)  - start a group
[^\t]     - match a non-tab
\|        - OR
^         - start of line
\zs       - start match now (not the previous char)
\t\{2\}   - at least 2 tabs
\t\@!     - not followed by another tab

Alternatively you can use /\t\@<!\t\{2\}\t\@! which is similar to the previous expression, except that it uses a zero-width negative look-behind assertion.

Note that those look-around assertions can be slow in Vim.

  • Thanks, that worked. I found one issue: I got also matches with three tabs where the match starts at the second tab. I encountered this by adding a ^. – musbach Feb 21 '18 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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