4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python3で簡単なVim Pluginを書く

Last updated at Posted at 2020-08-21

はじめに

  • Vimのことをもっと知りたいと思い、PythonでVim Pluginを書いてみた(Neovim使ってるけど...)
  • 人生初のVim Plugin

作ったもの

ディレクトリ、ファイル

.
├── autoload
│   └── passgen.vim
├── plugin
│   └── passgen.vim
└── python3
    └── passgen.py
  • Pluginとして動かすだけなら、最低限3つのファイルがあれば良さそう
  • もちろんテストもあったほうがよい
  • 公開するのであれば、更にdocやREADMEやLICENCEも合ったほうが良い

作成の流れ

まずmainの処理を普通の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)))

Pythonを呼び出すVimscriptを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

autoloadに書いた関数を、コマンドから呼び出す

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

参考にしました

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?