Skip to content

Instantly share code, notes, and snippets.

@yjsoon
Created August 27, 2012 03:22
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save yjsoon/3485271 to your computer and use it in GitHub Desktop.
Save yjsoon/3485271 to your computer and use it in GitHub Desktop.
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>
@rizzatti
Copy link

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
Copy link

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