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.