Python command to add Tab key Auto-complete

Source: Internet
Author: User

Environment Introduction
Os:rhel 5.5
Python version: 2.7.6
We are used to using the TAB key when using Linux commands, and we can implement similar functions under Python.
Like the tab function of the Linix shell, Python can also be implemented!
The specific implementation process is as follows:
1. Writing a Python script
$ cat tab_enable.py
#!/usr/bin/python
# Python startup file
Import Sys
Import ReadLine
Import Rlcompleter
Import atexit
Import OS
# tab Completion
Readline.parse_and_bind (' Tab:complete ')
# History File
Histfile = Os.path.join (os.environ[' HOME '), '. Pythonhistory ')
Try
Readline.read_history_file (Histfile)
Except IOError:
Pass
Atexit.register (Readline.write_history_file, Histfile)
Del os, Histfile, ReadLine, Rlcompleter
2. Calling scripts

Place the script in the Python default module, under the path,
/usr/local/python27/lib/python2.7/tab_enable.py


Start calling
>>> Import tab_enable
>>> Import OS
>>> os.<tab>

Note:
1. Install readline times wrong gcc:readline/libreadline.a:no such file or directory, workaround
[[email protected] readline-6.2.4.1] #yum-y install Readline-devel
[[email protected] readline-6.2.4.1] #yum-y install patch
[[email protected] readline-6.2.4.1]# python setup.py install

Python command to add Tab key Auto-complete

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.