Vim Tips Wiki
Advertisement
Tip 1073 Printable Monobook Previous Next

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


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:

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 them the same way identifiers are highlighted in your code.

The tip uses the classical regexp for matching IP addresses:

((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]?)

If you just want to match a simple IP address that is probably valid, then something like this would be simpler:

\d\{1,3}\%(\.\d\{1,3}\)\{3}

However, this will also match something like 999.999.999.999, which is obviously not valid.

Comments

Advertisement