Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: How to remove/clear views created by mkview from inside of vim

Tags:

vim

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:

  1. Find and remove the corresponding .vim/view/ file from the command-line
  2. Temporarily edit your .vimrc to turn off the loadview, open the file, and restore the .vimrc

Those are both cumbersome and involve doing things outside of vim. Isn't there some way to either:

  1. Open a file without doing loadview (or perhaps a way to pass an option to the vimrc to skip the loadview?) so when we close we will have a clear mkview
  2. Remove/clear any state set by loadview or else erase the corresponding loadview file for a given file from inside vim

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.

like image 395
David Ljung Madison Stellar Avatar asked Feb 07 '15 15:02

David Ljung Madison Stellar


1 Answers

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

like image 157
David Ljung Madison Stellar Avatar answered Sep 21 '22 02:09

David Ljung Madison Stellar