24

In Chrome, you can highlight some text on a web page and use the right-click context menu to open a google search for the selected text in a new tab.

It would be super convenient if I could access this feature using a keyboard shortcut instead of the right-click menu. I've tried searching for existing extensions and also scoured the list of existing keyboard shortcuts here: https://support.google.com/chrome/answer/157179?hl=en

Does anybody know a way to accomplish this?

1
  • Questions about web browser functionality belong on Super User.
    – ale
    Commented Jan 25, 2015 at 19:27

12 Answers 12

13

This will work while in Chrome:

  • First highlight some text
  • Hit CTRL+C - This copies the text
  • Hit CTRL+T - This creates a new tab and makes it the focus
  • Hit CTRL+V - This pastes the text in the Omnibox (Chrome defaults the cursor there)
  • Hit Enter - This will search the text in the Omnibox

Want to automate it? Use AutoHotKey to make it an automatic macro using CTRL+Alt+S Use this script::

^!s::
  Send ^c
  Send ^t
  Send ^v
  Send {Enter}
Return

FYI, I tested this script and it works in Chrome.

2
  • I was just notified that someone upvoted this answer. I looked at it. It just goes to show that progress gets made. Almost 7.5 years ago, someone wanted this feature and I provided a solution. Now this feature has been in browsers, for... several years now? I dont even remember it not being there.
    – Keltari
    Commented Jun 11, 2022 at 16:08
  • 1
    Are you saying the feature is currently in Chrome? I see that "Search Google" shows up in the context menu, but how do we assign a hotkey to it? Commented Feb 1, 2023 at 22:53
7

I have two answers for this in AHK as well.

This is global applicable anywhere (not only in chrome). Just select text and press Windows+G

#g::  ;;Google selected text
   Send, ^c
   Run, http://www.google.com/search?q=%Clipboard%
Return

One is this from my answer here. Select Text and press Windows+Shift+G. This is different in that it just gives you a link on the clipboard.

; Search google for the highlighted word
; then get the first link address and put it on the Clipboard

^!r:: Reload

#+g::
    bak = %clipboard%
    Send, ^c
    ;clipboard = %bak%`r`n%clipboard%
    Query = %clipboard%
    wb := ComObjCreate("InternetExplorer.Application")
    ;wb := IEGet()
    wb.Visible := false
    wb.Navigate("www.google.com/search?q=" Query)
    While wb.readyState != 4 || wb.document.readyState != "complete" || wb.busy ; wait for the page to load
      sleep 100
    ; loop % (Nodes := wb.document.getElementById("rso").childNodes).length
    ;     Links_urls .= (A_index = 1) ? Nodes[A_index-1].getElementsByTagName("a")[0].href : "`n" . Nodes[A_index-1].getElementsByTagName("a")[0].href
    ; Msgbox %Links_urls%

    Nodes := wb.document.getElementById("rso").childNodes
    First_link := Nodes[0].getElementsByTagName("a")[0].href
    Clipboard = %First_link%
    TrayTip, First Link on Google Search, %First_link% `r`n Ctrl+V to paste the link
return
3
  • The first option gives me the LAST ctrl + c or win + g. I'm not certain why?
    – josh
    Commented Jan 12, 2017 at 10:56
  • You have to select the text first. That's the only reason I can think of getting the last clip or are you using a clipboard manager? or try adding this to your script after the Send, ^c command to see what's on your clipboard TrayTip, Clipboard Contents, %clipboard% rn Commented Jan 31, 2017 at 4:27
  • I also get previous content with the first answer unless I add a Sleep after the copy.
    – eddi
    Commented Nov 30, 2020 at 21:18
2

Based on what Parivar Saraff has suggested here, here is a 3 in 1 AutoHotKey Script:

;Hotkey Modifier Symbols (for how to customize the hotkeys) https://www.autohotkey.com/docs/Hotkeys.htm#Symbols

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

;                               Google-search selected text
;  Usage:ctrl+shift+G
^+g::  
{
   Send, ^c
   Sleep 150
   Run, http://www.google.com/search?q=%Clipboard% ;(изм.себе на google.com.ua)
Return

}

