Vim Tips Wiki
(Remove html character entities)
(Reviewed, cleaned up.)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=1073
 
|id=1073
Line 10: Line 9:
 
|rating=41/17
 
|rating=41/17
 
|category1=Advanced Regex
 
|category1=Advanced Regex
|category2=
+
|category2=Usage
 
}}
 
}}
If you edit files which contain IP addresses and would like to have them marked in special color, you can use the following code in your vimrc:
+
If a user is edit a file which contain IP addresses and would like to have them marked in a special color, adding the following code in their .vimrc file will highlight instances of IP addresses in the file being edited.
 
<pre>
 
<pre>
 
syn match ipaddr /\(\(25\_[0-5]\|2\_[0-4]\_[0-9]\|\_[01]\?\_[0-9]\_[0-9]\?\)\.\)\{3\}\(25\_[0-5]\|2\_[0-4]\_[0-9]\|\_[01]\?\_[0-9]\_[0-9]\?\)/
 
syn match ipaddr /\(\(25\_[0-5]\|2\_[0-4]\_[0-9]\|\_[01]\?\_[0-9]\_[0-9]\?\)\.\)\{3\}\(25\_[0-5]\|2\_[0-4]\_[0-9]\|\_[01]\?\_[0-9]\_[0-9]\?\)/
Line 18: Line 17:
 
</pre>
 
</pre>
   
This will highlight them the same way identifiers are highlighted in your code.
+
This will highlight IP Addresses the same way identifiers are highlighted in source code files. This tip uses the following regular expression to find the address to be highlighted.
 
The tip uses the classical regexp for matching IP addresses:
 
 
<pre>
 
<pre>
 
((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
 
((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
 
</pre>
 
</pre>
   
  +
Other regular expressions can be used to find and highlight other patterns within text.
If you just want to match a simple IP address that is probably valid, then something like this would be simpler:
 
<pre>
 
\d\{1,3}\%(\.\d\{1,3}\)\{3}
 
</pre>
 
 
However, this will also match something like 999.999.999.999, which is obviously not valid.
 
   
 
==Comments==
 
==Comments==

Revision as of 21:14, 10 January 2011

Tip 1073 Printable Monobook Previous Next

created December 9, 2005 · complexity basic · author Matous Jan Fialka · version 6.0


If a user is edit a file which contain IP addresses and would like to have them marked in a special color, adding the following code in their .vimrc file will highlight instances of IP addresses in the file being edited.

syn match ipaddr /\(\(25\_[0-5]\|2\_[0-4]\_[0-9]\|\_[01]\?\_[0-9]\_[0-9]\?\)\.\)\{3\}\(25\_[0-5]\|2\_[0-4]\_[0-9]\|\_[01]\?\_[0-9]\_[0-9]\?\)/
hi link ipaddr Identifier

This will highlight IP Addresses the same way identifiers are highlighted in source code files. This tip uses the following regular expression to find the address to be highlighted.

((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)

Other regular expressions can be used to find and highlight other patterns within text.

Comments