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?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityThe :@
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'
@"
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...@"
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).@"
is pretty much what I use myself too. You can wrap it in a couple of mappings to make it easier to use