;                               Google-dictionary selected text
;  Usage:ctrl+shift+D
^+d::
{
   Send, ^c
   Sleep 150
   Run, https://www.google.com/search?q=define:%Clipboard% ;(изм.себе на google.com.ua)
Return

}

;                           Wikipedia-search selected text by using google "site:" operator
;  Usage:ctrl+shift+W
^+w:: 
{
   Send, ^c
   Sleep 150
   Run, https://www.google.com/search?q=site:wikipedia.org %Clipboard% ;(изм.себе на google.com.ua)
Return

}

also highlighted text conversion script(a combination of such script variations on the web):

;Hotkey Modifier Symbols (for how to customize the hotkeys) https://www.autohotkey.com/docs/Hotkeys.htm#Symbols


    ;Hotkey Modifier Symbols (for how to customize the hotkeys) https://www.autohotkey.com/docs/Hotkeys.htm#Symbols


#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.



cycleNumber := 1

#IfWinNotActive ahk_class XLMAIN

                                ;Highlighting any text, and then pressing that HotKey will cycle through the 4 most common text casings, converting the highlighted text right in-line.

                                    ;For example:

    ;If you highlight "This is a test sentence", and then hit that HotKey once, it'll make it all UPPERCASE ("THIS IS A TEST SENTENCE").
    ;Hit the HotKey again, it'll convert it to lowercase ("this is a test sentence").
    ;Hit it again and it'll convert it to Sentence case ("This is a test sentence"). (First letter is capitalized, rest is lower-case).
    ;Finally, hit it one more time and it'll convert it to Mixed case, or what I often call, "camel-case" ("This Is A Test Sentence"). (Each word is capitalized).

;  Usage:Ctrl+Shift+C
^+c:: 

If (cycleNumber==1)
{
ConvertUpper()
cycleNumber:= 2
}
Else If (cycleNumber==2)
{
ConvertLower()
cycleNumber:= 3
}
Else If (cycleNumber==3)
{
ConvertSentence()
cycleNumber:= 4
}
Else
{
ConvertMixed()
cycleNumber:= 1
}
Return

ConvertUpper()
{
    clipSave := Clipboard
    Clipboard = ; Empty the clipboard so that ClipWait has something to detect
    SendInput, ^c ; Copies selected text
    ClipWait
    StringReplace, Clipboard, Clipboard, `r`n, `n, All ; Fix for SendInput sending Windows linebreaks 
    StringUpper, Clipboard, Clipboard
    Len:= Strlen(Clipboard) ;Set number of characters ;Set number of characters
    SendInput, ^v ; Pastes new text
    Send +{left %Len%} ;Re-select text
    VarSetCapacity(clipSave, 0) ; Free memory
    Clipboard := clipSave ;Restore previous clipboard
}

ConvertLower()
{
    clipSave := Clipboard
    Clipboard = ; Empty the clipboard so that ClipWait has something to detect
    SendInput, ^c ; Copies selected text
    ClipWait
    StringReplace, Clipboard, Clipboard, `r`n, `n, All ; Fix for SendInput sending Windows linebreaks
    StringLower, Clipboard, Clipboard
    Len:= Strlen(Clipboard) ;Set number of characters
    SendInput, ^v ; Pastes new text
    Send +{left %Len%} ;Re-select text
    VarSetCapacity(clipSave, 0) ; Free memory
    Clipboard := clipSave ;Restore previous clipboard
}

