
Howdoi – Instant Coding Answers via the Command Line - gleitz
https://github.com/gleitz/howdoi
======
roryokane
According to the source code
([https://github.com/gleitz/howdoi/blob/master/howdoi/howdoi.p...](https://github.com/gleitz/howdoi/blob/master/howdoi/howdoi.py)),
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.

~~~
gleitz
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`

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

------
davej
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"];
    
            ?>

~~~
gleitz
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.

~~~
nicpottier
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.

~~~
gleitz
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...](http://stackoverflow.com/questions/7737146/django-template-
date-format)

$ howdoi format dates django templates -p2 -l

[http://stackoverflow.com/questions/1180372/how-to-format-
dat...](http://stackoverflow.com/questions/1180372/how-to-format-datetime-in-
django-template)

$ howdoi format dates django templates -p3 -l

[http://stackoverflow.com/questions/4087717/django-
template-d...](http://stackoverflow.com/questions/4087717/django-template-
date-filter-format-string-question)

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

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

------
decktech
_$ 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.

~~~
tom68
reset your password using iForgot

------
nicpottier
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...](http://stackoverflow.com/search?q=format+dates+django+templates)

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.

~~~
gleitz
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>

------
nnq
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 :)

~~~
spajus

      howdoi kill myself
      Thread.Abort

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

------
alexkus
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...

------
cllns
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.

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

------
darkarmani
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.

~~~
gleitz
Does SO provide data dumps like Wikipedia?

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

~~~
a3_nm
August 2012 : [http://www.clearbits.net/creators/146-stack-exchange-data-
du...](http://www.clearbits.net/creators/146-stack-exchange-data-dump)

------
coldpie
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.

~~~
geofft
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.

~~~
coldpie
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...](https://mailman.archlinux.org/pipermail/arch-dev-
public/2012-September/023558.html)

~~~
geofft
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.

------
andrewljohnson
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.

~~~
bnegreve
> 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.

------
roryokane
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.

~~~
robertskmiles
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.

------
travisby
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 ;)

~~~
gleitz
Excellent. Fork away!

------
madethemcry
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.

~~~
dirkk0
<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.

~~~
madethemcry
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.

------
Hawkee
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.

~~~
gleitz
Thanks!

------
tathagatadg
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

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

~~~
jgeralnik
Yes, it's just

    
    
      :r!howdoi python regex
    

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

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

------
lightyrs
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"]

------
firesofmay
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.

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

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

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

~~~
grandpa
For much the same reason that L1 caches exist.

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

------
niggler
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.

~~~
gleitz
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.

------
thiderman
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?

------
mtraven
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.

------
crazydiamond
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.

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

------
slajax
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.

~~~
junkblocker
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?

~~~
slajax
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.

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

>>> howdoi foo bar

Or as a magic function for ipython.

------
hughdbrown
Classic Stackoverflow moment:

howdoi -a parse HTML with regex

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

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

------
nerdfiles
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.

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

~~~
nerdfiles
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.

