Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #1073 - Match valid IP address

Created: December 9, 2005 5:18 Complexity: basic Author: Matous Jan Fialka Version: 6.0 Karma: 41/17 Imported from: Tip#1073

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 might be simpler:

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

The only difference is that this will also match something like 999.999.999.999, which is obviously not valid.

Comments

Feel free to add here any comments!


Advertisement