将Cursor更改为Vim中BufWritePost上的另一个窗口

时间:2011-06-02 22:39:47

标签: windows vim split

我有一个函数,无论何时调用它都会分割窗口并显示一些信息,将光标放在这个新窗口中。

到目前为止一切顺利。

但是我正在实现一个autocommand,它将触发相同的功能,除了光标永远不会改变到打开的窗口,就像它没有使用自动命令运行时,一切都很好。

触发此行的行如下所示:

 autocmd! BufWritePost *.py call MyFunction()

就像我说的那样,当你手动调用:call MyFunction()而不是使用自动命令时,它会很有效。

我认为Bram提到自动命令实际上并不意味着拆分窗口甚至移动光标。

有什么方法可以解决这个问题,还是我做错了什么?

1 个答案:

答案 0 :(得分:3)

根据ZyX在对原始问题的评论中所说的话,听起来这样可行:

function MyFunction()
    [ have all commands you currently have]
    [ . . . ]

    " then as last line include call to feedkeys()
    " this will stuff keystrokes into key buffer
    " and get executed after MyFunction() ends
    " remember that location will always be in 
    " original window, i.e, window that vim
    " was in when autocommand was triggered
    " so if new window is below original
    " window you could use this:

    " feedkeys call below edited to reflect ZyX's
    " improvement of \<C-\>\<C-n> to guarantee
    " we're in Normal mode before using window
    " movement key combo

    call feedkeys("\<C-\>\<C-n>\<c-w>j", 'n')

endfunction