GHCi can send data to Vim via :terminal (and vice versa)

:terminal is a Vim feature. That command creates a Vim window with a terminal (shell) in it. There is a very good documentation for it: :help terminal

From Vim you can send input to the terminal:
:call term_sendkeys(2, "ghci\<CR>")
where 2 is the buffer number of the terminal (find it with :ls)
The above function call will send ghci and a return key to the terminal, resulting in starting the ghci program. We can send any input this way. For example to get info on the Int type we can write in the Vim command line:
:call term_sendkeys(2, ":info Int\<CR>")

You can call Haskell functions, or ask for type information or :uses or anything, really… It seems that there are many vim plugins that can do such things. Also see @maxigit solution.

However, the :terminal allows ghci to reply back with data, to send data to Vim. From the :terminal documentation ( direct go to cmd :help terminal-api ) we learn that:

The job can send JSON to Vim, using a special escape sequence.  The JSON
encodes a command that Vim understands.  Example of such a message:
<Esc>]51;["drop", "README.md"]<07> 

drop is same as the Vim command :drop which edits a file.

So, from ghci I can write:

ghci> putStrLn "\ESC]51;[\"drop\", \"/etc/motd\"]\x07" 

and there will be no output as it is sent to Vim, which calls the functions drop with that argument ( motd - message of the day file), and a Vim window with the content of the file is created.
As explained in the documentation, beside drop you can call a user defined function with a special prefix.

GHCi has this command :def which allows us to create custom GHCi commands. Such custom macros could wrap normal GHCi commands and convert their output to the special sequence and JSON so to be send directly to Vim…

If you know of any vim plugin or attempt that implements this two-way interaction between ghci and vim, please let me know. I’m interested in solutions that are using :terminal and not tmux or screen…
If there are no such solutions yet… can you implement it, please :slight_smile: ? ( I’d like to but it will take a lot of time for me as I don’t know vim script…)

I think that vim & ghci can provide a great development environment that utilizes minimal resources… and always be around.

4 Likes