如何在gvim中运行javascript?

时间:2014-01-22 07:56:10

标签: javascript vim

我的系统:xp + gvim。
我打开gvim来编辑一个javascript语句,该文件还没有名字。

print(sum(range(1, 10)));

我想将<F7>映射到javascript,如何在_vimrc中编写地图句子?
当配置完成后输入55时,如何获得F7

我按elclanrs说,错误输出是

C:\WINDOWS\system32\cmd.exe /c ( node ^<C:\DOCUME~1\sanya\LOCALS~1\Temp\VIi8C.tmp)    
[stdin]:1    
print(sum(range(1, 10)));    
      ^    
ReferenceError: range is not defined    
    at [stdin]:1:11    
    at Object.<anonymous> ([stdin]-wrapper:6:22)    
    at Module._compile (module.js:456:26)    
    at evalScript (node.js:532:25)    
    at ReadStream.<anonymous> (node.js:154:11)    
    at ReadStream.EventEmitter.emit (events.js:117:20)    
    at _stream_readable.js:920:16    
    at process._tickCallback (node.js:415:13)    
shell returned 8    
Hit any key to close this window...    

enter image description here

1 个答案:

答案 0 :(得分:8)

在VIM中运行JavaScript的最简单方法是安装NodeJS,然后使用

在Node中运行当前缓冲区
:w !node

您甚至不需要保存它。使用console.log

console.log(sum(range(1, 10)));

映射F7。适用于当前文件:

map <F7> :call Run() <cr>
function Run()
  exec "! node %"
endfunction

要在浏览器中执行它,您只需将其编写为HTML格式的脚本:

<script>
  alert('in browser!');
</script>

然后在浏览器中运行它,例如F5:

map <F5> <Esc>:silent !google-chrome %<cr>
相关问题