Surfing web inside a terminal, because why not?
I recently wrote a python script to surf the web (see search results) directly into the terminal.
Here is a demo
Wrote a python script to surf the web 🔎 from inside the terminal with 0 external dependencies (yeah no pip install ;)— Bhupesh Varshney 🐧🐍 (@bhupeshimself) February 19, 2021
You can find the script on my github 🔽https://t.co/EKHahIcUTz
Feedback/Suggestions welcome#python pic.twitter.com/U0cVg9lFSc
The script is available on my github
It’s not that big of a deal since all credit goes to searx a privacy respecting FOSS metasearch engine
- It respects your privacy. No user-tracking
- You can even set up your own (private/public) searx instance on a VPS. List of public instances
- Aggregates results from more than 70 search services (DuckDuckGo, Wikipedia, etc)
The script doesn’t require any dependencies (except Python 3), just download the script and run it.
Note that surf was written for/as a Linux user. It should work on Mac without any problems. If you are on Windows consider sending patches if the script breaks. Thanks!
wget -q https://raw.githubusercontent.com/Bhupesh-V/.Varshney/master/scripts/surf chmod +x surf && mv surf $HOME/.local/bin/
If you are more of a bad boy/girl then add the location of surf script to your
export PATH="DIR_PATH_WHICH_CONTAINS_SURF:$PATH" # e.g export PATH="$HOME/Documents/.Varshney/scripts:$PATH"
You can view help & usage on surf by doing a simple
surf --help. Let’s go through all the options available.
- Just run the script, without any arguments. surf will prompt you for a search query:
- Show link description.
$ surf -d
- Provide search query as an argument (also
$ surf -dq "how to change the world"
- You can also pipe data to surf
$ echo "search this" | surf
-cto specify search category (default: general):
$ surf -c "videos" -dq "how to make a pizza"
Other categories include: “news”, “files”, “images”, “map”
- Update searx instance cache list before fetching results:
$ surf -udq "when is doomsday"
Updating cache is not usually required, I recommend running it once/twice a week to get a list of active instances.
The cache is located at
$HOME/.local/.searx_instanceson linux and
- Show only top N results (also
$ surf -t 5 -dq "check if key exists in map"
searx supports those sneaky search tricks as well,
surf -dq "site:youtube.com how to make pizza" surf -dq "site:stackoverflowcom <my-error-message>" surf -dq "intitle:best vim plugins" surf -dq "inurl:docs.djangoproject.com templates"
Well now you would be asking “what’s the use of this bhupesh, If I have to open the link in the browser at the end”
But what I realized is that it may accidentally fit right into my/your workflow if you use Vim, let me show how
Browsing Inside Vim, say whaat
We need a way to open links right from our vim terminal. Add this to your
" Open hyper link in current line function! OpenLink() let links =  try call substitute(getline('.'), 'http[s]\?:\/\/[^) \"]*', '\=add(links, submatch(0))', 'g') echo "Opening " . links exe "silent! !xdg-open " . links catch E684 echo "No link found :(" return endtry endfunction
exe "silent! !xdg-open " . links
will directly open your default browser with the link.
If you are not using X11, then you can just swap it directly with the browser of your choice or your default file manager
exe "silent! !firefox --new-tab " . links
exe "silent! !chromium-browser " . links
Also, I recently found a less bulky approach on vimtricks.com
function! OpenURLUnderCursor() let s:uri = expand('<cWORD>') let s:uri = substitute(s:uri, '?', '\\?', '') let s:uri = shellescape(s:uri, 1) if s:uri != '' silent exec "!open '".s:uri."'" :redraw! endif endfunction nnoremap gx :call OpenURLUnderCursor()<CR>
Searching Error messages inside Vim
Say I am debugging something and end up with a never before seen Error message. I can quickly send the error string to
surf and get links to StackOverflow or GitHub!
Here is a demo of me using flake on surf & searching about rules:
How this works is pretty straightforward. We just need to get the current visual selection.
function! GetVisualSelection() " Thanks: https://stackoverflow.com/a/6271254/8209510 let [line_start, column_start] = getpos("'<")[1:2] let [line_end, column_end] = getpos("'>")[1:2] let lines = getline(line_start, line_end) let lines[-1] = lines[-1][: column_end - (&selection == 'inclusive' ? 1 : 2)] let lines = lines[column_start - 1:] return join(lines, "\n") endfunction xnoremap <leader>t <esc>:split <bar> call SendQueryToTerm()<CR> function! SendQueryToTerm() let selection = GetVisualSelection() exe ":term surf -dq \'" . selection . "\'" endfunction
This will open a split above with a new terminal buffer.