Search code examples
vimvi

Is substitution linking possible in the vi editor?


I am trying to edit with two conditions in the vi.

example text)

hello world!
-apple watermelon
test text

two condition must be met.

  1. start with -
  2. I want to replace only the first letter of a word with a html tag (The first letter may not be the alphabet)

I tried substitution via pipe in vi, but i cant

Expected

hello world!
-<b>a</b>pple <b>w<b>atermelon
test text

Solution

  • You can use

    :g/^-/s#\<\(.\)#<b>\1</b>#g
    

    Meaning:

    • for every line that starts with - (:g/^-/)
    • substitute
      • the first character after a break (\<\(.\)) (while capturing it)
      • replace it with <b>\1</b> (\1 referencing the captured value)
      • multiple times per line (the g flag, remove this unless :set gdefault? returns nogdefault)