Instantly share code, notes, and snippets.

Embed
What would you like to do?
vim search Dash for word under cursor, filetype-specific
" Searches Dash for the word under your cursor in vim, using the keyword
" operator, based on file type. E.g. for JavaScript files, I have it
" configured to search j:term, which immediately brings up the JS doc
" for that keyword. Might need some customisation for your own keywords!
function! SearchDash()
" Some setup
let s:browser = "/usr/bin/open"
let s:wordUnderCursor = expand("<cword>")
" Get the filetype (everything after the first ., for special cases
" such as index.html.haml or abc.css.scss.erb)
let s:fileType = substitute(expand("%"),"^[^.]*\.","",1)
" Alternative ways of getting filetype, aborted
" let s:fileType = expand("%:e")
" let s:searchType = b:current_syntax.":"
" Match it and set the searchType -- make sure these are the right shortcuts
" in Dash! Sort by priority in the match list below if necessary, because
" Tilt-enabled projects may have endings like .scss.erb.
if match(s:fileType, "js") != -1
let s:searchType = "js:" " can assign this to jQuery, too
elseif match(s:fileType, "css") != -1
let s:searchType = "css:"
elseif match(s:fileType, "html") != -1
let s:searchType = "html:"
elseif match(s:fileType, "rb") != -1
let s:searchType = "rb:" " can assign this to Rails, too
elseif match(s:fileType, "php") != -1
let s:searchType = "php:"
elseif match(s:fileType, "py") != -1
let s:searchType = "python:"
else
let s:searchType = ""
endif
" Run it
let s:url = "dash://".s:searchType.s:wordUnderCursor
let s:cmd ="silent ! " . s:browser . " " . s:url
execute s:cmd
redraw!
endfunction
map <leader>d :call SearchDash()<CR>
@sirupsen

This comment has been minimized.

Copy link

sirupsen commented Mar 9, 2013

It might be simpler just to match on &filetype.

@durden

This comment has been minimized.

Copy link

durden commented Mar 12, 2013

I'm running the new version of Dash (1.8.0) and using this. It works great, but this 'advanced' version searches for 'python:' when I'm in a Python file. This triggers a google/stackoverflow search, not a search for Python specific documentation.

I assumed (maybe incorrectly) that the intent was to search for the Python documentation specifically, not the Internet. Is this assumption incorrect, or am I doing something wrong?

@durden

This comment has been minimized.

Copy link

durden commented Mar 13, 2013

Ah ha! I figure out what I was doing wrong with some help on Twitter from the developer. Just for reference if anyone runs into this I had python 2 and python 3 docsets installed. So my shortcuts for python were 'python2:' and 'python3' by default. Thus, 'python:' didn't match.

I just 'fixed' this by changing my search terms in dash to both map to 'python:'.

It's not ideal, but not sure the best way to detect this in the vim script unless you somehow detect what interpreter was referenced in the #! line (if it exists). Either way, this is a great little trick.

@gordolio

This comment has been minimized.

Copy link

gordolio commented Mar 13, 2013

I prefer having just this in my vimrc... you can expand it for any language you want. It knows the language based on the current filetype (:set ft?) rather than looking at the file's extension.
You can use <leader>d or :Dash

au FileType javascript command Dash call SearchDash('javascript:')
au FileType vim command Dash call SearchDash('vim:')
au FileType perl command Dash call SearchDash('perl:')
au FileType ruby command Dash call SearchDash('ruby:')
map <leader>d :Dash<cr>
function SearchDash(lib)
  let s:url = "dash://".a:lib.expand("<cword>")
  execute "silent ! /usr/bin/open ". s:url
  redraw!
endfunction
@rizzatti

This comment has been minimized.

Copy link

rizzatti commented Mar 14, 2013

I usually end up with autocommands in my projects that will make 'filetype' look like qt5.cpp or rspec.rails.ruby, for finner control over snip engines and other things. I use Dash all the time, and thought this gists were really nice, so I put this together:
https://github.com/zehrizzatti/dash.vim
I intend to add those refinements to filetype/docset lookup shortly.

@ChrisBuchholz

This comment has been minimized.

Copy link

ChrisBuchholz commented May 10, 2013

Hey,

I have added a few more file types and the ability to choose whether or not to search with the detected file type. Check it out: https://gist.github.com/ChrisBuchholz/5557954

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment