Consider the following function

function! Exp()
    call feedkeys(":new ~/.vimrc\<CR>")
    call search('F')

My expectation from Exp() was that if I call Exp() then

1) I see my vimrc file opened in a new window, and
2) the first instance of 'F' is searched in the vimrc file with the cursor landing on it.

However, what I see is that only the first expectation meets reality. In fact, the second line call search('F') seems to be ignored by vim.

PS: Please feel free if you have a suggestion to improve my code.

  • What if you replace call feedkeys(...) with split $MYVIMRC? – D. Ben Knoble Apr 16 '20 at 17:47
  • @D.BenKnoble I am sorry I do not follow. Can you elaborate on your suggestion? Also, perhaps I should have mentioned it in the question, that .vimrc is not really important here. I'd like to do this for arbitrary files. Thanks. – caffeinemachine Apr 16 '20 at 18:03
  • 1
    You dont need to call feedkeys to run an ex command. Just run the ex command (split, similar to new, and use $MYVIMRC to always get the right vimrc file) – D. Ben Knoble Apr 16 '20 at 18:04
  • That solves my issue. Thanks. But now I have another question. Why is call feedkeys(":split $MYVIMRC\<CR>") different from split $MYVIMRC? in the second line of the code. – caffeinemachine Apr 16 '20 at 18:13
  • 1
    Have you read :help feedkeys() ? It has to do with the underlying buffer of keystrokes getting manipulated, which often has unforseen impacts. Easier to just let the vimscript engine do things in order. You rarely need feedkeys. – D. Ben Knoble Apr 16 '20 at 18:17

You rarely need feedkeys(), which does things with vim’s user input lookahead buffer and can cause weird effects.

If you just want to run an ex command, you can do so literally in vimscript (which is just ex commands):

" alternately: split $MYVIMRC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.