简体   繁体   中英

Using Vim as a Javascript Sandbox: How do I write a buffer to node and read the result in another buffer?

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

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.

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.

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.

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!
  • Utilising quickfix or :make

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

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.

Dumb example:

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

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM