Introduction to Python script in VIM

Source: Internet
Author: User

When using Python scripts, you need to learn the appropriate knowledge. VIM has a wide range of Python script applications. The current implementation principle is very simple. Execute the VIM function JPython () in VIM, and then the function is responsible for calling python (of course, the common version of python needs to be put into the environment variable ).

The called command is "python-c...", that is, directly execute the following string, which contains the complete execution script. In the script, first open a file and use fp. readlines () read all rows and put them in a list (g_text). Then execfile () is used to call the Script Name passed in by JPython () and the global variable g: JPYTHON_PATH. In this way, you can directly use the g_text variable without worrying about file IO and path issues in the Python script. After python outputs the content using print or sys. stdout, the original content is replaced by VIM. The Code is as follows.

 
 
  1. Function! JPython (py_fname)
  2. Let w: fname = g: JPYTHON_PATH. '/'. a: py_fname
  3. Echo bufname (".")
  4. Let w: cmd = 'fp = open (r "'. bufname (". ").'", "rb ");'
  5. Let w: cmd. = 'G _ text = fp. readlines ();'
  6. Let w: cmd. = 'fp. close (); del fp ;'
  7. Let w: cmd. = 'execfile ("'. w: fname .'");'
  8. Exec '%! Python-c "'. escape (w: cmd ,"'\"").'"'
  9. "^ M will be generated after the output is found and replaced
  10. If has ('windows ')
  11. Try
  12. Exec '% s/\ r // G'
  13. Catch
  14. Endtry
  15. Endif
  16. Endfunction
  17. Let g: JPYTHON_PATH = "D:/scripts/vim"

Copy the function to vimrc and configure g: PYTHON_PATH to the specified position.

This is just a simple implementation of the idea. There may be a better way to operate in the actual environment, and there should be a better way to achieve interaction between VIM and Python. Some may ask why VIM scripts are not directly used. The main reason is that I only implement this simple function for VIM scripts. I believe there are not many people studying VIM scripts to use VIM, not to mention complex applications. python is relatively popular and widely used. It is relatively skilled in writing and modifying.

If you have any good ideas or suggestions, please leave a message for me. You are also welcome to propose a better solution.

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.