ConvertSentence()
{
    clipSave := Clipboard
    Clipboard = ; Empty the clipboard so that ClipWait has something to detect
    SendInput, ^c ; Copies selected text
    ClipWait
    StringReplace, Clipboard, Clipboard, `r`n, `n, All ; Fix for SendInput sending Windows linebreaks
    StringLower, Clipboard, Clipboard
    Clipboard := RegExReplace(Clipboard, "(((^|([.!?]+\s+))[a-z])| i | i')", "$u1")
    Len:= Strlen(Clipboard) ;Set number of characters
    SendInput, ^v ; Pastes new text
    Send +{left %Len%} ;Re-select text
    VarSetCapacity(clipSave, 0) ; Free memory
    Clipboard := clipSave ;Restore previous clipboard
}

ConvertMixed()
{
    clipSave := Clipboard
    Clipboard = ; Empty the clipboard so that ClipWait has something to detect
    SendInput, ^c ; Copies selected text
    ClipWait
    StringReplace, Clipboard, Clipboard, `r`n, `n, All ; Fix for SendInput sending Windows linebreaks
    StringUpper Clipboard, Clipboard, T
    Len:= Strlen(Clipboard) ;Set number of characters
    SendInput, ^v ; Pastes new text
    Send +{left %Len%} ;Re-select text
    VarSetCapacity(clipSave, 0) ; Free memory
    Clipboard := clipSave ;Restore previous clipboard
}

#IfWinNotActive

                        ; Convert selected text to inverted case
                                    ;    Ex: THIS_is-a_tESt -> this_IS-A_TesT
; Usage:ctrl+Shift+I 
^+i::
    Convert_Inv()
RETURN
Convert_Inv()
{
    ; save original contents of clipboard
    Clip_Save:= ClipboardAll

    ; empty clipboard
    Clipboard:= ""

    ; copy highlighted text to clipboard
    Send ^c{delete}

    ; clear variable that will hold output string
    Inv_Char_Out:= ""

    ; loop for each character in the clipboard
    Loop % Strlen(Clipboard)
    {
        ; isolate the character
        Inv_Char:= Substr(Clipboard, A_Index, 1)

        ; if upper case
        if Inv_Char is upper
        {
            ; convert to lower case
           Inv_Char_Out:= Inv_Char_Out Chr(Asc(Inv_Char) + 32)
        }
        ; if lower case
        else if Inv_Char is lower
        {
            ; convert to upper case
           Inv_Char_Out:= Inv_Char_Out Chr(Asc(Inv_Char) - 32)
        }
        else
        {
            ; copy character to output var unchanged
           Inv_Char_Out:= Inv_Char_Out Inv_Char
        }
    }
    ; send desired text
    Send %Inv_Char_Out%
    Len:= Strlen(Inv_Char_Out)

    ; highlight desired text
    Send +{left %Len%}

    ; restore original clipboard
    Clipboard:= Clip_Save
}
                            ; Text–only paste from ClipBoard (while the clipboard formatted text itself is being untouched)
; Usage:ctrl+Shift+I 
^+v::                          
   Clip0 = %ClipBoardAll%
   Clipboard = %Clipboard%  ; Convert clipboard text to plain text.
   StringReplace, clipboard, clipboard,%A_SPACE%",", All ; Remove space introduced by WORD
   StringReplace, Clipboard, Clipboard, `r`n, `n, All ; Fix for Send sending Windows linebreaks
   Send ^v                       ; For best compatibility: SendPlay
   Sleep 50                      ; Don't change clipboard while it is pasted! (Sleep > 0)
   ClipBoard = %Clip0%           ; Restore original ClipBoard
   VarSetCapacity(Clip0, 0)      ; Free memory
Return

                                    ; Wrap selected text in double quotes->" "
; Usage:Ctrl+Shift+Q
^+q::
    clipSave := Clipboard
    Clipboard = ; Empty the clipboard so that ClipWait has something to detect
    SendInput, ^c ; Copies selected text
    ClipWait
    StringReplace, Clipboard, Clipboard, `r`n, `n, All ; Fix for SendInput sending Windows linebreaks
    Clipboard := Chr(34) . Clipboard . Chr(34)
    Len:= Strlen(Clipboard) ;Set number of characters
    SendInput, ^v ; Pastes new text
    Send +{left %Len%} ;Re-select text
    VarSetCapacity(clipSave, 0) ; Free memory
    Clipboard := clipSave ;Restore previous clipboard
Return

; RELOAD 
!+^x::
   SplashTextOn,,,Updated script,
   Sleep,200
   SplashTextOff
   Reload
   Send, ^s
Return
2

