Search code examples
vimviewcommandwindow

Vim - load view and mkview errors in command line window


when I use this script in my .vimrc:

"Save and restore folds when a file is closed and re-opened
autocmd BufWinEnter                    ?* if(!empty(glob(expand("%:p")))) | silent loadview | endif
autocmd BufWritePost,BufLeave,WinLeave ?* if(!empty(glob(expand("%:p")))) | mkview          | endif

It creates errors when I open the command line window via q: (which opens up all the previous commands that I typed):

error image

How can I fix that, and make sure that this error does not occur?


Solution

  • Certain commands are not allowed in the command-line-window; :help E11 explains that.

    To deal with this in your :autocmd, there are several options:

    • suppress any error by prepending :silent! to the :mkview command
    • surrounding the call with :try ... catch /^E11:/ to just selectively ignore this error, but report anything else
    • add a check for the command-line window in your :if condition; in recent Vim versions, you can use getcmdwintype(); it returns an empty String in other windows; for a check that also works with older Vim versions, see ingo#compat#window#IsCmdlineWindow() in my ingo-library plugin