Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Update:</strong> My answer applied at the time it was created, but for modern versions of Node, look at <a href="https://stackoverflow.com/a/17110285/785065">this other answer</a>.</p> <p>First off, your usage of spawn isn't correct. Here are the docs. <a href="http://nodejs.org/docs/latest/api/child_processes.html#child_process.spawn" rel="nofollow noreferrer">http://nodejs.org/docs/latest/api/child_processes.html#child_process.spawn</a></p> <p>Your sample code makes it seem like you expect vim to automatically pop up and take over the terminal, but it won't. The important thing to remember is that even though you may spawn a process, it is up to you to make sure that the data from the process makes it through to your terminal for display.</p> <p>In this case, you need to take data from stdin and send it to vim, and you need to take data output by vim and set it to your terminal, otherwise you won't see anything. You also need to set the tty into raw mode, otherwise node will intercept some of the key sequences, so vim will not behave properly.</p> <p>Next, don't do readFileSync. If you come upon a case where you think you need to use a sync method, then chances are, you are doing something wrong.</p> <p>Here's a quick example I put together. I can't vouch for it working in every single case, but it should cover most cases.</p> <pre><code>var tty = require('tty'); var child_process = require('child_process'); var fs = require('fs'); function spawnVim(file, cb) { var vim = child_process.spawn( 'vim', [file]) function indata(c) { vim.stdin.write(c); } function outdata(c) { process.stdout.write(c); } process.stdin.resume(); process.stdin.on('data', indata); vim.stdout.on('data', outdata); tty.setRawMode(true); vim.on('exit', function(code) { tty.setRawMode(false); process.stdin.pause(); process.stdin.removeListener('data', indata); vim.stdout.removeListener('data', outdata); cb(code); }); } var filename = '/tmp/somefile.txt'; spawnVim(filename, function(code) { if (code == 0) { fs.readFile(filename, function(err, data) { if (!err) { console.log(data.toString()); } }); } }); </code></pre> <p><strong>Update</strong></p> <p>I seeee. I don't think readline is as compatible with all of this as you would like unfortunately. The issue is that when you createInterface, node kind of assumes that it will have full control over that stream from that point forward. When we redirect that data to vim, readline is still there processing keypresses, but vim is also doing the same thing.</p> <p>The only way around this that I see is to manually disable everything from the <code>cli</code> interface before you start vim.</p> <p>Just before you spawn the process, we need to close the interface, and unfortunately manually remove the keypress listener because, at least at the moment, node does not remove it automatically.</p> <pre><code>process.stdin.removeAllListeners 'keypress' cli.close() tty.setRawMode true </code></pre> <p>Then in the process 'exit' callback, you will need to call createInterface again.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload