codegourmet

savory code and other culinary highlights

Vim Modifiable / Readonly Buffer

| Comments

Sometimes, vim shows an annoying warning saying

1
2
E21: Cannot make changes, 'modifiable' is off
Press ENTER or type command to continue

or:

1
2
Changing readonly file
Press ENTER or type command to continue

In my case, it happened with all buffers that were created via plugins (for example the Vundle output or the vim-taskwarrior detail view).

This went away for a short time if I deleted all files inside ~/.vim/views.

After rummaging around for quite a while inside my .vimrc, I found the reason for this:

1
2
au BufWinLeave * silent! mkview
au BufWinEnter * silent! loadview

This is very useful as it saves and automatically reloads the current state (cursor, etc) of the file’s buffer.

When a readonly buffer window is dynamically created by a plugin and then closed, mkview will create a view for this window and since the script declared it as readonly, mkview will persist that state.

Next time the plugin is creating the dynamic window, the previous readonly state will be restored and the plugin won’t be able to write into it’s own window!

I found the following snippet here: Make views automatic

1
2
au BufWritePost,BufLeave,WinLeave *.* mkview
au BufWinEnter *.* silent loadview

The ?* solution from the vim wiki link above didn’t work for me, I guess because the buffer that had the problems has a name and thus is satisfying the expression.

expand("%") sounds promising, but someone noted that when closing a tab and the next tab is nameless, querying % might be unreliable.

For now I’ll restrict mkview to *.* files - the solution at the bottom of said wiki entry sounds ok but I’m not sure if I’m comfortable including this rather big script just for being able to save the state of files without a dot in their name.

Happy Coding! – codegourmet

Comments