Evaluate JavaScript in Vim

2021-12-25

I’ve made an application for evaluating output of a number of lines i JavaScript (or any other interpreted language) in Vim.

This is possible in Vim out of the box. Select a number of lines in visual mode, enter command mode. This will focus selected lines as such: :'<, '>. Now add :w !node (or python etc.) and the output will be displayed.

My application make something similar. If I make a visual selection and press <leader>i the output is displayed in a floating window.

function! GetResult()
    let result_cmd = system("node ~/Documents/display-message/scratch-output")
    let output = "\n\n" . "==> " . result_cmd
    exec 'call system("echo \"" .
        \ expand(output) . "\" >> ~/Documents/display-message/scratch-output")'
endfunction

vnoremap <leader>i
    :w! ~/Documents/dev/display-message/scratch-output<CR>
    :call GetResult()<CR>
    :!~/Documents/dev/display-message/build/msg<CR>

About | Archive