jerik
jerik

Reputation: 5767

add character before first word in line

I want to add a minus sign "-" infront of the first word in a line on the editor VIM. The lines contains spaces for indentation. The indentation shall not be touched. E.g

As Is

  list point 1
      sub list point 2
  and so on... 

I want

  - list point 1
      - sub list point 2
  - and so on... 

I can find the first word, but i struggle with replacing it in the correct way.

^\s*\w

in Vim

/^\s*\w

But in the replacement I always remove the complete found part....

:s/^\s*\w/- \w/

Which leads to

- ist point 1
- ub list point 2
- nd so on...     

Upvotes: 1

Views: 242

Answers (4)

romainl
romainl

Reputation: 196556

I'm late to the party but:

:%norm! I- <CR>

And another one with :s:

:%s/^\s*/&- /

Upvotes: 2

Kent
Kent

Reputation: 195059

:normal cmd may help too:

:%norm! wi-

note that after - there is a space.

Upvotes: 1

hek2mgl
hek2mgl

Reputation: 157992

An alternative to falsetrue's answer: You can capture the first word character and print it out along with the leading -:

%s/\(\w\)/- \1/

Upvotes: 1

falsetru
falsetru

Reputation: 369074

Use & which is replaced with the matched string:

:%s/\w/- &

Upvotes: 4

Related Questions