Search selected text from anywhere in Linux

I spend most of my time in front of a computer and often find myself juggling multiple tasks and resources.
Whether I’m reading articles, coding, or diving into research, there’s always that moment when I need to look something up online.
The typical process is a hassle: switching between applications, opening the browser, and start typing or copy-pasting some text to the search engine.
I wanted to simplify things, creating simple and effective way to quickly Google the selected text.
In this post, I’ll share my solution with you.

The Thought Process

In Linux, there’s a nice nifty feature : text selection using the mouse is automatically sent to the clipboard and can be pasted with the middle-click.
I was leveraging that feature for my workflow.
To get started, I needed to fetch the selected text from the clipboard. This can be done with the following command:

1
xclip -o -selection primary

I could then format the output into a search query URL and open it in the browser. Here’s the command to do just that:

1
2
$selected_text=$(xclip -o -selection primary)
xdg-open "https://www.google.com/search?q=$selected_text"

For more control over the browser, I can hardcode the executable:

1
2
$selected_text=$(xclip -o -selection primary)
brave-browser --incognito "https://www.google.com/search?q=$selected_text"

That’s it!
All I needed to do was to compile that into one script, bind the script to a shortcut and voilà.
A few lines in my AwesomeWM config and it’s ready to use.

Expanding Functionality

I’ve added more option to my current script like choosing between different search engines or using AI-powered search engines like Phind or Perplexity.
DMenu, my favorite launcher, is already a perfect fit for this task.

Here’s the script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/bash
# Description: Search or browse selected text (sst)

# Function to launch browser with URL
launch_browser() {
/usr/bin/brave-browser --incognito "$1"
}

# Get the selected text from the X selection buffer
selected_text=$(xclip -o -selection primary)

# Define search engines and their URLs
declare -A search_engines=(
["Google"]="https://www.google.com/search?q="
["DuckDuckGo"]="https://duckduckgo.com/?q="
["Brave"]="https://search.brave.com/search?q="
["Perplexity"]="https://www.perplexity.ai/?q="
["Phind"]="https://www.phind.com/search?q="
)

# Create the list of options for dmenu with newlines
options=$(printf "%s\n" "${!search_engines[@]}" "Open URL")

# Use dmenu to choose a search engine
search_engine=$(echo -e "$options" | dmenu -p "Apply on selection:")

# Perform the search based on the chosen engine
if [[ "$search_engine" == "Open URL" ]]; then
launch_browser "$selected_text"
elif [[ -n "${search_engines[$search_engine]}" ]]; then
launch_browser "${search_engines[$search_engine]}$selected_text"
fi

How am I using it?

Simple: select text anywhere, hit Super+s for search, choose a search engine and the browser handle the rest.
It’s way faster than firing a browser, go to the right url and start typing or pasting.
This is what I use to feed error logs to Perplexity.
It’s just so convenient!

This is now part of my current workflow.