Vim Tips Wiki
(Manual edit: Bugfixes: fcs_{reason,choice} need v: and <afile> needs expand().)
(code formatting)
Tags: Visual edit apiedit
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
  +
{{TipNew
{{TipProposed
 
|id=0
+
|id=1568
|previous=0
+
|previous=1567
|next=0
+
|next=1569
|created=June 3, 2008
+
|created=2008
 
|complexity=basic
 
|complexity=basic
 
|author=Ewfalor
 
|author=Ewfalor
 
|version=7.0
 
|version=7.0
 
|subpage=/200806
 
|subpage=/200806
|category1=
+
|category1=File Handling
 
|category2=
 
|category2=
 
}}
 
}}
  +
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.
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
+
To use it, add the following to your [[vimrc]], or create file <code>~/.vim/plugin/filechanged.vim</code> (Unix) or <code>$HOME/vimfiles/plugin/filechanged.vim</code> (Windows) containing the script below, then restart Vim.
  +
<source lang="vim">
$HOME/.vim/plugin or $HOME/vimfiles/plugin directory.
 
 
<pre>
 
 
au FileChangedShell * call FCSHandler(expand("<afile>:p"))
 
au FileChangedShell * call FCSHandler(expand("<afile>:p"))
 
function FCSHandler(name)
 
function FCSHandler(name)
let msg = 'File "'.a:name.'"'
+
let msg = 'File "'.a:name.'"'
let v:fcs_choice = ''
+
let v:fcs_choice = ''
if v:fcs_reason == "deleted"
+
if v:fcs_reason == "deleted"
let msg .= " no longer available - 'modified' set"
+
let msg .= " no longer available - 'modified' set"
call setbufvar(expand(a:name), '&modified', '1')
+
call setbufvar(expand(a:name), '&modified', '1')
 
echohl WarningMsg
elseif v:fcs_reason == "time"
+
elseif v:fcs_reason == "time"
let msg .= " timestamp changed"
+
let msg .= " timestamp changed"
elseif v:fcs_reason == "mode"
+
elseif v:fcs_reason == "mode"
let msg .= " permissions changed"
 
elseif v:fcs_reason == "changed"
+
let msg .= " permissions changed"
let msg .= " contents changed"
+
elseif v:fcs_reason == "changed"
let v:fcs_choice = "ask"
+
let msg .= " contents changed"
elseif v:fcs_reason == "conflict"
+
let v:fcs_choice = "ask"
  +
elseif v:fcs_reason == "conflict"
let msg .= " CONFLICT --"
 
let msg .= " is modified, but"
+
let msg .= " CONFLICT --"
let msg .= " was changed outside Vim"
+
let msg .= " is modified, but"
let v:fcs_choice = "ask"
+
let msg .= " was changed outside Vim"
echohl Error
+
let v:fcs_choice = "ask"
  +
echohl ErrorMsg
else " unknown values (future Vim versions?)
+
else " unknown values (future Vim versions?)
let msg .= " FileChangedShell reason="
 
let msg .= v:fcs_reason
+
let msg .= " FileChangedShell reason="
let v:fcs_choice = "ask"
+
let msg .= v:fcs_reason
  +
let v:fcs_choice = "ask"
endif
 
  +
echohl ErrorMsg
redraw!
 
 
endif
echomsg msg
 
 
redraw!
echohl None
 
 
echomsg msg
  +
echohl None
 
endfunction
 
endfunction
</pre>
+
</source>
  +
  +
You can easily customize this code to take whatever action you desire for the various values of <code>v:fcs_reason</code>, by setting <code>v:fcs_choice</code> (for example, you could reload the buffer if the mode has changed).
  +
 
Based on [http://groups.google.com/group/vim_use/browse_thread/thread/64785ff585431733/b2ff93351b09eee8 discussion on the vim_use mailing list].
  +
  +
==References==
  +
Basis of this tip:
  +
*{{help|FileChangedShell}}
  +
*{{help|v:fcs_reason}}
  +
*{{help|v:fcs_choice}}
  +
  +
Used in this tip:
  +
*{{help|setbufvar()}}
  +
*{{help|:echohl}}
  +
*{{help|:echomsg}}
  +
  +
==See also==
  +
*[[Suppressing file changed warnings in a specific buffer]]
  +
*[[Setting file attributes without reloading a buffer]]
   
  +
==Comments==
(Based on [http://groups.google.com/group/vim_use/browse_thread/thread/64785ff585431733/b2ff93351b09eee8 discussion on the vim_use mailing list].)
 

Latest revision as of 13:40, 16 June 2015

Tip 1568 Printable Monobook Previous Next

created 2008 · complexity basic · author Ewfalor · version 7.0


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 create file ~/.vim/plugin/filechanged.vim (Unix) or $HOME/vimfiles/plugin/filechanged.vim (Windows) containing the script below, then restart Vim.

au FileChangedShell * call FCSHandler(expand("<afile>:p"))
function FCSHandler(name)
  let msg = 'File "'.a:name.'"'
  let v:fcs_choice = ''
  if v:fcs_reason == "deleted"
    let msg .= " no longer available - 'modified' set"
    call setbufvar(expand(a:name), '&modified', '1')
    echohl WarningMsg
  elseif v:fcs_reason == "time"
    let msg .= " timestamp changed"
  elseif v:fcs_reason == "mode"
    let msg .= " permissions changed"
  elseif v:fcs_reason == "changed"
    let msg .= " contents changed"
    let v:fcs_choice = "ask"
  elseif v:fcs_reason == "conflict"
    let msg .= " CONFLICT --"
    let msg .= " is modified, but"
    let msg .= " was changed outside Vim"
    let v:fcs_choice = "ask"
    echohl ErrorMsg
  else  " unknown values (future Vim versions?)
    let msg .= " FileChangedShell reason="
    let msg .= v:fcs_reason
    let v:fcs_choice = "ask"
    echohl ErrorMsg
  endif
  redraw!
  echomsg msg
  echohl None
endfunction

You can easily customize this code to take whatever action you desire for the various values of v:fcs_reason, by setting v:fcs_choice (for example, you could reload the buffer if the mode has changed).

Based on discussion on the vim_use mailing list.

References[]

Basis of this tip:

Used in this tip:

See also[]

Comments[]