How to execute the contents of a buffer?

跟風遠走 提交于 2019-12-29 19:25:53

问题


I have a vim script I'm developing in my current buffer and I want to execute it. Is there a simple way to do it?

After a long search I have only found two related ways, but not exactly what I'm looking for:

  • a) "source" command - but to use it I first need to save the content to file and then "source" it back, which doesn't look simple

  • b) "call" command - but I don't want to call my function, I want to execute the whole file, which defines several functions and has some code outside of functions at all


回答1:


Use:

:%y"                      (alternatively: ggyG)
:@"

It will copy whole buffer into default register and then run it.

See :help :y and :help :@ (and also :help range maybe).




回答2:


An autocmd to source the file whenever you save might help:

autocmd BufWritePost <buffer> source %


来源:https://stackoverflow.com/questions/5326430/how-to-execute-the-contents-of-a-buffer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!