How do I execute Vimscript code from the current buffer?
Answer
:source % or :'<,'>source
Explanation
The :source command executes Vimscript from a file. When used with % (current file) or a visual selection, you can run code directly from your buffer — making Vim into an interactive Vimscript scratchpad.
Execute the entire file
:source % " Execute all Vimscript in the current file
:so % " Short form
Execute selected lines
" Select lines in visual mode, then:
:'<,'>source
" Or for a specific range:
:10,20source
Practical uses
" Reload your vimrc without restarting
:source ~/.vimrc
:source $MYVIMRC
" Test a function you're writing
" Write the function in a buffer, select it, source it
function! MyHelper(text)
return toupper(a:text) . '!'
endfunction
" Select above, :'<,'>source, then test:
:echo MyHelper('hello') " → HELLO!
" Apply a temporary setting
:source " (with visual selection of 'set cursorline')
Interactive development workflow
- Open a
.vimfile or scratch buffer - Write your Vimscript function or configuration
- Select the code and
:sourceit - Test it immediately
- Iterate and re-source
Tips
:sourceruns each line as an Ex command- For Lua (Neovim): use
:luafile %or:lua << EOF ... EOF $MYVIMRCis a built-in variable pointing to your vimrc- Use this workflow for developing custom functions, testing mappings, or debugging config
- Neovim users can also use
:sourcewith Lua files (.lua) - Documented under
:help :source