When using the command-line history window, I'd like to go back to the file I use it on to see what I'm working with. When revisualizing a block of code (gv), the cursor will go to the top after entering command-line history (q:). How do I switch between these two windows/panes?

command-line history

3 Answers 3


Generic answer:

<C-w>p moves the cursor to the previous window.

See :help window-move-cursor and, more generally, :help windows.

Specific answer:

You can't switch to another window while the command-line window is focused, as stated in :help cmdline-window:

In the command-line window the command line can be edited just like editing
text in any window.  It is a special kind of window, because you cannot leave
it in a normal way.
  • 2
    I get "E11: Invalid in command-line window; <CR> executes, CTRL-C quits", trying to do this.
    – oddRaven
    May 8, 2015 at 9:31
  • 1
    Please, see my edit.
    – romainl
    May 8, 2015 at 9:41
  • 1
    You can switch to another window from the cmdline window if you have executed a :wincmd before. It will then be in your cmdline history and you can execute it from there, like :wincmd p<CR>q:k<CR>. This also closes the cmdline window, however, so it's not very useful. But it should be possible to save the cursor position when leaving the cmdline window and so one can close it, do something else, and resume working with the cmdline history in the same place.
    – jjaderberg
    May 8, 2015 at 14:27

Sadly you can't. You have to close the window first.

From the help :h E11 :

The command-line window is not a normal window.
It is not possible to move to another window or edit another buffer.
All commands that would do this are disabled in the command-line window.
Of course it is possible to execute any command that you entered in the command-line window.
Other text edits are discarded when closing the window.


As others have mentioned, window commands are very limited for the cmdline window and there is no way to move the cursor to the previous window while keeping the cmdline window open. Here is a suggestion that, though it won't allow ordinary 'window switching', may simplify returning to look at the content of the buffer from which you summoned the cmdline window.

function! JJCmdWinResume()
    let cmd = 'q:'
    if exists('g:CmdWinCurPos')
        let cmd .= ':call setpos(".", g:CmdWinCurPos)^M'
    return cmd

nnoremap <expr> <Leader>q JJCmdWinResume()

:autocmd CmdWinEnter * nnoremap <buffer> <Leader>q :let g:CmdWinCurPos = getpos('.')<CR>:q<CR>

The idea is to allow storing the position of the cursor in the cmdline window when leaving it, and to allow returning to that cursor position when reentering it. We create a general expression map for Leader+qthat simply evaluates to opening the cmdline window (q:) if there is no cursor position stored, but if a position is stored, then it also sets the cursor to this position. Then with an autocmd we set a buffer specific map for the cmdline window, with the same lhs, that stores the cursor position before quitting the cmdline window. This way you can quit the cmdline window with Leader+q, look at the buffer you are working on, then reenter the cmdline window with the same keys and continue from where you left off.

Note that ^M in the function is a literal carriage return, to be entered with CTRL-V+RETURN. (If you yank-put this code you will have to replace the two characters ^ and M in this way.)

  • Great approach! I'm adopting this into my workflow
    – Blasco
    Oct 20, 2019 at 22:07
  • It doesn't seem to be fully working though, it get's stuck at the :call setpos
    – Blasco
    Oct 20, 2019 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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