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

mkview and loadview are fantastic ways to save state in vim, and many people use .vimrc commands like this to automatically save state in all files.

au BufWinLeave ?* mkview au BufWinEnter ?* silent loadview 

This creates status files (in place e.g. ~ / .vim / view)

Sometimes, however, you want to clear browsing information so that the file starts in a new state.

The only thing I can do is:

  • Find and remove the corresponding .vim / view / file from the command line
  • Temporarily edit your .vimrc to disable loadview, open the file and restore .vimrc

All of them are bulky and related to what they do outside of VIM. Is there any way:

  • Open the file without loading (or perhaps a way to pass the vimrc option to skip loadview?), So when we close we will have transparent mkview
  • Delete / clear any state set by loadview, or delete the corresponding loadview file for this file from inside vim

I suppose you could write a shell script that took the file path and tried to calculate the vim '= +' encoding of the paths in the .vim / view directory and delete it, and then call the shell script from inside vim, but it looks like vim there must be some support for this.

+7
vim
source share
2 answers

It seems that vim does not have this ability, so I needed to write a vimscript that does the right citation (thanks to the inspiration and Ingo's & viewdir note).

Here is vimscript, you can add this to your .vimrc to add a command to vim:

 " # Function to permanently delete views created by 'mkview' function! MyDeleteView() let path = fnamemodify(bufname('%'),':p') " vim 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 abbreviation 'delview') command Delview call MyDeleteView() " Lower-case user commands: http://vim.wikia.com/wiki/Replace_a_builtin_command_using_cabbrev cabbrev delview <cr>=(getcmdtype()==':' && getcmdpos()==1 ? 'Delview' : 'delview')<CR> 

After adding this parameter, you can simply:

 :delview 

From the command line and removes the view created for the current buffer / file

Your $ HOME environment variable should be set to what vim thinks ~ ~

+4
source share

Since automatic view processing starts with :autocmd , you can temporarily disable view loading through

 :noautocmd edit myfile 

Please note that this also disables any hooks made by other plugins / settings.


To delete a file from Vim, you can use delete() to define a custom command:

 :command! -nargs=1 DeleteView call delete(&viewdir . '/' . <q-args>) 

This can be further improved by creating globbing or even Vim filespec that goes into it.

+1
source share

All Articles