I would like to be able to search google from within any Vim file. A nice command might be :goo while in normal mode.

Then I type what I want to search and bam it opens my default browser with the search.

How would I do this?

  • 1
    I think you can have a look at github.com/szw/vim-g
    – nobe4
    Jul 28, 2016 at 14:47
  • 2
    I dunno, type then open browser, or open browser then type. Seems a bit pointless me. Plus if you type in the browser you get search completion. What's the use case?
    – Antony
    Jul 28, 2016 at 16:00
  • The use case is not having to switch focus to browser, also being able to paste confusing code from any of the registers into the search. Sometimes system register is a little choppy and it would be and extra step to copy to that register. Plus come on -->"Googling from vim"<-- Jul 28, 2016 at 18:16
  • 1
    If you want to google from your text editor you should have a look at emacs ;-) More seriously, I understand that it is fun to see but IMO that's not an efficient way to use Vim.
    – statox
    Jul 29, 2016 at 8:05
  • You will switch focus, at least mentally, so using the "right" tool for what you want is a plus. I feel like "Googling from vim" is like "gdb from vim", it can be done, but it's not the purpose of each tool (aka vim is only a text editor, nothing more).
    – nobe4
    Jul 29, 2016 at 8:06

5 Answers 5


You have a couple of options here:

Using a plugin:

Or, if you prefer a lightweight solution, you can try the following:

function! GoogleSearch()
     let searchterm = getreg("g")
     silent! exec "silent! !firefox \"http://google.com/search?q=" . searchterm . "\" &"
vnoremap <F6> "gy<Esc>:call GoogleSearch()<CR>


Using the vim-shell plugin you can rewrite this to:

function! GoogleSearch()
     let searchterm = getreg("g")
     Open "http://google.com/search?q=" . searchterm . "\" &"
vnoremap <F6> "gy<Esc>:call GoogleSearch()<CR>

You can also have a look at those links:

And I highly recommend this video by Drew Niel.

  • The vim-shell plugin could be used to make your function to be OS/browser agnostic.
    – mMontu
    Jul 28, 2016 at 17:00
  • "silent! !firefox \"http://google.com/search?q=" . searchterm . "\" &" should be "silent! !firefox \"http://google.com/search\\?q\\=" . searchterm . "\" &"
    – WW00WW
    Aug 20, 2018 at 3:15

As others have pointed out, Searching from Vim is not something one would want to do every time, but I do understand that there are some situations where you just want to search for a particular word in Vim. In those situations this plugin might be useful.

Mind you, this is something I wrote only for the purpose of searching for the word under the cursor, nothing more.


The Readme file has info about how to use this. And even if you don't want to use the plugin itself, look at the plugin file. Its really small and you will get an idea about how you can do system calls.

  • 1
    It's nice to see you are starting to write Vim plugins. I suggest you have a look at :h :execute, which let you insert variable name in your commands. This could simplify largely your scour#Search function.
    – nobe4
    Jul 29, 2016 at 12:07
  • 1
    I agree with @nobe4: your function could be one line actually: execute "silent !" . g:scour_browser . " www." . g:scour_search_engine . ".com/search?q=<cword>". execute will execute the string provided as argument, in the string you simply concatenate the browser and search engine variables with . and you're good to go :-)
    – statox
    Jul 29, 2016 at 12:13
  • 1
    @statox Thanks for the suggestion. It was a while ago I wrote that. I was just learning Vim scripting then. Though I am still a beginner in scripting I haven't bothered to update the plugin. Jul 29, 2016 at 14:24

you can do it like this

x-www-browser https://google.com

then you can do a search or if you want to open a link it would be like this

x-www-broswer https://www.youtube.com/watch?v=dQw4w9WgXcQ
  • 2
    Welcome to Vi and Vim! I'm not sure you addressed how to do this specifically in vim, though it looks as simple as doing :! x-ww-browser ...
    – D. Ben Knoble
    Oct 24, 2020 at 17:30
nmap gy :silent execute "!google-chrome http://google.com/search?q=" . shellescape("<cWORD>") . " &"<CR>
vmap gy <Esc>:silent execute "!google-chrome http://google.com/search?q=" . shellescape("<C-r>*") . " &"<CR>

The first command allows you press gy to search for a word under your cursor. The second command allows you to visually search for a term, also using gy but in visual mode. Replace google-chrome with your browser command or use vim-shell as in the other answer.


Providing a snippet to resolve url encoding.

" ref: https://vi.stackexchange.com/a/1958/49235
" Call shell command silently then Ctrl+L (or :redraw!) to refresh the screen when back to Vim.
command! -nargs=1 Silent execute ':silent !'.<q-args> | execute ':redraw!'

" google search
nnoremap <silent> gG  :Silent open "http://google.com/search?q="<c-r>=<sid>encode_words(expand("<cword>"))<cr><cr>
vnoremap <silent> gG y:Silent open "http://google.com/search?q="<c-r>=<sid>encode_words(@")<cr><cr>

" https://vi.stackexchange.com/questions/42717/how-do-i-turn-a-string-into-url-encoded-string
" URL encode a string. ie. Percent-encode characters as necessary.
function! s:encode_words(text) abort
    " Replace `\n`, preserve `\`
    let words = substitute(a:text, '\n', ' ', 'g')
    let words = substitute(words, '\\', '\\\', 'g')

    let result = ""
    let characters = split(words, '.\zs')
    for character in characters
        if character == " "
            let result = result . "+"
        elseif s:needs_encode(character)
            let i = 0
            while i < strlen(character)
                let byte = strpart(character, i, 1)
                let decimal = char2nr(byte)
                " Avoiding command expansion
                let result = result . "\\%" . printf("%02x", decimal)
                let i += 1
            let result = result . character

    return result

" Returns 1 if the given character should be percent-encoded in a URL encoded
" string.
function! s:needs_encode(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

    return 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.