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

it_*_*ure 5 html awk sed sublimetext3 emmet

我的口碑:debian8.

uname -a
Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.39-1+deb8u2 (2017-03-07) x86_64 GNU/Linux
Run Code Online (Sandbox Code Playgroud)

这是我的基本文件.

home 
help 
variables 
compatibility 
modelines 
searching 
selection 
markers 
indenting 
reformatting 
folding 
tags 
makefiles 
mapping 
registers 
spelling 
plugins 
etc
Run Code Online (Sandbox Code Playgroud)

我想创建一个html文件,如下所示.

<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>
Run Code Online (Sandbox Code Playgroud)

每行都添加了href和id属性,其值是行内容粘贴.html 和行内容本身相应的.

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

Ed *_*ton 5

$ sed 's:.*:<a href="&.html" id="&">&</a>:' file
<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>
Run Code Online (Sandbox Code Playgroud)


kar*_*kfa 5

如果你想这样做vi,没有必要的插件

打开文件,键入:并插入此行作为命令

%s:.*:<a href="&.html" id="&">&</a>
Run Code Online (Sandbox Code Playgroud)

它将在文件中进行所有替换.


Ner*_*elu 4

如果您确定内容,sed 是最好的解决方案(这里简单且相当快),如果不是,则需要一点复杂性,awk 可以更好地处理这一点:

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
Run Code Online (Sandbox Code Playgroud)