Skip to content

Instantly share code, notes, and snippets.

@ChrisBuchholz
Forked from yjsoon/gist:3485271
Last active December 17, 2015 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisBuchholz/5557954 to your computer and use it in GitHub Desktop.
Save ChrisBuchholz/5557954 to your computer and use it in GitHub Desktop.
" 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.":"
let s:searchType = ""
if a:0 > 0
" 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:"
elseif match(s:fileType, "cpp") != -1 || match(s:fileType, "cc") != -1
let s:searchType = "cpp:"
elseif match(s:fileType, "go") != -1
let s:searchType = "go:"
elseif match(s:fileType, "scss") != -1
let s:searchType = "sass:"
endif
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>
map <leader>fd :call SearchDash(1)<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment