Vim/Neovim: Scripting using Python

alpha2phi
2 min readJun 15, 2021

Use Vimscript and Python in your development workflow.

Vim/Neovim Scripting using Python

Overview

In my previous articles, I walked through with you how to develop a simple Neovim plugin using Neovim Lua APIs. For Python developers, there is a simpler solution as both Vim and Neovim support using Python for plugin development.

For Neovim (:h provider-python), it supports Python remote-plugins (:h remote_plugin.txt) and the Vim legacy python2 and python3 interfaces. To use Python plugins, you need the pynvim module. Run :checkhealth to see if you already have it.

For Vim (:h python3), type :version to check if Vim is built with Python 3 support.

Develop Plugins using Python

Let’s develop a few Vim/Neovim plugins using Python.

Text to Speech

With just a few lines of code, I can create a text-to-speech plugin.

  • Install the pyttsx3 library.
  • Under your Vim/Neovim configuration folder, create a file called tts.vim under the autoload(:h autoload) folder.
  • Add the following lines to tts.vim.
  • Python code block is wrapped within the python3 keyword. To access Vim commands (:h python-command), I use the vim module.
  • Add the following key mappings.
nnoremap <Leader>es :call tts#Speak()<CR>
vnoremap <Leader>es :call tts#Speak(1)<CR>

Press <Leader>es and the current word under the cursor (cWORD) will be converted to speech, or you can visually select a block of text.

--

--

alpha2phi

Software engineer, Data Science and ML practitioner.