5
I'd like to be able to evaluate a line of Javascript (or other code) in Vim, and have the result appear in a comment at the end of the line.
For example, if I have a file like:
console.log(4 + 5);
and my cursor is on that line, and I use the command :.w !node -e
, the result "9
" will appear in the buffer below. if I use :. !node -e
, the whole line will be replaced with the result, 9
.
How can I instead create a command that will append the result to the line's end in a comment, like so:
console.log(4 + 5); // 9
Also of note is that using :r
will print whatever is passed to it in the next line. r !echo "This is text."
will insert "This is text.
" on the line below the cursor. However a command like...
:r ". !node -e"
...will try to read ". !node -e"
as a file instead of a command; I don't really understand this behavior, but it makes things a bit less intuitive.
I'd like the end result to be somewhat reminiscent of the way the Atom text editor handles Hydrogen kernels, with the result of the code eval appearing at the left of the code. The plan is to create a keybinding (<C-Enter>
or <F5>
) to auto-evaluate the current line and display the result.
Oh wow, this works almost perfectly! Now I just need to find a way to adapt it to analyzing code. Is there a way to have this read to the end of the line, and then pipe the line to
node
orpython3
instead? I'll set as the accepted answer once I figure out how to do that, as this technically doesn't answer my question exactly. That said, it's pretty dang close! – diplomaticDeveloper – 2018-07-26T04:02:59.340To add to this, I think you have the first two characters of the command swapped around; it should be
_"ay/...
. – diplomaticDeveloper – 2018-07-26T04:46:25.927