10

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?

13

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

In short:

:%y|@"<CR>

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 '15 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. – EvergreenTree May 8 '15 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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