Ask Google Through Your Terminal

During my first week at Launch Acadmey I built a simple ruby script so that I don’t have to leave my keyboard to ask a question through google. I call it NoMouse.

The idea came when Johnny, one of my mentors at LaunchAcademy, taught me that everything we do in our GUI can be done through the terminal. This was a very interesting and cool concept as a beginner. I wanted to test this theory out. I wanted to see if I can open up my browser from the terminal and open a specific page.

Here is how I tackled the theory. I needed to first figure out how to open a browser or website through my terminal.

open “

This was possible. Now I need to know if I can run a script and open a browser. In my terminal I ran ruby browser.rb and it would run the following script.

system(“open”, “”)

This opened up possibilities for me. I was becoming greedy! Not only could I open a browser I wanted to see if I can feed a question to the script for it to search.

I needed to figure out what goes after “.com/” for google to search. I went to google and typed in a search. How to play basketball.

The important part of the link is everything following the “.com/” Google uses “#q=” and a “+” symbol for every space. Simply add the “#q=” to the system and then create a variable to substitute spaces for “+” symbols.

text = gets.chomp text.gsub!(“ ”,“+”) system(“open”, “”)

Ideally, this would turn all the white spaces to a ‘+“ and we start with the ’/#q=.‘ It is exact to the google search. This did not work. The # kept changing into ’%23’ when opens the google browser. When I type “How do I play basketball?” => It also leads to an error page. The browser is encoding # => %23. I had such a hard time figuring out how to either solve or get around this problme. I figured that there was no way of me to control google from changing # into %23. I needed another solution.

Breaking what I have done is found another way google searches. The escape method defined in the ruby doc, “Escapes the string, replacing all unsafe characters with code.” Basically when i input a question with all the ‘unsafe’ symbols, and spaces with safe code Here is my final code.{URI.escape(@question)}

Lastly, if you have any recommendations for me to improve this script or a vision of where you think this could go. Please feel free to share with me at my twitter handle. – @johnmoon6

Visit the repo

The idea is that I run the nomouse.rb script and it will prompt a message, ‘What is your question?’ then the user will input a question they have. This triggers your default browser to open and google search the question.