{"id":707,"date":"2011-04-11T19:25:43","date_gmt":"2011-04-11T19:25:43","guid":{"rendered":"http:\/\/blog.soton.ac.uk\/webteam\/?p=707"},"modified":"2011-04-15T08:45:24","modified_gmt":"2011-04-15T08:45:24","slug":"a-vim-one-liner-for-expanding-rdf-namespace-prefixes","status":"publish","type":"post","link":"https:\/\/blog.soton.ac.uk\/webteam\/2011\/04\/11\/a-vim-one-liner-for-expanding-rdf-namespace-prefixes\/","title":{"rendered":"A vim one-liner for expanding RDF namespace prefixes"},"content":{"rendered":"<p>I often make use of <a href=\"http:\/\/prefix.cc\/\">prefix.cc<\/a> to expand RDF namespace prefixes, e.g. expanding<br \/>\nfoaf -> http:\/\/xmlns.com\/foaf\/0.1\/<br \/>\ndcterms -> http:\/\/purl.org\/dc\/terms\/<\/p>\n<p>They&#8217;ve got a great API for using the site, and can output the expanded prefixes in a <a href=\"http:\/\/prefix.cc\/about\/formats\">bunch of formats<\/a>.<\/p>\n<p>I usually edit files using <a href=\"http:\/\/www.vim.org\/\">vim<\/a>, so I figured that being able to use prefix.cc without having to leave the editor or change windows would be pretty handy.<\/p>\n<p>After a few attempts, I came up with the macro below.  It binds &#8216;\\p&#8217; (type a backslash then a &#8216;p&#8217; 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.<\/p>\n<p>To use it, first bind the macro to a \\p by running the following in vim (make sure it&#8217;s all on one line!):<\/p>\n<p><code>:nmap &lt;silent&gt; \\p &lt;ESC&gt;:let @p = system(\"CO=$(curl -s http:\/\/prefix.cc\/\".expand(\"&lt;cword&gt;\").\".file.txt);NL=$(echo \\\"$CO\\\"\\|wc -l);if [ \\\"$NL\\\" -gt 1 ];then echo -n \\\"NOTFOUND\\\";else echo \\\"$CO\\\"\\|cut -f2\\|tr -d '\\n';fi\")&lt;CR&gt;ciw&lt;C-r&gt;p&lt;ESC&gt;<\/code><\/p>\n<p>Or to use it permanently, remove the leading &#8216;:&#8217; character, and copy it into your ~\/.vimrc file.<\/p>\n<p>To test it:<\/p>\n<p>1. Enter the word &#8216;foaf&#8217; somewhere in a text file<br \/>\n2. Move the cursor over the word &#8216;foaf&#8217; in command mode<br \/>\n3. Press &#8216;\\&#8217; then press &#8216;p&#8217;<br \/>\n4. The word foaf should be replaced with http:\/\/xmlns.com\/foaf\/0.1\/<\/p>\n<p>Any suggestions for improvements are welcome!<\/p>\n<h2><strong>Caveats!<\/strong><\/h2>\n<ul>\n<li>This has only been tested on Ubuntu linux, results may vary on other systems<\/li>\n<li>The macro uses the named buffer <code>\"p<\/code> in vim, so will need changing if you use this for anything important<\/li>\n<li>The macro assumes you&#8217;ve got <a href=\"http:\/\/en.wikipedia.org\/wiki\/CURL\">curl<\/a> installed (it&#8217;s used for querying prefix.cc)\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[73,136,74],"tags":[4230,4229,803363,4228],"class_list":["post-707","post","type-post","status-publish","format-standard","hentry","category-command-line","category-rdf","category-tips","tag-namespace","tag-prefix","tag-rdf","tag-vim"],"_links":{"self":[{"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/posts\/707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/comments?post=707"}],"version-history":[{"count":16,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/posts\/707\/revisions"}],"predecessor-version":[{"id":723,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/posts\/707\/revisions\/723"}],"wp:attachment":[{"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/media?parent=707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/categories?post=707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/tags?post=707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}