Fandom

Vim Tips Wiki

Add html tags with substitute

1,624pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Tip 1319 Printable Monobook Previous Next

created September 4, 2006 · complexity basic · author Sérgio Luiz Araújo Silva · version 6.0


Suppose you have a table of numbers like:

1 12 3 13 11 55 67
1 12 3 13 11 55 67
1 12 3 13 11 55 67
1 12 3 13 11 55 67
1 12 3 13 11 55 67

To generate html tags of form <td>1</td> use command:

:%s/\(\s\+\)\(\d\+\)/<td>\2<\/td>/g

The result:

<td>1</td><td>12</td><td>3</td><td>13</td><td>11</td><td>55</td><td>67</td>
<td>1</td><td>12</td><td>3</td><td>13</td><td>11</td><td>55</td><td>67</td>
<td>1</td><td>12</td><td>3</td><td>13</td><td>11</td><td>55</td><td>67</td>
<td>1</td><td>12</td><td>3</td><td>13</td><td>11</td><td>55</td><td>67</td>
<td>1</td><td>12</td><td>3</td><td>13</td><td>11</td><td>55</td><td>67</td>

To generate words like:

<td>uma</td><td>porção</td><td>de</td><td>palavras</td><td>para</td><td>testes</td><td>deve</td>
<td>ser</td><td>digitada</td><td>após</td><td>isto</td><td>coloque</td><td>espaço</td><td>no</td>
<td>inicio</td><td>de</td><td>cada</td><td>linha</td>

use commands:

:%s/^/ /g
:%s/\(\s\+\)\(\S\+\)/<td>\2<\/td>/g

CommentsEdit

:%s,\(\d\+\),<td>\1</td>,g

Also on Fandom

Random Wiki