Martin Wang
Martin Wang

Reputation: 1007

How to switch to another vim window and specify cursor location

I have two vim windows, and I want to go to another window according to a function.

I hope to specify the cursor location in my function. The code is

call feedkeys("\<C-W>W<CR>")
call cursor(10,10)

However, the cursor function is still bound to original window. How to let cursor know I have switched to new window?

Upvotes: 1

Views: 138

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172768

feedkeys() just adds (virtual) keystrokes to the input queue, and that will only be processed once your Vim function returns. That's why your cursor placement is executed before the switch in windows.

In general, avoid feedkeys() unless you really need it! (Those situations are few.) To execute a normal mode command, use :normal! instead. For window commands, there's even a special :wincmd (as you've discovered).

Upvotes: 3

Martin Wang
Martin Wang

Reputation: 1007

I find a solution:

wincmd w
call cursor(10,10)

Upvotes: 1

Related Questions