最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

vim - How to run javascript in gvim? - Stack Overflow

programmeradmin1浏览0评论

My system : xp + gvim.
I have open gvim to edit a javascript sentence ,the file has no name yet.

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

I want to map <F7> to javascript ,how can i write the map sentence in _vimrc?
How can i get 55 when i input F7 when the configuration finished?

I do as elclanrs say ,error output is

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._pile (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...    

My system : xp + gvim.
I have open gvim to edit a javascript sentence ,the file has no name yet.

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

I want to map <F7> to javascript ,how can i write the map sentence in _vimrc?
How can i get 55 when i input F7 when the configuration finished?

I do as elclanrs say ,error output is

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._pile (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...    

Share Improve this question edited Jan 22, 2014 at 9:18 romainl 197k21 gold badges297 silver badges335 bronze badges asked Jan 22, 2014 at 7:56 showkeyshowkey 33651 gold badges163 silver badges323 bronze badges 1
  • Learn to use markdown, it will seriously help you getting help. @elclanrs answer is correct, if you have troubles with node, ask new questions on stack overflow with the node.js tag. – romainl Commented Jan 22, 2014 at 9:22
Add a ment  | 

1 Answer 1

Reset to default 8

The easiest way to run JavaScript in VIM is to install NodeJS then you can run your current buffer in Node with VIM using

:w !node

You don't even need to save it. Use console.log:

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

Mapping F7. Works on current file:

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

To execute it in the browser you'd simply write it as a script in HTML:

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

And run it the browser, with F5 for example:

map <F5> <Esc>:silent !google-chrome %<cr>
发布评论

评论列表(0)

  1. 暂无评论