Skip to content


A vim one-liner for expanding RDF namespace prefixes

I often make use of prefix.cc to expand RDF namespace prefixes, e.g. expanding
foaf -> http://xmlns.com/foaf/0.1/
dcterms -> http://purl.org/dc/terms/

They’ve got a great API for using the site, and can output the expanded prefixes in a bunch of formats.

I usually edit files using vim, so I figured that being able to use prefix.cc without having to leave the editor or change windows would be pretty handy.

After a few attempts, I came up with the macro below. It binds ‘\p’ (type a backslash then a ‘p’ character into vim from command mode) to a macro that takes the word the cursor is under, queries prefix.cc with it, and replaces the original word with the expanded namespace, or with the text NOTFOUND.

To use it, first bind the macro to a \p by running the following in vim (make sure it’s all on one line!):

:nmap <silent> \p <ESC>:let @p = system("CO=$(curl -s http://prefix.cc/".expand("<cword>").".file.txt);NL=$(echo \"$CO\"\|wc -l);if [ \"$NL\" -gt 1 ];then echo -n \"NOTFOUND\";else echo \"$CO\"\|cut -f2\|tr -d '\n';fi")<CR>ciw<C-r>p<ESC>

Or to use it permanently, remove the leading ‘:’ character, and copy it into your ~/.vimrc file.

To test it:

1. Enter the word ‘foaf’ somewhere in a text file
2. Move the cursor over the word ‘foaf’ in command mode
3. Press ‘\’ then press ‘p’
4. The word foaf should be replaced with http://xmlns.com/foaf/0.1/

Any suggestions for improvements are welcome!

Caveats!

  • This has only been tested on Ubuntu linux, results may vary on other systems
  • The macro uses the named buffer "p in vim, so will need changing if you use this for anything important
  • The macro assumes you’ve got curl installed (it’s used for querying prefix.cc)

Posted in Command Line, RDF.

Tagged with , , , , .


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.