2023-06-22

Vim Floaterm Plugin - Python Code Runner

The simplest method to run Python code in Vim is to use the command:

   :! py %

that prints the script's standard output/error to the underlying command-line terminal (Command Prompt on Windows OS).

Another option, which is shown in the video below, it's to redirect the output of a Python script inside of a pop-up window. The following mapping for the keyboard sequence leader p y will save the active buffer and then run the script using Windows OS shortcut for the Python interpreter.

    "" .vimrc
    ...
    nnoremap <silent><Leader>py :w <bar> :FloatermNew --height=0.4 
        \   --width=0.9 --wintype=float --name="pcr" 
        \   --disposable --position=bottom --autoclose=0 py % <cr> 
    ...

or in the case of a Linux/Bash terminal:

    "" .vimrc
    ...
    nnoremap <silent><Leader>py :w | :FloatermNew --height=0.4 
        \   --width=0.9 --wintype=float --name="pcr" 
        \   --disposable --position=bottom --autoclose=0 python % <cr> 
    ...

The command FloatermNew is part of the plugin Vim-Floaterm.

Gnu Octave with Alacritty Terminal Emulator

The video above also shows the GNU-Octave REPL opened directly from VIM using Alacritty Terminal Emulator.

Let's suppose Gnu-Octave (version 8.2.0 at the time of writing) installation root folder is C:\octave-8.2.0-w64, then the command Start part of Dispatch.vim plugin can be used as follows:

   :Start -dir=C:\\octave-8.2.0-w64 alacritty.exe --command mingw64\bin\octave.bat

The default shell program was set to the command interpreter cmd.exe in Alacritty's configuration file.

    # %AppData%a\alacritty\alacritty.yml
    ...
    # Shell

    shell:
      program: cmd.exe
    ...