2023-03-17

Vim's Compiler System for VLANG and Python

This note briefly describes how to compile in VIM a VLANG program and set the Ipython console as an interactive debugger for Python scripts on Windows OS.

V-vim plugin provides syntax highlight and it also assigns the proper filetype (vlang) to V buffers. Another very useful plugin Dispatch.vim provides a set of commands to run Vim's compiler system asynchronously.

A compiler plugin defines the options that should be used with a specific compiler, such as errorformat and makeprg options. These plugins are located in a directory included in the runtime path (~/.vim/after/compiler in my configuration) and named as filetype.vim (e.g. python.vim).

.vimrc

Vim's compiler plugins are not automatically selected based on the buffer's filetype (unless you have a specific filetype plugin). However, it's possible to set the right compiler plugin every time VIM opens a file, by including the following AU command in the .vimrc file.

   au BufRead * try | execute "compiler ".&filetype | catch /./ | endtry

VLANG

As shown in the video at the bottom of this page, the command Make (part of Dispatch.Vim plugin) starts a build in the foreground, and eventual errors will be displayed automatically inside a Quick Fix list.

Here's the VIM compiler plugin used in this example.

    " filename: ~/.vim/after/compiler/vlang.vim
    " Vim compiler file
    " Compiler:             V Compiler
    " Maintainer:           @seve_py
    " Latest Revision:      2023-03-16
    if exists('current_compiler')
      finish
    endif
    let current_compiler = 'vlang'

    if exists(':CompilerSet') != 2              
      command -nargs=* CompilerSet setlocal <args>
    endif

    "                    path to vlang compiler       
    CompilerSet makeprg=C:\\Users\\Seve\\workplace\\v\\v.exe\ %

    CompilerSet errorformat=%f:%l:%c:%m

Python

A python script does not require to be compiled. In this case, the command .Spawn will open the Ipython console as an interactive debugger. Note that the plugin compiler uses the specific Windows OS Python's launcher (py), and it also defines some useful abbreviations.

    " filename: ~/.vim/after/compiler/python.vim
    " Vim compiler file
    " Compiler:             Python Compiler embedded Ipython console
    " Maintainer:           @seve_py
    " Latest Revision:      2023-03-16

    if exists('current_compiler')
      finish
    endif
    let current_compiler = 'python'

    if exists(':CompilerSet') != 2              " older Vim always used :setlocal
      command -nargs=* CompilerSet setlocal <args>
    endif
    CompilerSet makeprg=py\ %

    CompilerSet errorformat=%f:%l:%c:%m

    abb pybp embed()
    abb Ipi from IPython import embed