How to quickly add html attributes and values ​​for all lines using 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 an html file as shown below.

 <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> 

The href and id attributes were added to each line, the values ​​of which are the contents of the lines, the inserted .html and accordingly the contents of the line.

How to quickly add html attributes and values ​​for all lines using vim and plugins?
sed, awk, sublime text 3 are welcome to solve the problem.

+5
source share
10 answers

sed is the best solution (simple and fairly quick here) if you are confident in the content, if you do not need some complexity that awk handles better:

 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
source
 $ 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
source

if you want to do this in vi no plugin required

Open the file, type : and paste this line as a command

 %s:.*:<a href="&.html" id="&">&</a> 

it will do all the replacements in the file.

+5
source

To easily do this in Sublime Text, without adding additional plugins:

  • Open the base file in Sublime Text
  • Type Ctrl+Shift+P and enter syn html fuzzy search input type to set the file syntax in HTML.
  • In the View menu, make sure Word Wrap disabled.
  • Ctrl+A to select everything.
  • Ctrl+Shift+L to deselect multi-line editing.
  • Ctrl+C to copy the selection to the clipboard as multiple lines.
  • Alt+Shift+W to wrap each line with a tag - then tap a to convert the default <p> tag to a <a> tag (press esc ) to exit any context menu that may appear)
  • Enter a space , then href=" - you will see that it is added to each line, since they all have cursors. You should also note that Sublime automatically closed your quotes for you, so you have href="" with the cursor between quotation marks.
  • ctrl+v - the magic happens here - your clipboard contains every line containing the contents, so it inserts each corresponding value in quotation marks where the cursor lies. Then just enter .html to add the extension.
  • Use the right arrow to move the cursors outside the quotation marks for the href attribute and follow the two previous steps to add the id attribute with the id attached in the same way.
  • Voila! Everything is ready.

Multi-line editing is very powerful when you learn how to combine it with other keyboard shortcuts. This was a big improvement in my workflow. If you have any questions, please feel free to comment, and I will adjust if necessary.

+3
source

Try it -

 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 created 4 variables a, b, c and d, which you can edit of your choice.

OR

 while read -ri;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> 
+2
source

With bash single line:

 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 
+2
source

To execute it directly in vim:

 !sed 's:.*:<a href="&.html" id="&">&</a>:' % 
+2
source

There is no regular expression in awk, nothing, just a print line 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 have empty lines, just add /./ to { :

/./{print ...

+1
source
 list=$(cat basefile.txt) for val in $list do echo "<a href=\""$val".html\" id=\""$val"\">"$val"</a>" >> newfile.html done 

Using bash, you can always create a script or enter it on the command line.

0
source

This vim replacement template processes your base file:

 s#^\s*\(.\{-}\)\s*$#<a href="\1.html" id="\1">\1</a># 
  • ^\s* matches any leading spaces, then
  • .\{-} captures everything after that, not greedily - allowing
  • \s$ to match any trailing spaces.
  • This avoids things like <a href="home .html" id="home ">home </a> .

You can also process several basic files at once with vim:

 vim -c 'bufdo %s#^\s*\(.\{-}\)\s*$#<a href="\1.html" id="\1">\1</a># | saveas! %:p:r.html' some.txt more.txt` 
  • bufdo %s#^\s*\(.\{-}\)\s*$#<a href="\1.html" id="\1">\1</a># starts a replacement for each buffer loaded in vim
  • saveas! %:p:r.html saveas! %:p:r.html saves each buffer with the html extension, rewriting if necessary
  • vim will open and show you the saved more.html , which you can fix if necessary, and
  • you can use :n and :prev to visit some.html .

Something like seds is probably best suited for large jobs, but it allows you to set up conversions in vim right after they are created, use :u to cancel, etc. Enjoy it!

0
source

Source: https://habr.com/ru/post/1265910/


All Articles