Apparently, pressing S after activating the context menu on a highlighted text will do just that (Chrome 78 here). The context menu can be opened with Shift+F10 or with the dedicated "context menu" button on your keyword.

These two shortcuts can be combined into one using AutoHotKey:

^g::
  Send +{F10}
  Send s
Return

This, for example, will make Ctrl+G search for the highlighted text in a new tab.

The major advantage of this method over @Keltari answer is that it doesn't use the clipboard and thus doesn't overwrite previous values there.

2

Use this extension

https://chrome.google.com/webstore/detail/hotkeys-for-search/gfmeadbjkfhkeklgaomifcaihbhpeido

how to use it:

  1. Select text on a web page using the mouse.
  2. Press a keyboard shortcut to search for the selected text on your desired website.

Default shortcuts:

Alt+Q = Google

Alt+W = Wikipedia

Alt+A = Google Images

Alt+S = YouTube

and if you want to automate many tasks use this custom hotkey extension for chrome

https://chrome.google.com/webstore/detail/keyboard-fu/cafiohcgicchdfciefpbjjgigbmajndb

4
  • 1
    Simply recommending software doesn't make an answer. Please add steps necessary to set up the software to answer the question. Commented Jan 25, 2020 at 4:34
  • Please read How do I recommend software for some tips as to how you should go about recommending software. You should provide at least a link, some additional information about the software itself, and how it can be used to solve the problem in the question.
    – DavidPostill
    Commented Jan 25, 2020 at 7:59
  • 1
    OK i edited that answer although it's very clear anyone can click the link will see immediately how to use it info happy now Commented Jan 26, 2020 at 17:14
  • @Rkv88-Kanyan thank you, I'll be using this extension.
    – nightcoder
    Commented 4 hours ago
1

Using AutoHotKey as above

^!s::
  Send ^c
  Sleep 100
  Send ^t
  Send ^v
  Send {Enter}
Return

NOTICE: I added 'Sleep 100' to avoid previous clipboard search

1

Slight modification to the AutoHotKey scripts above that worked best for me:

^!s::
  tmp := clipboard
  Send, ^c
  Sleep 100
  Run, http://www.google.com/search?q=%clipboard%
  clipboard := tmp
Return

This doesn't nuke the previous clipboard value, and the added Sleep ensures that the clipboard has indeed been updated with the new value before you search.

1

Here some bash solutions for Linux.

The same as in the rest of the answers here. xdotool doing shortcuts thing and xclip handle clipboard:

#!/bin/bash

set -e
set -o pipefail

old_clipboard_content=$(xclip -selection clipboard -o)
xclip -selection primary -o | xclip -selection clipboard # Paste primary selection to clibpoard
xdotool key Control_L+t
xdotool key Control_L+v
xdotool key Return
echo $old_clipboard_content | xclip -selection clipboard

Or you can just simply open new tab from cli (work slightly faster btw):

#!/bin/bash

set -e

google-chrome/chromium/brave/or_whatever_browser_you_use --app-url "https://google.com/search?q=$(xclip -selection primary -o)"
0


This extension can help you:
https://chrome.google.com/webstore/detail/searchbar/fjefgkhmchopegjeicnblodnidbammed
After instaling mark those options:
*Open search results in a new tab by default (does not affect hotkeys; press Ctrl or middle-click to toggle new tab)
*Open new tabs in the foreground by default (press Shift to toggle between foreground and background)
Now you are able to run search for selected text with Ctrl+Shift+Alt+G shortcut

0
0

This repo provides an Autohotkey script that can be used to search google for your selected text from any window. The advantage is that it does not change your clipboard.

For the sake of completeness I quote the code:

; win+shift+g googles currently selected text
#+g::GoogleSelection()

; win+shift+w looks up selected word in Google Dictionary
#+w::DefineSelection()

GoogleSelection() {
    searchQuery := Trim(GetSelectedText())
    if !searchQuery {
        Run, www.google.com
        return
    }
    address := Format("www.google.com/search?q={}", searchQuery)
    Run, %address%
}

