Python's Way to implement tab automation and History command management _python

Source: Internet
Author: User
Tags readline

This article is an example of how Python implements tab automation and History command management. Share to everyone for your reference. The specific analysis is as follows:

Python startup file, which is the environment variable pythonstartup corresponding file

1. Add Tab key to ReadLine automatically complement the function

2. Manage history commands like a shell

The code is as follows:

Copy Code code 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)
Except IOError:
Pass
Atexit.register (Readline.write_history_file, Histfile)
Del ReadLine, Rlcompleter, Histfile, OS

I hope this article will help you with your Python programming.

One. This method can modify the automatic completion of the shell command line
1. Get the Python directory "I'm using a 64-bit Ubuntu system."

 [~$]python python 2.7.3 (default, APR 2013, 06:20:15) [GCC 4.6.3] on linux2 Type ' help
"," copyright "," credits "or" license "for the 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 '] >>> 

I see from above that Python's path on my computer is/usr/lib/python2.7.

2. Switch to the directory to write a startup.py script, the script directory is to deal with Python <tab> events, the script content 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

Copy Code code as follows:

[/USR/LIB/PYTHON2.7$]CD
[~$]vi. BASHRC

4. Increase environment variables

Copy Code code as follows:

#for python
Export pythonstartup=/usr/lib/python2.7/startup.py

5. Configure environment variables to take effect

Copy Code code as follows:

[~$]source. BASHRC

What is Pythonstartup?

Copy Code code as follows:

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

Two. This method can be automatically filled in vim

1. Download plugin:
Download Address: http://www.jb51.net/softs/305586.html

2. Copy to the corresponding directory

Copy Code code 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 VIM configuration file

Copy Code code as follows:

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

OK, is the test effective?

Related Article

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.