I have a nofile buffer that contains vimscript, and I want to execute it. However, :so % will not work because it requires a saved file, not a buffer. Is there a vim command to do this by default? If not, is it possible to create a function which does the same things?

1 Answer 1


Yes, you can yank the whole buffer with ggyG or :%y and execute it with :@".

In short:


Used in a mapping:

nnoremap <key> :%y|@"<CR>

Used in a flexible command that works on the whole buffer by default or an optional range:

command! -bar -range=% Foo execute <line1> . ',' . <line2> . 'y|@"'
  • you have a missing colon in first command.
    – Vitor
    May 7, 2015 at 15:29
  • Also, this does clobber the " register. I imagine you could use another register if you wanted to, or you could just save the " register and restore it afterwards. May 8, 2015 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.