Last active
December 17, 2015 14:49
-
-
Save matthutchinson/5627367 to your computer and use it in GitHub Desktop.
Quickly google for something in vim (uses visual text selection for the query)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" get visually selected words/lines | |
function! GetVisualSelection() | |
let [lnum1, col1] = getpos("'<")[1:2] | |
let [lnum2, col2] = getpos("'>")[1:2] | |
let lines = getline(lnum1, lnum2) | |
let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)] | |
let lines[0] = lines[0][col1 - 1:] | |
return join(lines, ' ') | |
endfunction | |
" search highlighted text (single line or word) with Google | |
function! GoogleSearch() | |
let selection = GetVisualSelection() | |
let query = substitute(selection, '[[:space:]]', '+', 'g') | |
silent! exe '!open "http://google.com/search?q=' . query . '" > /dev/null 2>&1' | redraw! | |
endfunction | |
vmap ?? :call GoogleSearch()<cr> |
Still learnt a lot of stuff though :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍 then you realise there's a plugin for this already