(Insert TipProposed template + minor manual clean) |
(Manual edit: Updated in line with vim_use discussion.) |
||
Line 11: | Line 11: | ||
|category2= |
|category2= |
||
}} |
}} |
||
+ | By default, when Vim notices the file for a buffer being edited has been |
||
− | {{dodgy|This tip has several problems that were fixed in the mentioned vim_use thread. These fixes need to be merged in before using this tip.}} |
||
+ | deleted (e.g. after running a shell command), it raises error E221 to warn the |
||
+ | user. An additional behaviour desired by some is to also mark the buffer as |
||
+ | 'modified' so that it can't be abandoned without providing a bang (!). This |
||
+ | isn't foolproof, as the file could still be deleted without Vim noticing, so |
||
+ | your best defense is to make sure files you want are not deleted; however |
||
+ | this can provide a second line of defense. |
||
+ | To use it, add the following to your [[vimrc]] or make a .vim file in your |
||
− | Recently there was some [http://groups.google.com/group/vim_use/browse_thread/thread/64785ff585431733/b2ff93351b09eee8 discussion on the vim_use mailing list] regarding Vim's behavior when it notices the buffer you are editing no longer exists. |
||
+ | $HOME/.vim/plugin or $HOME/vimfiles/plugin directory. |
||
− | |||
− | The default behavior is to notify the user by raising Error E221. The concern expressed in the mailing list discussion was that Vim ought to mark the buffer as modified to protect against inadvertently closing the buffer, and thus losing the file's contents forever. |
||
− | |||
− | While the first line of defense is to take care not to delete the same files you are currently editing, as a user you don't always have that luxury. Sometimes files are deleted for you by a process that is cleaning up after itself. In these cases it is helpful to have a second line of defense. |
||
− | |||
− | Add the following autocommand definition to your [[vimrc]]: |
||
<pre> |
<pre> |
||
− | + | au FileChangedShell * call FCSHandler("<afile>:p") |
|
+ | function FCSHandler(name) |
||
+ | let msg = 'File "'.a:name.'"' |
||
+ | let fcs_choice = '' |
||
+ | if fcs_reason == "deleted" |
||
+ | let msg .= " no longer available - 'modified' set" |
||
+ | call setbufvar(expand(a:name), '&modified', '1') |
||
+ | elseif fcs_reason == "time" |
||
+ | let msg .= " timestamp changed" |
||
+ | elseif fcs_reason == "mode" |
||
+ | let msg .= " permissions changed" |
||
+ | elseif fcs_reason == "changed" |
||
+ | let msg .= " contents changed" |
||
+ | let fcs_choice = "ask" |
||
+ | elseif fcs_reason == "conflict" |
||
+ | let msg .= " CONFLICT --" |
||
+ | let msg .= " is modified, but" |
||
+ | let msg .= " was changed outside Vim" |
||
+ | let fcs_choice = "ask" |
||
+ | echohl Error |
||
+ | else " unknown values (future Vim versions?) |
||
+ | let msg .= " FileChangedShell reason=" |
||
+ | let msg .= fcs_reason |
||
+ | let fcs_choice = "ask" |
||
+ | endif |
||
+ | redraw! |
||
+ | echomsg msg |
||
+ | echohl None |
||
+ | endfunction |
||
</pre> |
</pre> |
||
+ | (Based on |
||
− | After sourcing vimrc, any file that Vim detects as being deleted is marked as modified, requiring the user to first write it or supply a bang (!) to a command that would result in closing the buffer. |
||
+ | [http://groups.google.com/group/vim_use/browse_thread/thread/64785ff585431733/b2ff93351b09eee8 |
||
− | |||
+ | discussion on the vim_use mailing list].) |
||
− | ==Comments== |
||
− | {{todo}} |
||
− | There were some issues noted in the discussion linked above with this particular implementation. We must merge in the final result of that discussion to have a usable tip. |
Revision as of 05:59, 9 June 2008
Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).
By default, when Vim notices the file for a buffer being edited has been deleted (e.g. after running a shell command), it raises error E221 to warn the user. An additional behaviour desired by some is to also mark the buffer as 'modified' so that it can't be abandoned without providing a bang (!). This isn't foolproof, as the file could still be deleted without Vim noticing, so your best defense is to make sure files you want are not deleted; however this can provide a second line of defense.
To use it, add the following to your vimrc or make a .vim file in your $HOME/.vim/plugin or $HOME/vimfiles/plugin directory.
au FileChangedShell * call FCSHandler("<afile>:p") function FCSHandler(name) let msg = 'File "'.a:name.'"' let fcs_choice = '' if fcs_reason == "deleted" let msg .= " no longer available - 'modified' set" call setbufvar(expand(a:name), '&modified', '1') elseif fcs_reason == "time" let msg .= " timestamp changed" elseif fcs_reason == "mode" let msg .= " permissions changed" elseif fcs_reason == "changed" let msg .= " contents changed" let fcs_choice = "ask" elseif fcs_reason == "conflict" let msg .= " CONFLICT --" let msg .= " is modified, but" let msg .= " was changed outside Vim" let fcs_choice = "ask" echohl Error else " unknown values (future Vim versions?) let msg .= " FileChangedShell reason=" let msg .= fcs_reason let fcs_choice = "ask" endif redraw! echomsg msg echohl None endfunction
(Based on [http://groups.google.com/group/vim_use/browse_thread/thread/64785ff585431733/b2ff93351b09eee8 discussion on the vim_use mailing list].)