
Search engines are a disappointing bunch. A variety of people have been complaining about the demise of the utility of Google, for example Tesla's Karpathy.
For many classes of topics/questions Google Search has become super SEO'd, surfacing very low quality often ad-heavy/paginated content. I find myself appending "reddit" to a lot of queries and often getting much better results.
— Andrej Karpathy (@karpathy) March 7, 2021
One of the areas where Google fails most is code search, due to its omission of allowing searching for special characters.
Once upon a time there was Google Code Search (or is again?), but that is currently not useful for searching, say, GitHub.
I find it very helpful to see usage examples when I work with new code, especially after Dash or the respective Language Server have left me with unhelpful documentation (or even worse: nothing).
Recently, due to Neovim's very own TJ (or here), I came across Sourcegraph which is very neat, since it not only respects special symbols but also allows funny things like regex searches, limit language searched etc.
Obviously, we want this integrated into Neovim and triggerable via shortcut, and since (Neo)vim is awesome like that, it's extremely easy to set this up with a little VimL magic (yes, yes, I should use Lua now that 0.5 is out but I just copy pasted this from earlier (sad), pre-Neovim times).
If you put the following into your .vimrc
, you're in luck.
vnoremap <silent> f8 "ay:call SearchSourceGraph()<CR>
nnoremap <silent> f8 viw"ay:call SearchSourceGraph()<CR>
function! UrlEncode(string)
let result = ""
let characters = split(a:string, '.\zs')
for character in characters
if character == " "
let result = result . "%20"
elseif CharacterRequiresUrlEncoding(character)
let i = 0
while i < strlen(character)
let byte = strpart(character, i, 1)
let decimal = char2nr(byte)
let result = result . "%" . printf("%02x", decimal)
let i += 1
endwhile
else
let result = result . character
endif
endfor
return result
endfunction
" Returns 1 if the given character should be percent-encoded in a URL encoded
" string.
function! CharacterRequiresUrlEncoding(character)
let ascii_code = char2nr(a:character)
if ascii_code >= 48 && ascii_code <= 57
return 0
elseif ascii_code >= 65 && ascii_code <= 90
return 0
elseif ascii_code >= 97 && ascii_code <= 122
return 0
elseif a:character == "-" || a:character == "_" || a:character == "." || a:character == "~"
return 0
endif
return 1
endfunction
function! SearchSourceGraph()
let url = @a
let encoded = UrlEncode(url)
let command = "silent !open 'https://sourcegraph.com/search?q=" . shellescape(encoded, 1) . "'"
execute command
endfunction
Note: Attentive readers will realize that you can (and I do!) use above code to search any URL-encoded query enabled website (like, Science forbid, Google).
E.g.
function! OpenURL()
let url = @a
let encoded = UrlEncode(url)
let command = "!open 'https://google.com/search?q=" . shellescape(encoded, 1) . "'"
execute command
endfunction
vnoremap fg "ay:call OpenURL()<CR> " fg for find google
nnoremap fg viw"ay:call OpenURL()<CR>
Of course, an obvious next improvement is to limit the search to the current file type, which you can just easily add to the query parameter:
function! SearchSourceGraph()
let url = @a
let encoded = UrlEncode(url)
let command = "silent !open 'https://sourcegraph.com/search?q=" . shellescape(encoded, 1) ."+lang:".&ft."'"
execute command
endfunction
That's it! Now we'll patiently wait until TJ finishes https://github.com/tjdevries/telescope-sourcegraph.nvim