14

As of nodejs 6.6.0, when using the nodejs REPL you can enter multiline text in the terminal with:

.editor

I would like the nodejs REPL to open an instance of vim (in the same manner that git does when prompting for commit information) so that I can enter the multiline text there.

Does anyone know how to configure this? I realize that I could just run the nodejs REPL within vim or neovim in the first place, but I'm looking for a pure fix here.

romainl
  • 186,200
  • 21
  • 280
  • 313
user3751385
  • 3,752
  • 2
  • 24
  • 24
  • 1
    Eric wrote a great answer. I just wanted to mention that there's a really handy plugin which makes working with REPLs in Neovim much easier. I use it to run Racket samples from SICP, but it works well with the Node REPL too. You essentially leave a terminal mode window open, and copy text from your current file to be evaluated using various keybinds. Check out [neoterm](https://github.com/kassio/neoterm). – Rich Churcher Oct 16 '19 at 06:53

2 Answers2

3

There doesn't appear to be an easy way to customize this behavior. Digging through the code for nodejs, it appears that the editor mode is simply streaming each line of together.

See here for the relevant piece of code on GitHub.

The closest solution to "use" vim in this NodeJS REPL is to enable Vi(m) mode in bash (if you're using the bash shell).

set -o vi

After running this or putting into your .bashrc file, the command line interface will act like Vim and have editing modes you have to navigate.

Personally, even though I am a Vim user, don't like the Vi mode in bash because it is difficult to know which mode you're in, even if my go-to mode is Normal mode. Adding some text to indicate which mode you're in may be possible, but I don't think its worth looking into. But your mileage will vary.

See https://sanctum.geek.nz/arabesque/vi-mode-in-bash/ for more on Vi mode in bash.

Eric Leung
  • 2,354
  • 12
  • 25
  • 1
    You can tune your inputrc to show “mode strings,” though not every command/version of bash supports them. I actually love it—it kills me that vim’s command line doesnt default to modal editing, and I need an extra keypress to get to it – D. Ben Knoble Nov 24 '19 at 05:57
  • 1
    Ah nice! I think I found what you're referring to https://stackoverflow.com/a/32614367/6873133. Looks like you'll need at least bash 4.3/4.4 to work. I'll have to check this out and see if that fixes my frustration with it. – Eric Leung Nov 25 '19 at 00:42
  • @D.BenKnoble Discovered mode strings thanks to you! Very nice although I have always been fine without them. I should add that for me vim mode makes sense everywhere because I have Caps Lock as my Escape key system wide. I am not sure there is a way map Esc to another key or combination just for `bash`/`zsh` so that may be why even vim power users but who rely on for example the `jk` trick find a vi mode on the command line not so useful. Just a pet theory of mine. – cassepipe Feb 16 '23 at 11:48
1

I found running the node REPL under VI mode in bash to not work particularly well (e.g. pressing 0 in normal mode would do nothing, rather than sending the cursor to the start of the line).

I then found this comment on github, which worked as desired. This won't open an instance of Vim for your editing, but it will let you navigate the node REPL (including under .editor) with vim controls. This is also in the node docs here.

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
mhugh
  • 21
  • 1
  • 1