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
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|@"'
-
-
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. Commented May 8, 2015 at 11:52