DefineSelection() {
    word := Trim(GetSelectedText())
    if !word {
        Run, www.google.com/search?q=google+dictionary
        return
    }
    address := Format("www.google.com/search?q=define+{}", word)
    Run, %address%
}

GetSelectedText() {
    oldClipboard := ClipboardAll
    Clipboard := ""
    Send, {CtrlDown}c{CtrlUp}
    ClipWait, 0, false

    if ErrorLevel {
        return Fail(oldClipboard)
    }

    selection := Clipboard

    Clipboard := ""
    Send, {ShiftDown}{Right}{ShiftUp}{CtrlDown}c{CtrlUp}{ShiftDown}{Left}{ShiftUp}
    ClipWait, 0, false

    if ErrorLevel {
        return Fail(oldClipboard)
    }

    if !InStr(Clipboard, selection) {
        Clipboard := ""
        Send, {ShiftDown}{Left}{ShiftUp}{CtrlDown}c{CtrlUp}{ShiftDown}{Right}{ShiftUp}
        ClipWait, 0, false

        if ErrorLevel or !InStr(Clipboard, selection) {
            return Fail(oldClipboard)
        }
    }

    Clipboard := oldClipboard
    return selection
}

Fail(oldClipboard) {
    Clipboard := oldClipboard
    return ""
}
2
  • 1
    Welcome to SuperUser! Please do not only link to code on GitHub, especially if it is as short as this one, as it may be taken down so that your answer does no longer provide useful information. Instead quote it as code in your answer, and give the link to GitHub as a source. Commented Jan 7, 2022 at 9:06
  • @DarkDiamond Thanks! I edited my answer to include the code.
    – Armin Gh
    Commented Jan 7, 2022 at 19:31
0

I'm offering a solution for Mac. As mentioned by others, using keystroke "Cmd-C, Cmd-T, Cmd-V, Enter" in AppleScript/Automator is working with some tricks, like placing Delay 0.1 between the commands, otherwise the old clipboard seems to be living into the next call. However, if my keyboard layout was in another language at the time of execution, it works wrong (plus the Mac OS is not happy that Automator sends keystrokes, you need to give it permission). So, I had to look for other ways. Page https://www.sixhat.net/applescript-search-clipboard-text-on-sixhat-net.html gave me an inspiration. And that one is based on a script from the Computerworld.

So, I created a workflow in Automator as a 'quick action', with parameters "workflow receives no input in any application" and a single call to Run AppleScript. The script content is below:

on run {input, parameters}
    
    tell application "Google Chrome"
        activate
        delay 0.1
        tell active tab of front window to copy selection
    end tell
    
    set t to the clipboard as text
    set myquery to SaR(t, " ", "+")
    
    tell application "Google Chrome"
        activate
        set theURL to "https://www.google.com/search?q=" & myquery
        open location theURL
    end tell
    
    return input
    
end run

on SaR(sourceText, findText, replaceText)
    set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, findText}
    set tempText to text items of sourceText
    set AppleScript's text item delimiters to replaceText
    set sourceText to tempText as string
    set AppleScript's text item delimiters to atid
    return sourceText
end SaR

Then I assigned a keyboard shortcut to this workflow according to https://apple.stackexchange.com/questions/175215/how-do-i-assign-a-keyboard-shortcut-to-an-applescript-i-wrote, and it works.

0

If you have Tampermonkey or a similar userscript manager installed in your browser, you can add the following script, which allows you to search selected text by shortcut alt+g:

// ==UserScript==
// @name         Google Search Selected Text
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Google search the selected text in a new tab when pressing Alt+G
// @author       Good day Sir!
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to handle the keydown event
    function handleKeydown(event) {
        // Check if Alt+G is pressed
        if (event.altKey && event.key === 'g') {
            // Get the selected text
            var selectedText = window.getSelection().toString().trim();
            if (selectedText) {
                // Create the Google search URL
                var searchUrl = 'https://www.google.com/search?q=' + encodeURIComponent(selectedText);
                // Open the search URL in a new tab
                window.open(searchUrl, '_blank');
            }
        }
    }

    // Add event listener to handle keydown events
    document.addEventListener('keydown', handleKeydown);
})();

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .