force vim to overwrite external changes

心不动则不痛 提交于 2019-12-05 21:04:03

Indeed, the overwrite confirmation cannot be turned off with :w!, and :set autoread doesn't help in this case, neither. What does work is instructing Vim to explicitly check for changes before the write:

:checktime | w

I believe

set autoread

should do it. It tells Vim to automatically re-reads the file changed outside Vim.

I saw this in a mailing list. Apparently it is called if the file has changed timestamp, after a call to an external shell command.

function! ProcessFileChangedShell()
    if v:fcs_reason == 'mode' || v:fcs_reason == 'time'
        let v:fcs_choice = ''
    else
        let v:fcs_choice = 'ask'
    endif
endfunction

autocmd FileChangedShell call ProcessFileChangedShell()

But it did not consistently fire for me. (Depending whether or not I had edited the file since the change, which in my case was external.)

There are some more tricks on the VimTips wiki which may help.

Add this to your ~/.vimrc file:

set autoread
nnoremap <C-u> :checktime<CR>

Now whenever you want vim to reload external changes, just click CTRL-U :)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!