Open Apple Dictionary From vim

Apparently you can open Apple’s dictionary from the command line. For example, we can look up the word “absentia” in the dictionary.

> open dict://absentia

When writing text, documentation or other non-code paragraphs, I sometimes find that I need to look up a synonym since I occasionally tend to reuse the same words over and over. I don’t want to remember to open my dictionary when I am writing and I do not need to look up a word in every session, so let us define a script-local function (denoted by the function’s s: prefix):

if has('mac') || has('macunix')
    " Open Dictionary.app on mac systems
    function! OpenDictionary(...)
        let word = ''

        if a:1 !=# ''
            let word = a:1
        else
            let word = shellescape(expand('<cword>'))
        endif

        call system("open dict://" . word)
    endfunction
endif

The function is only available on Mac systems thanks to the outermost gurads. The function checks if it got a single argument. If not, it defaults to getting the current word under the cursor (by expanding ‘'), then proceeds to open the dictionary with that word. Next, we define a command for the function.

if has('mac') || has('macunix')
    command! -nargs=? Dict call OpenDictionary(<q-args>)
endif

Executing :Dict absentia will now open the dictionary on the entry of “absentia”. Without an argument, it will open the dictionary with the current word under the cursor.