微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用vim和插件快速添加所有行的html属性和值?

我的口碑:debian8. @H_404_5@

@H_404_5@

uname -a
Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.39-1+deb8u2 (2017-03-07) x86_64 GNU/Linux
@H_404_5@这是我的基本文件.

@H_404_5@

home 
help 
variables 
compatibility 
modelines 
searching 
selection 
markers 
indenting 
reformatting 
folding 
tags 
makefiles 
mapping 
registers 
spelling 
plugins 
etc
@H_404_5@我想创建一个html文件,如下所示.

@H_404_5@

<a href="home.html" id="home">home</a>
<a href="help.html" id="help">help</a>
<a href="variables.html" id="variables">variables</a>
<a href="compatibility.html" id="compatibility">compatibility</a>
<a href="modelines.html" id="modelines">modelines</a>
<a href="searching.html" id="searching">searching</a>
<a href="selection.html" id="selection">selection</a>
<a href="markers.html" id="markers">markers</a>
<a href="indenting.html" id="indenting">indenting</a>
<a href="reformatting.html" id="reformatting">reformatting</a>
<a href="folding.html" id="folding">folding</a>
<a href="tags.html" id="tags">tags</a>
<a href="makefiles.html" id="makefiles">makefiles</a>
<a href="mapping.html" id="mapping">mapping</a>
<a href="registers.html" id="registers">registers</a>
<a href="spelling.html" id="spelling">spelling</a>
<a href="plugins.html" id="plugins">plugins</a>
<a href="etc.html" id="etc">etc</a>
@H_404_5@每行都添加了href和id属性,其值是行内容粘贴.html和行内容本身相应的.

@H_404_5@如何使用vim和插件快速添加所有行的html属性和值?
sed,awk,sublime text 3都欢迎解决问题.

解决方法

如果你确定内容,sed是最好的解决方案(这里简单而且非常快),如果不是需要一些复杂性,awk可以更好地对待它: @H_404_5@

@H_404_5@

awk '
   {
   # change special char for HTML constraint 
   Org = URL = HTML = $0
   # sample of modification
   gsub( / /,"%20",URL)
   gsub( /</,"%3C",HTML)

   printf( "<a href=\"%s\" id=\"%s\">%s</a>\n",URL,Org,HTML)
   }
   ' YourFile

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