开发者

Change Cursor to another window on BufWritePost in Vim

开发者 https://www.devze.com 2023-03-10 06:11 出处:网络
I have a function that whenever it is called it splits the window and displays some information, placing the cursor in this new window.

I have a function that whenever it is called it splits the window and displays some information, placing the cursor in this new window.

So far so good.

But I am implementing an autocommand that will trigger the same function, and everything works great except that the cursor never changes to the opened window like when it is not running开发者_如何学Go with the autocommand.

The line that triggers this looks like:

 autocmd! BufWritePost *.py call MyFunction()

Like I said, it works great when you call manually :call MyFunction() but not with the autocommand.

I think Bram mentioned that autocommands are really not meant to split windows or even move the cursor.

Is there any way around this or am I doing something wrong?


Going by what ZyX said in the comments to original question, it sounds like this would work:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消