Skip to content

Instantly share code, notes, and snippets.

@shmup
Created October 8, 2018 23:43
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 shmup/94e13bfb588d3e02dea8f95fdd0bb113 to your computer and use it in GitHub Desktop.
Save shmup/94e13bfb588d3e02dea8f95fdd0bb113 to your computer and use it in GitHub Desktop.
search duckduckgo (or google) via vim's keywordprg

Search DuckDuckGo (or Google) via vim's keywordprg

This will search DDG or Google (using Firefox) with the filetype+keyword and go to the first result

This is related to :help keywordprg and pressing K on a keyword

Probably you can swap firefox with chrome in the snippets, idk

Put this in your $HOME/.vim/vimrc and swap the commented out line if you don't use vim-dispatch

" don't mess with these filetypes (use the default keywordprg)
let ignorelist =['python','vim','help']

autocmd! Filetype * if (index(ignorelist, &ft) == -1)
  \ | let &l:keywordprg=":Dispatch " . fnamemodify($MYVIMRC, ":h") . "/search.sh " . &l:filetype | endif
  "\ | let &l:keywordprg=fnamemodify($MYVIMRC, ":h") . "/search.sh " . &l:filetype | endif

Store this as $HOME/.vim/search.sh and swap the commented out line if you prefer google

#!/usr/bin/env bash
# remove the +! from DDG or the btnI= from google if you don't want to navigate to the first result

firefox "https://duckduckgo.com/?q=${1//[.]/+}+%22$2%22+!"
# firefox "https://google.com/search?btnI=&q=${1//[.]/+}+$2"
@pbnj
Copy link

pbnj commented Jan 5, 2023

Nice. I achieved similar functionality with ddgr cli and a vim command.

The gist of it is something like this:

command! -nargs=* DDGR terminal ddgr <args>

Which I also set as keywordprg (with DuckDuckGo's bangs, it's 😗👌):

augroup ddgr
  autocmd!
  autocmd FileType python setlocal keywordprg=:DDGR\ !python
  autocmd FileType go setlocal keywordprg=:DDGR\ !godoc
augroup END

Published as a minimal vim plugin: https://github.com/pbnj/vim-ddgr

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