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

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


As the automatic view handling is triggered by :autocmd, you can temporarily disable the loading of the view via

:noautocmd edit myfile

Note that this also turns off any hooks done by other plugins / customizations.


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

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

This could be further improved by building globbing or even Vim's filespec escaping into it.

Tags:

Vim