【问题标题】:How do I emulate a keypress inside a Vim function?如何在 Vim 函数中模拟按键?
【发布时间】:2012-03-15 18:15:00
【问题描述】:

我将从代码开始

function BigScrollUp()
  let count = 20
  while count > 0
    "Press" CTRL-Y <-- how do I emulate this?
    sleep 5m
    count -= 1
  endwhile
endfunction

我想创建一个带有动画的快速上下滚动功能,以便我可以跟踪我要去的地方。

【问题讨论】:

标签: function vim keypress emulation


【解决方案1】:

您可以使用feedkeys()。输入:help feedkeys 阅读更多:

feedkeys({string} [, {mode}])               *feedkeys()*
        Characters in {string} are queued for processing as if they
        come from a mapping or were typed by the user.  They are added
        to the end of the typeahead buffer, thus if a mapping is still
        being executed these characters come after them.
        The function does not wait for processing of keys contained in
        {string}.
        To include special keys into {string}, use double-quotes
        and "\..." notation |expr-quote|. For example,
        feedkeys("\<CR>") simulates pressing of the <Enter> key. But
        feedkeys('\<CR>') pushes 5 characters.
        If {mode} is absent, keys are remapped.
        {mode} is a String, which can contain these character flags:
        'm' Remap keys. This is default.
        'n' Do not remap keys.
        't' Handle keys as if typed; otherwise they are handled as
            if coming from a mapping.  This matters for undo,
            opening folds, etc.
        Return value is always 0.

call feedkeys("\<C-Y>")

【讨论】:

    【解决方案2】:

    试试这个:

    " Press CTRL-Y:
    normal <Ctrl+v><Ctrl+y>
    

    按字面意思输入 ctrl+v,然后按 ctrl+y,这将导致在脚本中显示为 ^Y 的单个字符。

    【讨论】:

    • 我认为不值得为此发布我自己的问题,但是我将如何重新映射 Shift+g 以便在我的函数结束后将光标放在文件的最后一行?我一直在尝试normal &lt;S-G&gt; 的迭代
    • 试一试:normal G 注意gvim 中的G 不同。
    • 为什么需要&lt;Ctrl-V&gt;
    猜你喜欢
    • 1970-01-01
    • 2015-01-07
    • 2011-08-02
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    相关资源
    最近更新 更多