Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add html attributes and values for all lines quickly with vim and plugins?

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.

like image 893
showkey Avatar asked Mar 26 '17 02:03

showkey


8 Answers

$ 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>
like image 150
Ed Morton Avatar answered Sep 29 '22 20:09

Ed Morton


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.

like image 21
karakfa Avatar answered Sep 29 '22 21:09

karakfa


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
like image 43
NeronLeVelu Avatar answered Sep 29 '22 21:09

NeronLeVelu


To complete this easily in Sublime Text, without any plugins added:

  1. Open the base file in Sublime Text
  2. Type Ctrl+Shift+P and in the fuzzy search input type syn html to set the file syntax to HTML.
  3. In the View menu, make sure Word Wrap is toggled off.
  4. Ctrl+A to select all.
  5. Ctrl+Shift+L to break selection into multi-line edit.
  6. Ctrl+C to copy selection into clipboard as multiple lines.
  7. 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)
  8. Type a 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.
  9. 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.
  10. Use the right arrow to move the cursors outside of the quotes for the href attribute and follow the two previous steps to similarly add an id attribute with the intended ids pasted in.
  11. Voila! You're done.

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.

like image 40
Alexander Nied Avatar answered Sep 29 '22 21:09

Alexander Nied


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
like image 38
sat Avatar answered Sep 29 '22 19:09

sat


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>
like image 37
VIPIN KUMAR Avatar answered Sep 29 '22 21:09

VIPIN KUMAR


To execute it directly in vim:

!sed 's:.*:<a href="&.html" id="&">&</a>:' %
like image 40
showkey Avatar answered Sep 29 '22 21:09

showkey


In awk, no regex, no nothing, just print strings around $1s, 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 ...

like image 39
James Brown Avatar answered Sep 29 '22 19:09

James Brown