[英]Using external commands in vim map-operator function
I'd like to create a vim mapping that allows for motions to control what text to output. 我想创建一个vim映射,该映射允许动作控制输出什么文本。 There doesn't seem to be a whole lot of documentation on this and I'm having trouble adapting the sample in vim's help page to what I need.
关于这方面似乎没有太多的文档,并且我无法使vim帮助页面中的示例适应我的需要。 Here's what I have, which gives no errors but does not replace the text.
这就是我所拥有的,它没有错误,但不能代替文本。 What am I missing?
我想念什么?
nmap ,s :set opfunc=CustomFunc<cr>g@
function! CustomFunc(type, ...)
let sel_save = &selection
let &selection = "inclusive"
let reg_save = @@
if a:type == 'line'
silent exe "normal! '[V']!sort"
elseif a:type == 'block'
silent exe "normal! `[\<C-V>`]!sort"
else
silent exe "normal! `[v`]!sort"
endif
let &selection = sel_save
let @@ = reg_save
endfunction
I used !sort
as a very simple example for testing. 我使用
!sort
作为一个非常简单的测试示例。 The idea here is you could have a block like: 这里的想法是您可能有一个类似的代码块:
{
Hello, World!
Hello, Earth!
Hello, Planet!
}
If you were to then move the cursor somewhere in the {} block and type ,si{
it would replace the block with 如果要在{}块中将光标移动到某处并键入
,si{
,它将替换为
{
Hello, Earth!
Hello, Planet!
Hello, World!
}
The workaround would be vi{:!sort<cr>
, or if you have vmap ,s :!sort
then vi{,s
, but it would be nice to have this as an operator mapping (less typing, more flexible, etc). 解决方法是
vi{:!sort<cr>
,或者如果您有vmap ,s :!sort
然后是vi{,s
,但最好将其作为运算符映射(键入更少,更灵活等)。 。
I don't think external filtering with :!
我不认为使用
:!
外部过滤:!
can ever be anything else then linewise. 沿线可以是其他任何东西。
If you require something like that, I think you need to resort to things like the system
function and "manually" manipulate the buffer contents, instead of relying on Vim's filtering command. 如果您需要这样的东西,我认为您需要诉诸
system
功能之类的东西并“手动”操作缓冲区内容,而不是依靠Vim的过滤命令。
And in the linewise case, the following, much simpler, incantation fixes it: 在逐行情况下,以下更简单的方法可以解决此问题:
'[,']!sort
That is, instead of silent exe "normal! '[V']!sort"
. 即,代替
silent exe "normal! '[V']!sort"
。 I reckon the additional mode-switching implied with exec/ normal
kills the operator-pending mode (?). 我认为exec /
normal
所隐含的其他模式切换会杀死操作员挂起的模式(?)。
{
Hello, World!
Hello, Earth!
Hello, Planet!
}
(cursor on W
, type , S i B ): (
W
光标,类型为, S i B ):
{
Hello, Earth!
Hello, Planet!
Hello, World!
}
Ex commands are always linewise, so it doesn't easily work what you want. Ex命令始终按行排列,因此无法轻松实现所需的功能。 There are some plugins, that help you with that (eg Dr. Chips VIS plugin or my NrrwRgn plugin. But I am not sure, if they help you in that particular case of omaps.
有一些插件可以帮助您解决此问题(例如Dr. Chips VIS插件或我的NrrwRgn插件。但是我不确定,在特定的omaps情况下它们是否对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.