简体   繁体   中英

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?

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>

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM