Hacker News new | past | comments | ask | show | jobs | submit login
Howdoi – Instant Coding Answers via the Command Line (github.com/gleitz)
245 points by gleitz on Jan 8, 2013 | hide | past | favorite | 85 comments



According to the source code (https://github.com/gleitz/howdoi/blob/master/howdoi/howdoi.p...), the answers come from Stack Overflow, searched using Google site search. Howdoi scrapes and prints the first code block in the top answer, or the full text of the top answer if it contains no code block.


Precisely! You can pass the "-a" flag to print the entire answer or "-l" to get the link itself. "-p2" will give the second answer.

I find myself using the following command quite a bit:

open `howdoi print stack trace python -l`


-p2 gives the second search result's answer, not the second answer from the first result. Or so it seems from other replies.


Although not user facing, there is also a DuckDuckGo search endpoint if you don't want to use Google for one reason or another.


That is exactly what I would have guessed. It's basically "I'm feeling lucky" for coding questions.


It should probably use symbolhound.com instead, which does exact symbol queries on stackoverflow content. Google, alas, thinks we are all terrible people for wanting our precious bytes.


That's a nice search tool. I found that the relevance is not quite as good as Google (for example "howdoi get referrer url php") but this could be used when you enter a query that contains symbols.


Super useful, love it.

Some queries I tried (results were not perfect but pretty decent):

    howdoi split string javascript
        var split = 'john smith~123 Street~Apt 4~New York~NY~12345'.split('~');

        var name = split[0];
        var street = split[1];

        etc...

    howdoi get day from date javascript
        var date = new Date();
        var day = date.getDay();

    howdoi iterate coffeescript
        ages = []
        ages["jim"] = 12
        ages["john"] = 7

        for k,v of ages
          console.log k + " is " + v
  
    howdoi get referrer url php
        <?php

        session_start();

        if (!isset($_SESSION["origURL"]))
          $_SESSION["origURL"] = $_SERVER["HTTP_REFERER"];

        ?>


An enhancement I've been considering is noticing when users ask for a second or third answer with "-p2" or "-p3". If it happens often and the user never queries again after reading the second answer you can assume it is probably correct and display it to all users as the default response.


Also.. don't you think the more common case is not that the top answer is incorrect but rather you picked the wrong question from the search results? Seems to me that would be more common.


Ah yes I should clarify. -p2 picks the second search result from Google, not the second answer to the question.

$ howdoi format dates django templates -l

http://stackoverflow.com/questions/7737146/django-template-d...

$ howdoi format dates django templates -p2 -l

http://stackoverflow.com/questions/1180372/how-to-format-dat...

$ howdoi format dates django templates -p3 -l

http://stackoverflow.com/questions/4087717/django-template-d...

Another improvement might be to list the second and third answers to the currently selected question.


maybe howdoi --reroll? (Repeat same question for a second opinion)


That sounds smart but it also means that you'd need to keep track of and store user actions centrally. I don't know if I'd like that. You could always make it an opt-in feature I suppose.


That's not a bad idea, kind of meta level answer tracking.

What about making it even easier by just prompting as to whether it answered their question? (perhaps defaulting to 'y' so you can just hit enter if so)


not necessarily. usually if i am on stack overflow i am looking for an easy-type answer. usually if that is not one of the first two, i move on.


$ howdoi program

Why do you need the Apple Developer Program Enrollment ID? I also tried to apply to the iOS Developer Program this weekend. I applied, I paid and I got my activation codes. But then when I tried to activate with my Apple ID I got an error saying (in my case) they "were not able to successfully verify my identity". In that error message there was an Enrollment ID# consisting of both letters and numbers, a total of 10 characters, that I was supposed to include when contacting the support regarding this issue. I have not seen the same Enrollment ID somewhere else in the process and therefore I assume it is some kind of internal ID for my signing up process and you should only see it/use it when you hit some problems during the process.

I'll figure it out one of these days.


reset your password using iForgot


I really like the idea of this, I'm getting some good results and some not so good.

One that fails rather badly:

format dates django templates Returns simply "date", despite StackOverflow having lots of hits: http://stackoverflow.com/search?q=format+dates+django+templa...

But some others are super cool:

create datetime from epoch >>> datetime.datetime.fromtimestamp(1284286794)

One suggestion is that by default I would include the link to the stackoverflow page.

Some color might be nice to make it extra sexy, look at the colorama package to do it easily cross platform.


Thanks for playing with it. Sometimes the first answer isn't the greatest but -p2 or -p3 will get it. In this case the following command gets you closer:

howdoi format dates django templates -p3

Excellent idea for colorization! I've already got that suggestion on the github page -> https://github.com/gleitz/howdoi/issues/4


It even has a good sense of humor:

    $ howdoi kill someone
    ps aux
...yeah, first I must find out if "he"'s alive and how can I find "him", so it's a good first step :)

    $ howdoi clean up blood stains
    No, you cannot achieve that with PHP [...]
...indeed, PHP has never been my fav language :)


  howdoi kill myself
  Thread.Abort


someone should submit a patch that will display the phone number of a local suicide hotline if anyone asks this ...seriously!


Excellent, I can give it to a friend who seems to use me (via Skype) as a proxy for StackOverflow.

He hasn't got the hint with lmgtfy links so I'll try a different tack...


Very cool! You might want to change

USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17"

to something a little more honest though, unless you have a good reason for keeping it.


He scrapes from Google right? Google doesn't like automated queries so it makes sense.


It took a minute to realize that it requires an internet connection. Maybe you should spell that out?

I thought at first you had compiled a corpus of knowledge, but this is much better, but with the requirement of internet access.


Bad news if you use it at work. Management will see all the search queries using the Internet and think you are doing a lot of surfing.

This is basically like those old Technet and MSDN programs for Windows, they searched the online Microsoft database for new content. I found that out the hard way when I was asking why I was using so much Internet and then I found out it was my MSDN lookups when encountering bugs in the Windows API calls etc and even if business related, management was counting them. Management didn't know the difference between MSDN and MSN even if they paid a lot for my MSDN subscription. Management was constantly searching MSN for stock quotes and news items BTW because it was the default web page in Internet Explorer.


I wouldn't want to work for a company that restricts access to StackOverflow, or MSDN for that matter.


Neither would I, but this was back during the Dotcom crashes when jobs were hard to find because the market was flooded with people looking for work.

You also don't know it until after you are hired, and you find out they install remote access trojans on your Windows workstation, and count the number of characters you type, and monitor your Internet use and don't know the difference between a work related site, a non-work related site, or just accessing ASP rendered pages on the Intranet I was developing for them. Basically it was a Big Brother type of organization, a law firm in downtown Saint Louis MO.


That doesn't sound like a good work environment.


It was very stressful, the IT department at the law firm I worked at had a 90% turnaround for over four years that I worked there. They treated us like dirt/crap. For example I had to call Seagate Software for Crystal Report issues and their tech support number was in Canada. Everything a law firm does is tagged to a client and matter number for accounting purposes. I used the office numbers because it was business related. Since it was in Canada there was no 1-800 or toll free number, so they took the cost of the calls (what they usually bill the clients for LD calls) out of my paycheck because it was Canada. I asked to be reimbursed because it is business related, and they refused, telling me not to call Canada anymore as they only want to deal with US based companies. Keep in mind that Crystal Reports was bundled with Visual Studio 5.0/6.0 back then and they paid for Crystal Reports 7.0 to give everyone that nifty Web based server that gave them RTF, Word, Excel, and PDF files on the fly. It had a lot of bugs, and you had to talk to Seagate (I think they changed names or got bought out since there, Business Objects, SaS, whatever) which was located in Canada. I got so upset that I bought a LD calling card to make those calls.

Moving from Windows 95 and Office 97 to Windows 2000 and Office 2000 was a nightmare, because there were bugs and I was told Microsoft was aware of it and will fix it in the next service pack. We had gone to the latest and greatest of Microsoft's software instead of waiting for service packs to be released to address bugs so it is more stable. My Fax program used a Word Object to copy RightFAX codes into a document and print with a RightFax printer driver to fax out documents. Problem was Word 2000 had a bug that messed up the toolbar after a document was closed using the Word object in Visual BASIC. Microsoft admitted the bug, but didn't release a service pack to fix it yet. Management didn't understand that and had me search MSDN for a solution or maybe a beta service pack to address the issue. Then I am chewed out for wasting five hours searching the MSDN knowledgebase for a solution. I forget how we fixed it, but the work-a-round from Microsoft was to minimum Word 2000 and then restore it and the toolbar bug went away after that. After the service packs were released the bug went away.

While working for that company I suffered a nervous breakdown from the abuse and stress, and ended up on short-term disability, when I returned I was fired two weeks later for having a panic attack at work. They had moved me to an open area, no more cubical, near a bookshelf, and people mocked me and laughed at me as they walked by.

I had the right to sue, but because I am mentally ill, no lawyer would take the case. I am considering writing an ebook on the computer industry, as well as another ebook on what lead up to me becoming mentally ill and no longer able to work as a result.

Yes it was not a good work environment, it ruined my life and career.


Does SO provide data dumps like Wikipedia?


It looks like they are trying to make the dumps available on S3: http://meta.stackoverflow.com/questions/158050/regular-stack...

They also have odata endpoints so you should be able to get at the data (updated weekly) that way. It looks like there is a problem with it at the moment, though (I get 'server error'): http://data.stackexchange.com/stackoverflow/atom


Yes, but the last I could find is from 2011 http://blog.stackexchange.com/category/cc-wiki-dump/



They did monthly or bi-monthly data dumps. Not sure whether they still do. A few gibibytes of zipped XML would be a bit hard to ship, though :P


You could allow people to download a tag for offline use. Would be more data to store though.


Be nice if you'd make it explicit that it depends on Python 2 (change your hashbang to point to python2, or make it work in either runtime). Arch Linux defaults to Python 3 these days.


Isn't that an Arch bug? Last I heard, there was consensus in the upstream Python community that /usr/bin/python should not be Python 3, which makes sense because Python 3 is deliberately incompatible with Python 2. Of course, that viewpoint might have changed.

But still, even if upstream disagrees, I think it is the responsibility of the packagers not to break ABI compatibility in this way (just as they wouldn't ship OpenSSL 1.0 as libopenssl0.9.8.so). I'm pretty sure every script that uses Python 3 uses python3 in the shebang.


You're talking about PEP 394[1]. It suggests that "python" point to either "python2" or "python3", depending on what ships with the distro by default or on the distro's stated goals. Arch tries to be as cutting edge as possible, even putting out new kernels within weeks after their release. It makes sense for Arch to begin pushing the boundaries here.

There was a discussion on the arch-dev-public mailing list[2] recently that you might be interested in.

In any case, I think it's clearly beneficial for every new Python application to make their version requirements explicit in the hashbang.

[1] http://www.python.org/dev/peps/pep-0394/

[2] https://mailman.archlinux.org/pipermail/arch-dev-public/2012...


Cool, thanks for the pointers! I'm a little disappointed that Debian doesn't seem to be shipping the python2 symlink, which means that I can't switch to using that in my own code.


I'm not familiar with the proper way to annotate that. How would I change the hashbang?

#!/usr/bin/env python


I'm not an expert, but I believe just s/python/python2/. Also put it in the documentation as a requirement.

Edit: Hee hee

[aeikum@aeikum howdoi]$ howdoi use python2 on default python3

/usr/bin/env python2.7


My worry is that not everyone will have python2.7 so I'd rather stick to "#!/usr/bin/env python". It would be great if this works but it doesn't

#!/usr/bin/env python2


This is pretty cool, but it doesn't really feel a need for me.

Google for _split string javascript_ or the other queries, and you'll get basically the same results, just not limited to StackOverflow as howdoi does.

Maybe if I were more married to my command line this would be useful, but in general, I'm only there for bash and git. I spend most of my time in an editor, so popping over to the browser is no more work for me than showing the terminal.


> so popping over to the browser is no more work for me than showing the terminal.

But parsing a web page to find an precise answer usually takes a while.


I think the appeal of this for me would be that it feels less flow breaking to use my command-line (all work things) than using the browser (mix of work things and distractions.)


A similar program is `cheat`, from http://cheat.errtheblog.com/. Like howdoi, it prints short help on various topics to the commandline. But rather than searching Stack Overflow, it accesses a much smaller online wiki of cheat sheets. I’d give some examples of cheat sheets it has, but the online wiki seems to be down right now.


Huh, I wrote a little tool to keep track of my own command line snippets ages ago, also called `cheat`. I guess that's the natural name for it.


This is awesome! I'm going to create some issues (and hopefully pull requests!) later with some feature ideas.

I'm thinking seeing the question, and listing all questions so I know which -pX I want ;)


Excellent. Fork away!


That would be super useful!


I can`t use tools like this nor other local knowledge databases like evernote (although I save programming notes in evernote). If I have a pending question I ask myself two questions.

"Have I ever had this problem ?" "Did I save the answer ? (In evernote or whatever)"

To think about those two answers I need around 2000ms. After 1000ms I wrote the query and asked google again... Is it only me? I wish I would use my local knowledge database but I only use it when I`m not lucky with google after the first queries.


https://twitter.com/dirkk/status/255941043189583872

What I meant with that - post your solution not locally but somewhere useful (Stackoverflow?). You'll do yourself a favor and others can benefit from that.


Great tweet! :) Maybe it`s my fear of finding my own post on stackoverflow. Anyway I`ll try to follow your advice. I have a bad ratio giving back knowledge to the community- mostly due to my even worse time management.


I think the code here is a very good example to get started with Python. It includes some very essential elements for interacting with external websites.


Thanks!


Whoa! I was just doing the same thing but with stackoverflow's api and in vim ... instead of making the query from the command line i.e. leave your editor, I'm thinking of writing the query like a comment and highlight it so that I can see the returned search results in a separate buffer. Haven't made much progress due to unfamiliarity with programming vim


It was my understanding it's easy to run a terminal command from vim.


Yes, it's just

  :r!howdoi python regex
:r! executes a command and inserts its standard output below the cursor


Cool! ... that solves it! One less unfinished side project


This is amazing. Thank you so very much!

I was very impressed by responses to queries like this:

    howdoi retrieve the keys from an array of hashes in ruby

        hash = {"apple" => "fruit", "carrot" => "vegetable"}
        array = hash.keys   #=> ["apple", "carrot"]


Would be nice if it caches 2-3 answers so it doesn't have to call each time. Also if I could specify like -p123 it shows me first three answers instead of only first and I have to make a new call till I find my answer. Really useful. Thanks for sharing.


Nice suggestions! We've got a whole list going on at https://github.com/gleitz/howdoi/issues


Looks like an awesome tool for the little things that nobody bothers to memorize -- very cool!


Why keep them in your brain when StackOverflow memorizes it for you?


For much the same reason that L1 caches exist.


Indeed! If only my L1 cache were as large as my disk...


Is there a way to "contribute answers"? I think a hookup with a wiki-type system would make this useful and essentially replace the text file of commands I discovered.


No way to currently contribute answers. Another commenter mentioned cheat.errtheblog.com that uses a wiki system. The downside is that when the wiki fails (like right now) the application breaks.

You may want to just submit a good answer to StackOverflow and make sure it is well written and gets lots of votes.


I enjoy this, since it's a very clever and well executed idea. Cheers!

However, something bugs me a little; Is there any reason for not mentioning Stack Overflow in the README?


Very nice. I like the -l feature but wish the link could appear with the normal answer, doesn't seem to be a way to do that.


btw, on my system the `brew install` failed giving an error about "make the appropriate changes to your system". When I tried installing pyquery I realized it requires sudo access to write to /Library. I was able to install using "sudo python setup.py install". (pip gave a stack trace).

Tried "howdoi split string zsh" and it gave me a good answer.


Thanks for the tip, I was just figuring out what changes I should make, when run into your post.


Can't seem to get it installed either via brew, pip or python setup.py install on osx 10.8.2

Bummer. It looks really cool.


Installs just fine here on OSX 10.8.2

  % uname -a
  Darwin xxx 12.2.1 Darwin Kernel Version 12.2.1: Thu Oct 18 16:32:48 PDT 2012; root:xnu-2050.20.9~2/RELEASE_X86_64 x86_64

  % sudo pip install howdoi
  Password:
  Downloading/unpacking howdoi
    Downloading howdoi-0.2.tar.gz
    Running setup.py egg_info for package howdoi

  Requirement already satisfied (use --upgrade to upgrade): pyquery in /Library/Python/2.7/site-packages (from howdoi)
  Requirement already satisfied (use --upgrade to upgrade): lxml>=2.1 in /Library/Python/2.7/site-packages (from pyquery->howdoi)
  Requirement already satisfied (use --upgrade to upgrade): cssselect in /Library/Python/2.7/site-packages (from pyquery->howdoi)
  Installing collected packages: howdoi
    Running setup.py install for howdoi

      Installing howdoi script to /usr/local/bin
  Successfully installed howdoi
  Cleaning up...
Maybe file a bug with what happens when you try?


It works now. Had to run:

  sudo chmod -R go+w /Library/Python/2.7/site-packages/
which is noted at the bottom of the readme and I missed it.


Would be cool if one could directly call howdoi from the python shell.

>>> howdoi foo bar

Or as a magic function for ipython.


Classic Stackoverflow moment:

howdoi -a parse HTML with regex


Very nice, I'm going to use this. Thanks!


this is incredible! thanks so much for creating this. now i just need to figure out how to port it to Alfred :)


Brilliant. Given the data set, this appears somewhat intelligent, since more theoretical computer science problems may simply result in something like "advice" rather than code blocks.

$ howdoi mvp pattern

$ howdoi wittgenstein

$ howdoi hopfield network

$ howdoi feed forward network

$ howdoi artificial heart

It's like an overly precocious child that got trapped in a library, and is now set to aid you in your research/development.


$ howdoi turing

$ howdoi solve the halting problem

$ howdoi find the holy grail

I could do this all day.


"howdoi wittgenstein" is the search of the day. Well done!


I'm sure there's tons of academic literature riddled with ad hoc <code> block usage and strange deviations from standard markup.

This thing is great.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: