Write a vim plugin in Python

Thing you want to do

--I want to make a plugin for vim

――But I don't understand vimscript! !!

--I want to write in Python! !! !! !!

What to make

--If you enter MyNameIs <name>, you will see Hello <name>

--Implementation escapes to python, vimscript is used like a python wrapper

Python in VimScript

I often use python and pyfile

Python and pyfile are essentially the same, the difference between whether the source is written directly or separated into files. For example, the following two codes are the same

python:

python print('Hello')

pyfile

pyfile hello.py

hello.py


print('Hello')

Note that if you write python, that line is already in the Python world. I wrote the following and was very worried.

function! hello#hello(name)
  python hello_hello(a:name)
endfuncion

It is easy to understand that this is a mistake by rewriting it as follows

function! hello#hello(name)
  python << endpython
hello(a:name)
endpython
endfuncion

There is also a pydo one. If you write pydo <body>,

def _vim_pydo(line, linenr):
  <body>

Is created, and the line contents are passed to line and the line number is passed to linenr for each line in the selected range. If you return a value, the line will be replaced with that value, but since it is a function, you must write return as a matter of course. So it's a little long and crap. It seems that ruby can write this kind of thing more beautifully.

For example, if you execute the code below, the line number will be inserted at the beginning of the line in the selection range.

pydo return str(linenr) + line 

Implementation

Directory structure

plugin/
  hello.vim
autoload/
  hello.vim
src/
  hello.py

hello.py

Create a function without worrying about vim.

1/25 postscript: @Thnka pointed out the possibility of name conflicts. It pollutes the global space and must be prefixed or included in the class.

src/hello.py


def hello_hello(name):
  print('Hello {0}'.format(name))

autoload/hello.vim

This is the liver

There are three things to do.

  1. Load the created python script.
  2. Import vim without python
  3. Create a function called from plugin and hit a python function inside the function

First, load the created python script. Note how to get the script path.

autoload/hello.vim


pyfile <sfile>:h:h/src/hello.py

Then import vim. If you do not do this, you will not be able to exchange values.

autoload/hello.vim


python import vim

Finally, create a function called from the plugin side and hit the python function inside the function. Since python and the missing line are already in the python world, pass arguments through the vim module.

autoload/hello.vim


function! hello#hello(name)
  python hello(vim.eval('a:name'))
endfunction

plugin/hello.vim

This is no different from a normal plug-in. Bridging the command and the function described in autoload

plugin/hello.vim


command! -nargs=1 MyNameIs call hello#hello(<f-args>)

Completed form

src/hello.py


def hello_hello(name):
  print('Hello {0}'.format(name))

autoload/hello.vim


let s:save_cpo = &cpo
set cpo&vim

pyfile <sfile>:h:h/src/hello.py
python import vim

function! hello#hello(name)
  python hello_hello(vim.eval('a:name'))
endfunction

let &cpo = s:save_cpo
unlet s:save_cpo

plugin/hello.vim


if exists("g:loaded_hello")
  finish
endif
let g:loaded_hello = 1

let s:save_cpo = &cpo
set cpo&vim

command! -nargs=1 MyNameIs call hello#hello(<f-args>)

let &cpo = s:save_cpo
unlet s:save_cpo

reference

-Until the Vim plugin is made-Bocchi study session

Other

I actually made a plugin called httpstatus.vim. You can see what the HTTP status code meant from Vim. The text uses Python's BaseHTTPServer.BaseHTTPRequestHandler.responses.

I made it enthusiastically, but apparently mattn had already made it. In addition, it seems to be more sophisticated than the one I made this time (you can check not only the status code but also the message), but anyway I can only check from the status code, so it was enough.

Recommended Posts

Write a vim plugin in Python
Write a simple Vim Plugin in Python 3
Write a binary search in Python
Write A * (A-star) algorithm in Python
Create a plugin to run Python Doctest in Vim (1)
Write a pie chart in Python
Write a depth-first search in Python
Write the test in a python docstring
Write a short property definition in Python
Write a Caesar cipher program in Python
Write a simple greedy algorithm in Python
Write Python in MySQL
Split files when writing vim plugin in python
A Vim plugin that automatically formats Python styles
Take a screenshot in Python
Create a Wox plugin (Python)
Write Pandoc filters in Python
Create a function in Python
Create a dictionary in Python
Write beta distribution in Python
Write python in Rstudio (reticulate)
Make a bookmarklet in Python
Draw a heart in Python
I want to write in Python! (2) Let's write a test
Write a log-scale histogram on the x-axis in python
Write your blackbird plugin # 001
Write your blackbird plugin # 003
Write a vim plugin in Python
Write a simple Vim Plugin in Python 3
Maybe in a python (original title: Maybe in Python)
How to write a Python class
Write a table-driven test in C
[python] Manage functions in a list
Hit a command in Python (Windows)
Write JSON Schema in Python DSL
Create a DI Container in Python
Automatically format Python code in Vim
Write an HTTP / 2 server in Python
Draw a scatterplot matrix in python
Write AWS Lambda function in Python
ABC166 in Python A ~ C problem
Create a binary file in Python
Write selenium test code in python
Implementing a simple algorithm in Python 2
Create a Kubernetes Operator in Python
Write python list fast vim tips
Solve ABC037 A ~ C in Python
Run a simple algorithm in Python
Draw a CNN diagram in Python
Create a random string in Python
Write C unit tests in Python
Schedule a Zoom meeting in Python
Write a batch script with Python3.5 ~
When writing a program in Python
Generate a first class collection in Python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Solve ABC175 A, B, C in Python
Use print in a Python2 lambda expression
A simple HTTP client implemented in Python
Do a non-recursive Euler Tour in Python
I made a payroll program in Python!
Precautions when pickling a function in python
Display a list of alphabets in Python 3
Try sending a SYN packet in Python