使用Vim作为Javascript沙箱:如何向节点写入缓冲区并在另一个缓冲区中读取结果?

时间:2021-03-29 03:58:39

I'm attempting to utilise vim as a complex javascript sandbox editor.

我正在尝试使用vim作为一个复杂的javascript沙箱编辑器。

The end goal is to have a left window with my code, and a right window with dynamically updating output from the node.js executable.

最终目标是有一个带有我的代码的左窗口,以及一个带有从节点动态更新输出的右窗口。js可执行。

I'm currently utilising:

我正在利用:

map <Leader>r :silent :w !node -p > /tmp/jsconsole<cr>

silent !cat '' > /tmp/jsconsole

botright  vnew
e /tmp/jsconsole

set buftype=nofile
set bufhidden=hide
set nobuflisted
setlocal noswapfile
" set nomodifiable " maybe later once i can figure out how to lock/unlock

wincmd h
set buftype=nofile
set bufhidden=hide
set nobuflisted
setlocal noswapfile

autocmd QuitPre * :qa

set autoread

This works, kind of. I find that vim's autoread function is unreliable.

这种工作,。我发现vim的autoread函数不可靠。

Ideally, I would be able to run a left buffer's contents without saving the file through node -p via stdin, read stdout and stderr into the right hand side buffer, which would not be editable.

理想情况下,我可以运行左缓冲区的内容,而无需通过节点-p通过stdin保存文件,将stdout和stderr读入右端缓冲区,这是不可编辑的。

I'm aware there are a few ways to do this, but lack the knowledge to glue it together.

我知道有几种方法可以做到这一点,但是我缺乏将它们结合在一起的知识。

  • Reading the output to a register and showing that register in a chosen buffer
  • 将输出读入寄存器并在选定的缓冲区中显示该寄存器
  • Utilising r!
  • 利用r !
  • Utilising quickfix or :make
  • 利用quickfix或者:

How can I pass a buffers contents, through stdin to node -p, capture stdout and stderr and pass them to a different buffer?

如何通过stdin将缓冲区内容传递到节点-p,捕获stdout和stderr,并将它们传递到另一个缓冲区?

3 个解决方案

#1


2  

There are various scripts more or less doing what you want.

有各种脚本或多或少地执行您想要的操作。

I recently came upon the very minimalist pipe2eval that lets you evaluate the current line or visual selection and inserts the result below, in a comment.

我最近遇到了极简主义的pipe2eval,它允许您评估当前行或可视选择,并在注释中插入下面的结果。

Dumb example:

愚蠢的例子:

my_array = [1,2,3];
my_array.push(4);
my_array[3];
// 4

#2


1  

Pursuant to Randy's comment, utilising vim-slime on top of tmux with the node REPL in the right hand tmux pane does indeed work. This is a pretty valid way to answer this question specifically, although I'm still interested in the vim-contained answer.

根据Randy的评论,在tmux上使用vim-slime和右边tmux窗格中的节点REPL确实有效。虽然我仍然对包含vim的答案感兴趣,但这是一个非常有效的方法来具体回答这个问题。

Although I haven't set it up yet, I plan to make this a specific tmux config managed by tmuxinator.

虽然我还没有设置它,但我计划将它设置为tmux配置,由tmuxinator管理。

#3


1  

You can use https://github.com/metakirby5/codi.vim just install it and add this lines to your .vimrc

您可以使用https://github.com/metakirby5/codes .vim来安装它,并将这些行添加到.vimrc中

let g:codi#interpreters = {
  \ 'javascript': {
     \ 'rightalign': 0,
  \ },
\ }

#1


2  

There are various scripts more or less doing what you want.

有各种脚本或多或少地执行您想要的操作。

I recently came upon the very minimalist pipe2eval that lets you evaluate the current line or visual selection and inserts the result below, in a comment.

我最近遇到了极简主义的pipe2eval,它允许您评估当前行或可视选择,并在注释中插入下面的结果。

Dumb example:

愚蠢的例子:

my_array = [1,2,3];
my_array.push(4);
my_array[3];
// 4

#2


1  

Pursuant to Randy's comment, utilising vim-slime on top of tmux with the node REPL in the right hand tmux pane does indeed work. This is a pretty valid way to answer this question specifically, although I'm still interested in the vim-contained answer.

根据Randy的评论,在tmux上使用vim-slime和右边tmux窗格中的节点REPL确实有效。虽然我仍然对包含vim的答案感兴趣,但这是一个非常有效的方法来具体回答这个问题。

Although I haven't set it up yet, I plan to make this a specific tmux config managed by tmuxinator.

虽然我还没有设置它,但我计划将它设置为tmux配置,由tmuxinator管理。

#3


1  

You can use https://github.com/metakirby5/codi.vim just install it and add this lines to your .vimrc

您可以使用https://github.com/metakirby5/codes .vim来安装它,并将这些行添加到.vimrc中

let g:codi#interpreters = {
  \ 'javascript': {
     \ 'rightalign': 0,
  \ },
\ }