UPD
Vim9 has updated source
command that can source not only the file but buffer lines, in a simplest form it is just a
:source
to evaluate whole buffer, :.source
to evaluate current line.
Thus, simplified version of this plugin could be put into your .vimrc:
vim9script
# source vimscript (operator)
def SourceVim(...args: list<any>): any
if len(args) == 0
&opfunc = matchstr(expand('<stack>'), '[^. ]*\ze[')
return 'g@'
endif
if getline(1) =~ '^vim9script$'
vim9cmd :'[,']source
else
:'[,']source
endif
return ''
enddef
nnoremap <silent> <expr> <space>v SourceVim()
xnoremap <silent> <expr> <space>v SourceVim()
nnoremap <silent> <expr> <space>vv SourceVim() .. '_'
Run vimscript from anywhere in vim and save output to *
(clipboard) register.
call minpac#init()
" ...
" more plugins
" ...
call minpac#add('habamax/vim-evalvim')
Then :call minpac#update()
to install it.
There are no default mappings, add your own, for example:
xmap <leader>v <Plug>(EvalVim)
nmap <leader>v <Plug>(EvalVim)
omap <leader>v <Plug>(EvalVim)
nmap <leader>vv <Plug>(EvalVimLine)
Then having somewhere in your buffer add:
echo "hello world"
-
<leader>vv
to evaluate current line -
You should see
hello world
printed below
fun! TestFun(p)
return a:p
endfun
echomsg TestFun("Another Hello!")
-
<leader>vip
to evaluate current paragraph -
You should see
Another Hello
printed below
Note
|
You can also visually select text block and evaluate it with <leader>v .
Although you will not see the message printed — you can find it in
:messages .
|
set nu!
set cursorline!
set cursorcolumn!
-
<leader>vip
to evaluate current paragraph of settings -
line numbers, cursorline and cursorcolumns will be toggled