Quickly access Terraform docs from the CLI or Vim

As I've mentioned before, I use Terraform every day at work, and am constantly looking up the documentation for the many resources. I've had a simple shell script called tfdoc to speed up looking at docs for a while, found here:


Whilst in vim I had been using !tfdoc aws_resource_name for a while, but finally came up with something a little nicer, by adding a simple vim command:

" e.g. :Tfdoc aws_instance
"      :Tfdoc -d aws_instance
if executable('tfdoc')  
    command! -nargs=* Tfdoc :call system('tfdoc' . ' ' . <q-args>)

That's nice, I can now just run :Tfdoc aws_lb to lookup a resource by name and :Tfdoc -d aws_lb for the data source, but we can do better than that.

Next, I added some mappings:

nnoremap <silent> <Leader>tfr :Tfdoc <C-R><C-W><CR>  
nnoremap <silent> <Leader>tfd :Tfdoc -d <C-R><C-W><CR>  
xnoremap <silent> <Leader>tfr y:Tfdoc <C-R>"<CR>  
xnoremap <silent> <Leader>tfd y:Tfdoc -d <C-R>"<CR>  

By pressing the leader key + tfr or tfd I can lookup the resource/data source under the cursor, or if the name is selected in visual mode.