假设我正在编辑我的_vimrc文件,我刚刚添加了几行,例如新的键映射.我不想重新加载整个文件(:so %),因为这将重置我正在尝试的许多临时内容.我只想运行我正在处理的两行.
我没有运气试图将行复制/粘贴到命令缓冲区,因为我不能在那里使用put命令.有什么办法可以将当前行(或当前选择)作为EX命令运行吗?
摘要:
在Anton Kovalenko的回答和Peter Rincker的评论之后,我得到了这些关键地图,如果处于可视模式,它们执行当前行或当前选定的行:
" Execute current line or current selection as Vim EX commands.
nnoremap <F2> :exe getline(".")<CR>
vnoremap <F2> :<C-w>exe join(getline("'<","'>"),'<Bar>')<CR>
Run Code Online (Sandbox Code Playgroud)
Tho*_*ius 36
要将当前行作为ex命令执行,您还可以使用:
yy:@"
Run Code Online (Sandbox Code Playgroud)
这会将当前行拉到"-register并执行它.我不认为打字太多.
Ant*_*nko 16
在光标下执行行作为Ex命令:
:execute getline(".")
Run Code Online (Sandbox Code Playgroud)
方便2线.(我想弄清楚用区域做的事情,但我不是vim用户).对于目前选定的地区,以下似乎可以完成这项工作:
:execute getreg("*")
Run Code Online (Sandbox Code Playgroud)
正如Peter Rincker所评论的,此映射可用于执行当前选定的行:
:vnoremap <f2> :<c-u>exe join(getline("'<","'>"),'<bar>')<cr>
Run Code Online (Sandbox Code Playgroud)
为此,我定义了以下命令和映射:
":[range]Execute Execute text lines as ex commands.
" Handles |line-continuation|.
" The same can be achieved via "zyy@z (or yy@" through the unnamed register);
" but there, the ex command must be preceded by a colon (i.e. :ex)
command! -bar -range Execute silent <line1>,<line2>yank z | let @z = substitute(@z, '\n\s*\\', '', 'g') | @z
" [count]<Leader>e Execute current [count] line(s) as ex commands, then
" {Visual}<Leader>e jump to the following line (to allow speedy sequential
" execution of multiple lines).
nnoremap <silent> <Leader>e :Execute<Bar>execute 'normal! ' . v:count1 . 'j'<CR>
xnoremap <silent> <Leader>e :Execute<Bar>execute 'normal! ' . v:count1 . 'j'<CR>
Run Code Online (Sandbox Code Playgroud)
发布此内容后,我找到了解决方法。我可以使用 将文本复制到剪贴板"*y,然后使用鼠标中键将该文本放入命令缓冲区。这对我有用,但对于没有剪贴板支持、鼠标支持或只是不愿意将手从 Vim 位置移开的人来说,这并不是一个方便的解决方案。