Skip to content

Instantly share code, notes, and snippets.

@matthutchinson
Last active December 17, 2015 14:49
Show Gist options
  • Save matthutchinson/5627367 to your computer and use it in GitHub Desktop.
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)
" 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>
@matthutchinson
Copy link
Author

👍 then you realise there's a plugin for this already

@swmcc
Copy link

swmcc commented May 22, 2013

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