mkview and loadview are fantastic ways to save state in vim, and many people use .vimrc commands such as this to automatically save state on all files.
au BufWinLeave ?* mkview
au BufWinEnter ?* silent loadview
This creates the state files (in a location such as ~/.vim/view)
Sometimes, though, you want to clear the view information so the file starts in a fresh state.
The only ways I can see doing this are:
Those are both cumbersome and involve doing things outside of vim. Isn't there some way to either:
I suppose you could write a shell script that took the file path and tried to figure out vim's '=+' encoding of paths in the .vim/view directory and remove that and then call the shell script from inside vim, but it seems like vim should have some sort of support for this.
User @pkeu pointed out that if you have automatic view creations then you need to turn these off if you are currently viewing the file that you want to delete the view for, otherwise when you exit it will just create it again, you can ensure this by changing your mkview autocommands from:
au BufWinLeave ?* mkview
au BufWinEnter ?* silent loadview
to be bundled in a group called "keepview" - which makes them easy to turn off:
:augroup keepview
: autocmd BufWinLeave ?* mkview
: autocmd BufWinEnter ?* silent loadview
:autogroup END
Then they can they be turned off by adding this line before the endfunction:
:autocmd! keepview
The only problem with this approach is that MyDeleteView isn't smart enough to check whether you are currently viewing the file you are deleting or not, and if not, then you won't save a view of the current file. The other option is to not add this at all, and just delete views of files that aren't currently open. Or write a smarter MyDeleteView() that checks if you're currently in the file.
It looks like vim doesn't have this ability, so I needed to write a vimscript that does the proper quoting (thanks to inspiration and the note about '&viewdir' from Ingo).
Here is the vimscript, you can add this to your .vimrc to add the command to vim:
" # Function to permanently delete views created by 'mkview'
function! MyDeleteView()
let path = fnamemodify(bufname('%'),':p')
" vim's odd =~ escaping for /
let path = substitute(path, '=', '==', 'g')
if empty($HOME)
else
let path = substitute(path, '^'.$HOME, '\~', '')
endif
let path = substitute(path, '/', '=+', 'g') . '='
" view directory
let path = &viewdir.'/'.path
call delete(path)
echo "Deleted: ".path
endfunction
" # Command Delview (and it's abbreviation 'delview')
command Delview call MyDeleteView()
" Lower-case user commands: http://vim.wikia.com/wiki/Replace_a_builtin_command_using_cabbrev
cabbrev delview <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'Delview' : 'delview')<CR>
After adding this, you can simply do:
:delview
From the commandline and it will delete the view created for the current buffer/file
Your $HOME environment variable must be set to whatever vim thinks '~' is
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With