Home > Uncategorized > Vim: highlight repeated properties of property file

Vim: highlight repeated properties of property file

While refactoring some localization property files I found that many messages and properties were repeated. This might be due to the lack of a naming convention for the strings or maybe because the properties were not sorted alphabetically.

For example, in one of the files the string Upload was repeated in three different places:


After normalizing the file I ended up with:


Before deleting the duplicated properties I wanted to check if the string they had was the same in all occurrences. With that in mind, I decided to make a little vim function that would highlight all the duplicated property names. For that you have to write the following in your .vimrc file:

function! HighlightRepeatedProps() range
let propCounts = {}
let lineNum = a:firstline
while lineNum <= a:lastline
let lineText = getline(lineNum)
if lineText != ""
let propName = matchstr(lineText, "^[^=]*")
let propCounts[propName] = (has_key(propCounts, propName) ?
propCounts[propName] : 0) + 1
let lineNum = lineNum + 1
exe 'syn clear Repeat'
for propName in keys(propCounts)
if propCounts[propName] >= 2
exe 'syn match Repeat
"^' . escape(propName, '".\^$*[]') . '=.*$"'

command! -range=% HighlightRepeatedProps <line1>,<line2>call HighlightRepeatedProps()

After saving your changes on .vimrc open your .properties file and type :HighlightRepeatedProps, if you have any repeated property names vim will highlight them in a different colour.

Useful, right?

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: