Your Linux Data Center Experts

Google has an excellent tool to allow for command-line access to parts of their API. I'm especially fond of the ability to edit documents using vim. I recently bought a Nexus 7 tablet and wanted to cleanly view many of these documents upon it but the default width of about 65 characters varied greatly from my default vim setup and made for some less than smooth reading when viewed on the Nexus 7. Read on for how I was able to modify both my environment and ~/.vimrc to make this process very smooth.

First off, if you aren't familiar with the google command-line tools, read up and install them:

The basic use of the “google” command to edit a document using vim looks like this:

   google docs edit --title 'Test Plans' --editor vim

To get vim to use some different settings when running in this way takes two parts. The first is an entry in your ~/.vimrc to test for an environment variable and then if that variable is not null we'll run some commands:

    " Place this block after similar settings so they don't get overridden.
    if $GDOC != ""
        " Delete duplicate blank lines, which this API currently does.
        autocmd VimEnter * exe 'g/^\_$\n\_^$/d'
        autocmd VimEnter * exe '1'
        " Set the textwidth that we're working with.
        set textwidth=65
        " Handy paragraph formatting.
        set formatprg=par\ -w65\ -gq
        nnoremap  gqip
    endif

Now to trigger this section of our ~/.vimrc we'll modify the above command line using a bash function, added to your ~/.bashrc or similar file:

    function gvi()
    {
        GDOC=1 google docs edit --title "$*" --editor vim
    }

Now to edit our file with this alias. First re-source your ~/.bashrc then run the function with the name of your document (whitespace is OK):

    $ . ~/.bashrc
    $ gvi Test Plans

That's it in a nutshell. Now whenever you use the gvi function you'll include those specific vim settings. And these documents look great on my Nexus 7!

As a footnote, you can also specify some vim command-line arguments on the google command line. So if you preferred to use a different file for your vim settings you certainly can. The command would look like this:

   google docs edit --title 'Some File' --editor 'vim -u .othervimrc'

Enjoy!

comments powered by Disqus

Join our other satisfied clients. Contact us today.