I'm used to just yanking and pasting the register @" into the commandline to execute vimscript, when I'm doing testing experimentation (since it's faster than pasting it into the .vimrc)

Any other ways to execute vimscript?

  • 1
    Note that @" in command-line isn't really "pasting" the register into the command-line... But :@ is a command by itself... You can use it anywhere where a command is possible. But you can't use @" as part of an existing command, after :echo for example, that doesn't really work...
    – filbranden
    Commented Sep 13, 2020 at 23:57
  • 1
    oh, didn't know! my bad! I saw somewhere once that @" was recommended to execute vimscript, and that it was just posting the content of the register (one of the available register if i recall right). Commented Sep 13, 2020 at 23:59
  • 1
    It's a subtle point... In effect it's not much different from what you described. Yeah @" is pretty much what I use myself too. You can wrap it in a couple of mappings to make it easier to use
    – filbranden
    Commented Sep 14, 2020 at 0:27

1 Answer 1


The :@ command, to run a set of Ex commands, is the most direct one to run a block of Vimscript directly.

There are some limitations, such as you can't define local functions and variables (in the s: namespace.)

Until recently, Vim would break a :@ if line-continuations starting with backslash were used, but that was fixed in version 8.2.0997.

I think my main recommendation here is simply to create a mapping to make it more convenient to use. It's possible to make something more sophisticated that would try to save and restore registers, but there are downsides to that, for example if the Ex block being executed is trying to set the register itself... Furthermore, saving and restoring registers requires running in a function, which means you can't use that mapping to replace the function itself, while it's being executed...

So I'd suggest something fairly simple, just using a named non-default register for the sake of preserving the typical paste from the default register across this operation.

I'd also suggest defining this for filetype Vim only, so in file ~/.vim/after/ftplugin/vim.vim:

xnoremap <buffer> <silent> <Leader>s "sy:@s<CR>

Use a visual selection to pick the block of Vimscript to execute and use <Leader>s to source it.

For completeness, you might want to update the b:undo_ftplugin variable to undo the mapping if you change filetypes:

let b:undo_ftplugin .= '| xunmap <Leader>s'

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.