Python implements automatic Tab population and History Command Management

Source: Internet
Author: User
This article mainly introduces how to implement Tab auto-completion and History Command Management in Python. The example shows how to implement tab auto-completion, which has some reference value, for more information about how to implement Tab auto-completion and historical command management in Python, see the example in this article. Share it with you for your reference. The specific analysis is as follows:

Python startup file, that is, the file corresponding to the environment variable PYTHONSTARTUP

1. add the tab key auto-completion function for readline

2. Manage History commands like Shell

The code is as follows:

The code is as follows:

Import rlcompleter
Import readline
Import atexit
Import OS
# Http://stackoverflow.com/questions/7116038/python-tab-completion-mac-osx-10-7-lion
If 'libedit 'in readline. _ doc __:
Readline. parse_and_bind ('bind ^ I rl_complete ')
Else:
Readline. parse_and_bind ('Tab: complete ')
Histfile = OS. path. join (OS. environ ['home'], '. pyhist ')
Try:
Readline. read_history_file (histfile)
Handle T IOError:
Pass
Atexit. register (readline. write_history_file, histfile)
Del readline, rlcompleter, histfile, OS

I hope this article will help you with Python programming.

I. This method can be used to modify the auto-completion of shell command lines.
1. obtain the python Directory [I am using a 64-bit ubuntu system]

[~$]pythonPython 2.7.3 (default, Apr 10 2013, 06:20:15) [GCC 4.6.3] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import sys>>> sys.path['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0','/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/ubuntuone-client', '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', '/usr/lib/python2.7/dist-packages/ubuntuone-couch', '/usr/lib/python2.7/dist-packages/ubuntuone-installer', '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol']>>> 

As shown above, the path of python on my computer is/usr/lib/python2.7.

2. switch to this directory and write a script for startup. py. the script directory is used to process python. Event. the script content is as follows:

#!/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

3. switch to your home directory

The code is as follows:


[/Usr/lib/python2.7 $] cd
[~ $] Vi. bashrc

4. add environment variables

The code is as follows:


# For python
Export PYTHONSTARTUP =/usr/lib/python2.7/startup. py

5. the configuration environment variables take effect

The code is as follows:


[~ $] Source. bashrc

What is PYTHONSTARTUP?

The code is as follows:


If this is the name of a readable file, the Python commands in that file are executed before the first prompt
Is displayed in interactive mode. The file is executed in the same name space where interactive commands are
Executed so that objects defined or imported in it can be used without qualification in the interactive session.
You can also change the prompts sys. ps1 and sys. ps2 in this file.

II. This method can be automatically completed in VIM.

1. download the plug-in:
: Http://www.bitsCN.com/softs/305586.html

2. copy the corresponding directory

The code is as follows:


Unzip pydiction-1.2.1.zip
Cp python_pydiction.vim/usr/share/vim/vim73/ftplugin
Mkdir/usr/share/vim/vim73/pydiction
Cp complete-dict/usr/share/vim/vim73/pydiction/
Cp pydiction. py/usr/share/vim/vim73/pydiction/

3. modify the vim configuration file

The code is as follows:


Let g: pydiction_location = '/usr/share/vim/vim73/pydiction/complete-dict'
Let g: pydiction_menu_height = 20

OK. check whether the test takes effect.

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.