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 | $selected_text=$(xclip -o -selection primary) |
For more control over the browser, I can hardcode the executable:
1 | $selected_text=$(xclip -o -selection primary) |
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 |
|
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.