I am trying to execute a keystroke from function when its called. However, it does not work.

function SmartNERDTreeFocus()
    if expand('%') =~ 'NERD_tree.*'
        normal <C-w><C-p>
        "execute 'normal \<c-w>\<c-p>'
        "return "\<C-w>\<C-p>"
        call NERDTreeFocus()

map <silent> <C-b> :call SmartNERDTreeFocus()<CR>

Hitting the shortcut will open up NERDTree, but hitting it again while in NERDTree's buffer does nothing. I want it to return to the previous window (C-wC-p).

What is the correct way to execute some keystrokes from a vimscript function in neovim?

  • 3
    you can either use wincmd p or you need to put \<C-W>\<C-P> into double quotes Jun 8, 2021 at 15:48
  • Both of those methods works! Thanks
    – Drew
    Jun 8, 2021 at 15:50


Your Answer

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

Browse other questions tagged or ask your own question.