首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用vim和plugins为所有行快速添加html属性和值?

如何使用vim和plugins为所有行快速添加html属性和值?
EN

Stack Overflow用户
提问于 2017-03-26 02:37:52
回答 10查看 795关注 0票数 5

我的操作系统:debian8 8。

代码语言:javascript
复制
uname -a
Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.39-1+deb8u2 (2017-03-07) x86_64 GNU/Linux

这是我的基本文件。

代码语言:javascript
复制
home 
help 
variables 
compatibility 
modelines 
searching 
selection 
markers 
indenting 
reformatting 
folding 
tags 
makefiles 
mapping 
registers 
spelling 
plugins 
etc

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

代码语言:javascript
复制
<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>

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

如何使用vim和plugins为所有行快速添加html属性和值?

sed,awk,崇高的文本3都是欢迎解决问题的。

EN

回答 10

Stack Overflow用户

回答已采纳

发布于 2017-03-28 09:17:41

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

代码语言:javascript
复制
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
票数 4
EN

Stack Overflow用户

发布于 2017-03-28 04:11:00

代码语言:javascript
复制
$ 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>
票数 5
EN

Stack Overflow用户

发布于 2017-03-28 16:50:18

如果您想在vi本身中执行此操作,则不需要插入。

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

代码语言:javascript
复制
%s:.*:<a href="&.html" id="&">&</a>

它将使文件中的所有替换。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43024359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档