-1

In Vim, how do you find the first 2 characters of every word in a file and replace it.

I'm actually looking to use matchadd for a plugin, but I'm struggling with vim's particular regex.

I have tried:

\w\{2}

But this gets the next set of 2 characters.

I have also tried:

\w\{2}\{-}

But this doesn't work as it's a syntax error.

I really want a non greedy regex on any 2 characters per word.

David
  • 2,715
  • 2
  • 22
  • 31

2 Answers2

2

Try with word-boundaries. In are \< for the beginning of word and \> for the end. I use very-magic option (\v) so I don't need to escape them:

:s/\v<\w{2}(\w*)>/\1/g
Birei
  • 35,723
  • 2
  • 77
  • 82
  • This was VERY helpful, but I get Pattern not found. – David Sep 17 '13 at 18:03
  • 1
    In `vim` if you apply a substitution command to a line without a match, it throws a **Pattern not found** error. You have to ensure before it that it will match with the `global` command. Look at its help page: `help global` – Birei Sep 17 '13 at 19:43
0

It turns out all I really needed was:

\<\w\{2}

Thanks Birei for the hints (upvoted). Note, for this to work properly, I did not need the "\>" etc.

David
  • 2,715
  • 2
  • 22
  • 31