user3622460
user3622460

Reputation: 1251

VIM how to add certain html tags to the start or end of a line?

using... 2,40s/^/foo I can add stuff to the start of line(s) in VIM.

using... 2,40s/$/foo I can add stuff to the end of line(s) in VIM.

My question is... How can I add certain html tags to the end or start of a line in vim?

Example..

2,40s/$/</li> - This command does not work. I can add <li> to the end or start, but when I add '/' to it making the closing tag </li> it doesn't allow it.

I also came across this problem when trying to add 2,40s/^/&nbsp to the start of some lines. The '&' character breaks the replace.

Is it possible to be able to add certain tags to vim lines, such as </li> or & anything?

Upvotes: 0

Views: 259

Answers (2)

Xavier T.
Xavier T.

Reputation: 42218

The other approach would be to use visual block mode with CtrlV, select all the line, then use A, type your tag </foo> , then ESC.

It will add </foo> at the end of every line.

Upvotes: 0

kingdaemon
kingdaemon

Reputation: 1686

You need to escape special characters like / and & with \

Example:

2,40s/^/<pre>
2,40s/$/<\/pre>
2,40s/^/\&nbsp;

If you don't want to escape slashes you can use custom delimiter, in this case #:

2,40s#$#</pre>

Upvotes: 1

Related Questions