3

I'm trying to replicate my vscode setup in neovim, but none of the options I have found seem to work.

Goal

A vertically split application. Left side: source code, split into cells with the # %% marker. Right side: interactive ipython notebook

In the interactive notebook, I can:

  1. send parts/cells of the source
  2. type new adhoc code for "rapid prototyping", and
  3. see matplotlib figures.

The solution ideally works in wezterm on all 3 main operating systems.

enter image description here

Is there a way to get this setup in neovim?

What I've tried

iron.nvim

This is the most promising.

(+) can send parts/cells of source
(+) can run adhoc code in interactive window
(-) cannot plot any figures inline

Nearest to plotting is run plt.plot() to plot all created figures in external windows. The disadvantages are that I need to leave nvim to see them, and that I need to recreate the figures to see them again after closing their windows.

enter image description here

molten.nvim

(+) can run parts of source
(-) does not recognise # %% cell markers, so I need to select the lines before running them
(-) ipython session is not shown, so cannot run adhoc code (unless I type it in the main file, select it, and run it, which is cumbersome)
(+-) figures are shown, but in a separate terminal split. (And on my system there is a bug, with the figures continually redrawn until I leave the selected code section - see the repeated figures on the right side.)

There is an option to use kitty (the terminal emulator? the image protocol?) and possibly show the images in the "floating" output window (shown on the left in the screenshot as the "inserted" buffer), but I cannot get it to work.

enter image description here

Jupynium

(+) can run cells of code
(+) can display figures inline
(-) separate browser window; leaving nvim; uncomfortable workflow
(-) cannot run adhoc code (I can add a cell in the browser and run adhoc code there, and this is not synced back to the source due to one-way syncing. So-far, so good. However, the plugin then gets confused about the correspondence between the source code cells and the notebook in the browser, rendering it useless.)
(-) buggy in general (i.e. when editing the source code, the browser does not get correctly updated)

enter image description here

Jukit

(+) can run parts of source
(-) does not recognise # %% cell markers, so I need to select the lines before running them
(-) does not show figures, at least not in wezterm. I cannot use kitty because I need something that also runs on windows. Wezterm does understand the kitty image protocol, but the Jukit does not seem to make that differentiation.

enter image description here


My configuration can be found here.

If there are other plugins I should try, do let me know.

Also, if I made any mistakes in my config, and one of the 4 plugins I discussed can in fact do what I want, it's much appreciated if you correct me.

Finally, if I've gotten used to a workflow for which there is a much better alternative which I don't need to shoe-horn into vim, please do share.

4
  • 1
    Why go from VSCode to Neovim at all? You seem to be happy with your setup, stick with it.
    – Friedrich
    Commented Jun 17 at 19:10
  • 1
    I use neovim for everything else and I want to also use it for this final use-case. Also, the completely different way of editing text in both completely does my head in 😅
    – ElRudi
    Commented Jun 17 at 20:32
  • Thanks for the nice comparison for the different solutions. I believe Neovim/Vim will probably always be behind since the display is a purely text based where Visual Studio Code display is an html page. The strength of Neovim/Vim is the editor part which is a strong motivation to try to move you do :-) I suppose you have to select one and collaborate with the other to make it better (keeping in mind that for the sheer display you'll always stay behind ;-)) Commented Jun 18 at 5:56
  • 1
    Yes, the terminal is only text-based. Yet, there are ways (kitty image protocol, sixel, ...) to get graphics in there, e.g. see the screenshot under "molten". I'm hoping there is a solution that combines the best of all worlds and leave vscode behind for good.
    – ElRudi
    Commented Jun 18 at 8:28

1 Answer 1

3

I have tried all of the above and the solution I find works best is the combination of jupytext.vim and jupyter-vim with jupyter qtconsole. qtconsole lets you display the plots inline, and you can hide the menu and scrollbar by editing the config so it just looks like an ipython console. You also need to allow output from other clients. Here's a sample ~/.jupyter/jupyter_qtconsole_config.py:

c = get_config()
c.JupyterQtConsoleApp.hide_menubar = True
c.ConsoleWidget.scrollbar_visibility = False
c.HistoryConsoleWidget.include_other_output = True
c.FrontendWidget.include_other_output = True 
c.JupyterWidget.include_other_output = True

jupytext.vim lets you edit notebooks as if they were .py files with cell markers as # %% when used with the option: let g:jupytext_fmt = 'py:percent'. jupyter-vim lets you send code to the qtconsole.

The workflow would be as follows: first run jupyter qtconsole & then vim your_notebook.ipynb. In vim, :JupyterConnect then you can start sending code cells to qtconsole with <localleader>X or visual selection with <localleader>e.

enter image description here

6
  • Very nice solution! Do you know how to make the QtConsole automatically scroll graphs (when I output graphs the QtConsole stop autoscrolling and I see only the very top of the graph at the bottom of the console window)? Commented Jun 19 at 5:27
  • 1
    @VivianDeSmedt Indeed this issue exists and is known "bug" in qtconsole, as referenced here but you can patch it as suggested
    – gui
    Commented Jun 19 at 5:47
  • Great! it works like a charm :-) Thanks! Commented Jun 19 at 5:59
  • Nice, many thanks for your answer! In your screenshot, the qtconsole is a separate (tmux?) window/pane, is that correct? Is there a way to run it in a terminal window inside neovim, so it's a neovim split that I can easily navigate to, using the same color theme, etc?
    – ElRudi
    Commented Jun 21 at 7:28
  • @ElRudi It is indeed a separate window (but not tmux, qtconsole is gui). I use a window manager (i3wm) so I can easily switch between windows with <super>h/j/k/l.
    – gui
    Commented Jun 21 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.