Write a simple Vim Plugin in Python 3

Introduction

――I wanted to know more about Vim, so I wrote a Vim Plugin in Python (I'm using Neovim ...) --The first Vim Plugin in my life

What I made

--This is https://github.com/shase/passgen.vim --Just generate a password with the specified digit and insert it into vim's buffer

Directories, files

.
├── autoload
│   └── passgen.vim
├── plugin
│   └── passgen.vim
└── python3
    └── passgen.py

--If you just want to run it as a Plugin, you should have at least 3 files. ――Of course you should also have a test --If you want to publish it, you should also match doc, README and LICENCE.

Creation flow

First, write the main process as ordinary Python

python3/passgen.py


  
import secrets
import string


def passgenvim_generate_password(size=''):
   chars = string.ascii_letters + string.digits
   return ''.join(secrets.choice(chars) for x in range(int(size)))

Write Vimscript that calls Python under autoload

autoload/passgen.vim


let s:save_cpo = &cpo
set cpo&vim

py3file <sfile>:h:h/python3/passgen.py
python3 import vim

function! passgen#passgen(size)
  python3 vim.command("call setline('.', '%s')" % passgenvim_generate_password(vim.eval('a:size')))
endfunction

let &cpo = s:save_cpo
unlet s:save_cpo

Call the function written in autoload from the command

plugin/passgen.vim


let s:save_cpo = &cpo
set cpo&vim

command! -nargs=1 PASSGEN call passgen#passgen(<f-args>)

let &cpo = s:save_cpo
unlet s:save_cpo

OK like this

I used it as a reference

Recommended Posts

Write a simple Vim Plugin in Python 3
Write a vim plugin in Python
Write a simple greedy algorithm in Python
Write a super simple molecular dynamics program in python
Create a plugin to run Python Doctest in Vim (2)
Create a plugin to run Python Doctest in Vim (1)
Write a binary search in Python
Write A * (A-star) algorithm in Python
Write a pie chart in Python
Write a depth-first search in Python
Implementing a simple algorithm in Python 2
Run a simple algorithm in Python
A simple HTTP client implemented in Python
Try drawing a simple animation in Python
Create a simple GUI app in Python
Write a short property definition in Python
Write a Caesar cipher program in Python
Set up a simple HTTPS server in Python 3
Create a Vim + Python test environment in 1 minute
A simple Pub / Sub program note in Python
Create a simple momentum investment model in Python
Split files when writing vim plugin in python
A Vim plugin that automatically formats Python styles
Set up a simple SMTP server in Python
Take a screenshot in Python
Create a Wox plugin (Python)
Create a function in Python
Create a dictionary in Python
Write beta distribution in Python
Write python in Rstudio (reticulate)
Make a simple Slackbot with interactive button in python
Make a bookmarklet in Python
Write a log-scale histogram on the x-axis in python
Simple regression analysis in Python
Draw a heart in Python
Simple IRC client in python
A simple way to avoid multiple for loops in Python
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
First simple regression analysis in Python
Hit a command in Python (Windows)
Simple OAuth 2 in Python (urllib + oauthlib)
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
Create a binary file in Python
Solve ABC036 A ~ C in Python
Create a Kubernetes Operator in Python
Write python list fast vim tips
Solve ABC037 A ~ C in Python
Write a super simple TCP server
Draw a CNN diagram in Python
Create a random string in Python
Simple gacha logic written in Python
Write C unit tests in Python
Schedule a Zoom meeting in Python