My os: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
Here is my base file.
home
help
variables
compatibility
modelines
searching
selection
markers
indenting
reformatting
folding
tags
makefiles
mapping
registers
spelling
plugins
etc
I want to create a html file as bellow.
<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>
Every line was added href and id attributes,whose values are line content pasted .html
and line content itself correspondingly.
How to add html attributes and values for all lines quickly with vim and plugins?
sed,awk,sublime text 3 are all welcomed to solve the problem.
$ 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>
if you want to do this in vi
itself, no plug-in neccessary
Open the file, type :
and insert this line as the command
%s:.*:<a href="&.html" id="&">&</a>
it will make all the substitutions in the file.
sed is the best solution (simple and pretty fast here) if your are sure of the content, if not it need a bit of complexity that is better treated by 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
To complete this easily in Sublime Text, without any plugins added:
Ctrl+Shift+P
and in the fuzzy search input type syn html
to set the file syntax to HTML.View
menu, make sure Word Wrap
is toggled off.Ctrl+A
to select all.Ctrl+Shift+L
to break selection into multi-line edit.Ctrl+C
to copy selection into clipboard as multiple lines.Alt+Shift+W
to wrap each line with a tag-- then tap a
to convert the default <p>
tag into an <a>
tag (hit esc
to quit out of any context menus that might pop up)space
then href="
-- you should see this being added to every line as they all have cursors. Also you should note that Sublime has automatically closed your quotes for you, so you have href=""
with the cursor between the quotes.ctrl+v
-- this is where the magic happens-- your clipboard contains every lines worth of contents, so it will paste each appropriate value into the quotes where the cursor is lying. Then you simply type .html
to add the extension.href
attribute and follow the two previous steps to similarly add an id
attribute with the intended id
s pasted in.Multi-line editing is very powerful as you learn how to combine it with other keyboard shortcuts. It has been a huge improvement in my workflow. If you have any questions please feel free to comment and I'll adjust as needed.
With bash
one-liner:
while read v; do printf '<a href="%s.html" id="%s">%s</a>\n' "$v" "$v" "$v"; done < file
(OR)
while read v; do echo "<a href=\"$v.html\" id=\"$v\">$v</a>"; done < file
Try this -
awk '{print a$1b$1c$1d}' a='<a href="' b='.html" id="' c='">' d='</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>
Here I have created 4 variable a,b,c & d which you can edit as per your choice.
OR
while read -r i;do echo "<a href=\"$i.html\" id=\""$i"\">"$i</a>";done < f
<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>
To execute it directly in vim:
!sed 's:.*:<a href="&.html" id="&">&</a>:' %
In awk, no regex, no nothing, just print
strings around $1
s, escaping "
s:
$ awk '{print "<a href=\"" $1 ".html\" id=\"" $1 "\">" $1 "</a>"}' file
<a href="home.html" id="home">home</a>
<a href="help.html" id="help">help</a>
If you happen to have empty lines in there just add /./
before the {
:
/./{print ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With