Thursday, July 8, 2010

Tab completion in the python interpreter

Since I program mostly in python, I spend a lot of time in the default python interpreter of my distro. I even use it as a replacement for bash quite often - mainly because of python's excellent string manipulation capabilities and well, I confess, also because bash's syntax always baffles me (especially the if statements). So whenever I get stuck with some bash code, I take the escape route of opening up the python interpreter.

But the default python interpreter that comes bundled with most linux distros (and also the OS X), lacks a killer feature of bash: tab completion. One can find this feature in some python interpreters like bpython, but they sometimes overdo it (popping up suggestions even when one doesn't want them, for example).

Anyway, I recently learned from this excellent blog post by Matthias Friedrich that it is REALLY easy to add tab completion in the python interpreter: all one needs to do is import rlcompleter and readline modules of python and assign a completer key (Tab is the most natural choice, of course). The blog post I linked above also describes how to set the PYTHONSTARTUP environment variable so that tab completion is enabled every time the interpreter is opened.

Since python's readline module uses the same GNU readline utility used by bash for tab-completion, it should be just as much fun, right? Well, that's not quite the case. Because tab completion imposes a severe restriction on you here: you cannot use the Tab key for indenting your code anymore. And using single space for python's code indentation is a very bad idea: trying to distinguish between different indentation levels in heavily nested code blocks is a trouble not worth taking even for sake of the benefits given by tab completion.

Anyway, I figured out a way to circumvent the problem by looking at these lines in the source code of the rlcompleter module (The file in question is /usr/lib/python2.6/rlcompleter.py for Debian and Ubuntu): When one presses the completion key the function complete(self, text, state) defined in line#29 is called with the word being typed as it's second argument. I simply added an extra elif after the 42nd line asking it to return '\t' if the argument consists of only whitespaces: So, now I have all the goodness of tab completion without losing the ability of using tab for code indentation!

64 comments:

  1. Many institutions limit access to their online information. Making this information available will be an asset to all.
    Custom Research Paper

    ReplyDelete
    Replies
    1. I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai

      Delete
  2. Excellent Thank you.

    ReplyDelete
  3. For most kids, Santa already rates pretty highly replica belts in their eyes, given the whole presents, elves and magical reindeer schtick. For the kids stuck behind a car crash in Coppell, Texas early last week prada replica, the jolly old elf has likely moved to hero status.

    Former Arlington, Texas volunteer fire fighter chanel outlet Brad Luddeke was on his way to deliver toys to needy kids when he came across the accident on Interstate 635. Luddeke training louis vuitton outlet went into full swing, as he pulled a driver from one of the crashed vehicles not only once, but again, when he noted the car replica handbags catching fire.

    ReplyDelete
  4. Heya i am for the first time here. I found this board and I find It truly useful & it helped me out a lot. I'm hoping to offer one thing again and aid others such as you helped me. seo manchester

    ReplyDelete
  5. acheter vente maillot nba pas acheter vente maillot nba pas cher cher Such bones humble and flatter not just the security they have, many people are present, because acheter vente maillot nba pas cher one acheter vente maillot nba pas cher kind of fear and a desire, or almost impossible for the kind of opportunity. cheap to pay the acheter vente maillot nba pas cher fare, because the http://www.nailartmetisse.fr poor sale after watching the film it is no extra money, of course, left the next bucks in his pocket, pulled out to not only make the .

    ReplyDelete
  6. You can acquisition the air-conditioned accumulating of the new figures. Such accoutrements are a abundant alternative for females, who would like to authority a fashionable bag but cannot do so because of breitling replica the high costs. If you are advantageous enough, you can even analysis good, aboriginal appearance artist handbags. If you are planning to acquirement avant-garde and contemporary broad handbags and purses, accomplish abiding that you do a omega replica affable exchange analyze afore selecting a benefactor or even a handbag. Back you can ascertain so abounding forms in replica handbags, you can get abashed while purchasing one. Accomplish abiding that the louis vuitton replica backpack is alive and can be acclimated at altered places. Also, because the amount ambit is absolutely low-priced, you can acquirement two or three accoutrements and allowance them to your friends, babe acquaintance or wife. Also, accomplish abiding you accumulate the personality of the anyone in mind. Cheap Replica HandbagsAsk a woman what she brand affairs and I’m academic that abounding will say handbags and shoes. These are top of replica handbags abounding women’s ambition account for their birthdays, Christmas and sometimes just for the heck of it.Gowns in accession to t shirts are aswell accomplished items to market. General t shirts may actual able-bodied be purchased with attention to beneath than $6 for every section and may end up getting readily broadcast with attention to to be a acceptable accord back two times it has the aboriginal value. Opt for any banker / banker which offers beginning layouts, accomplished products, in accession to replica watchesarchitecture advanced articles so you may rapidly bazaar your own articles in accession to apprehension rapidly action of one’s stocks. Also, advance chargeless of amount shipment accuse ads fabricated accessible through a acceptable amount of wholesalers.





    ReplyDelete
  7. ralph lauren shirts, http://www.poloralphlaurenshirts.us.com/
    oakley, http://www.occhialioakleyoutlets.it/
    ray ban, http://www.occhiali-rayban.it/
    oakley outlet, http://www.oakleyoutlet.in.net/
    oakley sunglasses, http://www.oakleysunglasses-outlet.us.com/
    kobe bryant shoes, http://www.kobebryantshoes.in.net/
    pandora jewelry, http://www.pandorajewelryoutlet.us.com/
    cheap nike shoes, http://www.cheap-nikeshoes.cc/
    hollister uk, http://www.hollistershirts.co.uk/
    hermes belt, http://www.hermesbelts.us/
    cheap wedding dresses, http://www.cheap-weddingdresses.net/
    coach outlet canada, http://www.coachoutletcanada.com.co/
    cheap jordans, http://www.cheapjordanshoes.in.net/
    north face outlet, http://www.thenorthface.me/
    oakley sunglasses, http://www.oakleysunglassesdiscount.us.com/
    louis vuitton handbags, http://www.louisvuittonhandbags.org.uk/
    soccer jerseys, http://www.soccerjerseys.us.com/
    cheap nfl jerseys, http://www.cheapnfljerseys.org/
    juicy couture outlet, http://www.juicycoutureoutlet.net/
    the north face clearance, http://www.thenorthfaceclearances.us.com/
    chanel handbags, http://www.chanelhandbagsoutlet.in.net/
    yoga pants, http://www.yogapants.us.com/
    futbol baratas, http://www.futbol-baratas.com/
    prada handbags, http://www.pradahandbagsoutlet.co.uk/
    air jordan 13, http://www.airjordan13s.com/
    cai2015107

    ReplyDelete
  8. Being a interpreter is a very nice and interesting job but this job is not easy. An interpreter should be very educated and perfect in his job and in uk essay writing service and for this purpose he have to do much study and struggle.

    ReplyDelete
  9. I like this a lot. Thank you for sharing. I'm always looking for upcycles like this. In the end, you don't know it was a shipping pallet to begin with!
    Watch Live Cricket Streaming Online Free Score www.cricinfobuzz.com
    Live Cricket score crickinfo
    Watch live Matchs South Africa vs India test

    ReplyDelete

  10. If you want to know National Cat Day 2016 and why is it celebrated by pet lovers all across the globe,
    just have a look at my blog.
    You too can celebrate this day with your loving pet cat.

    ReplyDelete
  11. Its very good and very excellent article. i really enjoyed this post and i hope you will keep posting this kinds of post in future.

    ReplyDelete
  12. valentine day Facebook profile pic cute valentines day quotes valentines day quotes for him to make someone happy, and by this, you can also make someone your Valentine. You can decorate your PC and mobile screen on this event by these type of images

    ReplyDelete
  13. Visit us now to view & share beautiful collection of Best Images of
    rose day quotes

    ReplyDelete
  14. Visit us now to view & share beautiful collection of Best Images of
    happy chocolate day 2017

    ReplyDelete
  15. It can be hard to break through to the misfortune alleviation office if gathering specialists are debilitated from exchanging calls. This is one of the advantages of having an assistant, for example, a lawyer or a lodging advisor. The first will scare charge authorities and the second may have contacts inside the misfortune mit office. car title loans

    ReplyDelete
  16. They were so pleasant. They clarified that losing a vocation was the ideal motivation behind why the bank ought to adjust their credit. They knew precisely who to converse with at the bank. They thought of an advance alteration plan and conveyed frames for Joe and Dawn to round out. Check Cashing

    ReplyDelete
  17. Managing an account organizations and money related loan specialists typically offer data just about their items. Their sites have data identifying with their product offering and you may locate some deceptive information that makes it troublesome for you to comprehend which items are really the best. cash advance corona

    ReplyDelete
  18. The fortunate thing about secured home credit is that it is offered with lower financing cost. As a result of the security gave this kind of credit accompanies low intrigue. cash advance

    ReplyDelete
  19. Home value advances, when used to diminish obligation, for example, Visas, are a decent approach to help recover your FICO assessment fit as a fiddle. When taking out your new terrible credit home advance, you will need to search around a bit to get the absolute best arrangement conceivable on your advance terms. cash advance

    ReplyDelete
  20. I'm certain that I don't need to clarify what you could do with the cash, however you may be upbeat to realize that with a dealer credit; there are no confinements on how you spend it. Regardless of whether it be to make up for lost time with bills, make finance or extend your advertising endeavors, having the additional working capital can truly have any kind of effect in your business. payday loans chula-vista

    ReplyDelete
  21. On the off chance that your application is affirmed, there is a couple of various elements that are included. There is be sum took into account financing, a rate of that sum that the bank will consider a service agreement, and the quantity of rate focuses that the merchant can add to the credit. car title loans

    ReplyDelete
  22. This implies those hoping to purchase condo structures should be keen with their choices and ensure that he is working with a trustworthy organization that has involvement with this kind of venture when the economy is low. The accompanying is some present data about how the home loan banks are functioning. car title loans

    ReplyDelete
  23. great article, I was very impressed about it, wish you would have stayed next share

    lennyfacetext.com

    ReplyDelete
  24. I loved the post, keep posting interesting posts. I will be a regular reader...

    Affordable Online Essay Help UK

    ReplyDelete
  25. On the off chance that you are hoping to settle your loan obligation, you will need to investigate your accounts. Settling any sort of obligation is a procedure when your paycheck is attempting to make least installments. Since a loan online is a transient credit, intrigue includes quick. Keep these minimal effort advances here and now when you pay them off on the first due date.

    ReplyDelete
  26. Kenapa wanita ingin melakukan cara menggugurkan kandungan untuk mengakhiri kehamilan yang tidak di inginkan dengan paksa . Ada beberapa alasan seseorang ingin melakukan cara menggugurkan hamil seperti terlalu banyak anak , tidak siap menjadi ibu , masing mementingkan karir dan sebagainya . Menggugurkan kandungan mempunyai tingkat risiko yang sangat tinggi dan berat . Oleh sebab itu Anda harus memikirkan cara aborsi aman agar tidak membahayakan diri Anda . telat datang bulan merupakan hal yang wajar yang di alami oleh wanita , lantas bagaimana
    cara melancarkan haid ? Menjaga pola makan sehat dan hindari stress merupakan salah satu untuk mengatasi haid tidak lancar . Bagi pria perlu juga untuk mengetahui cara merangsang wanita agar pasangan Anda lebih bergairah dalam melakukan hubungan bersama Anda .

    ReplyDelete
  27. This information was really valuable, hope you keep it updated. Jual Lemari Arsip

    ReplyDelete
  28. I thank you for the information and articles you provided

    ReplyDelete
  29. Packers and Movers Bangalore - Reliable and Verified Household Shifting Service Providers Give Reasonable ###Packers and Movers Charges. Cheap and Best Office Relocation Compare Quotation for Assurance for Local and Domestic House Shifting and Get estimates today to save upto 20%, ***Read Customer Reviews - @ Packers And Movers Bangalore

    ReplyDelete
  30. Get Packers and Movers Jaipur List of Top Reliable, 100% Affordable, Verified and Secured Service Provider. Get Free ###Packers and Movers Jaipur Price Quotation instantly and Save Cost and Time. Packers and Movers Jaipur ✔✔✔Reviews and Compare Charges for household Shifting, Home/Office Relocation, ***Car Transportation, Pet Relocation, Bike SHifting @ Packers And Movers Jaipur

    ReplyDelete
  31. Get Shifting/Relocation Quotation from ###Packers and Movers Delhi. Packers and Movers Delhi 100% Affordable and Reliable ***Household Shifting Services. Compare Transportation Charges and Save Time, Verified and Trusted Packers and Movers in Delhi, Cheap and Safe Local, Domestic House Shifting @ Packers And Movers Delhi

    ReplyDelete
  32. An awesome blog thanks a lot for giving me this great opportunity to write on this.please visit our website I assure you that it will be very useful for you @ Packers And Movers Jaipur

    ReplyDelete
  33. Packers and Movers Chennai Give Safe and Reliable ***Household Shifting Services in Chennai with Reasonable ###Packers and Movers Price Quotation. We Provide Household Shifting, Office Relocation, ✔✔✔ Local and Domestic Transportation Services, Affordable and Reliable Shifting Service Charges @ Packers And Movers Chennai

    ReplyDelete
  34. With the progression and modernisation of the general public, the requirement for 'recreation' has turned into a vital one which ought not be disregarded. Barbacue Catering

    ReplyDelete
  35. Albeit a great many people go to school and graduate one of the various devoted instructive projects that are accessible, a few competitors can acquire certificate on the off chance that they have a four year college education that isn't explicit for recreational treatment, Gym Flooring

    ReplyDelete