Vim Tips Wiki
m (Avoid irritating "ATTENTION" message and always open it read-only moved to Open same file read-only in second Vim: Page moved by JohnBot to improve title)
(Change to TipImported template + severe manual clean)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=847
 
|id=847
  +
|previous=846
|title=Avoid irritating "ATTENTION" message and always open it read-only
 
  +
|next=848
|created=January 9, 2005 18:16
+
|created=January 9, 2005
 
|complexity=basic
 
|complexity=basic
 
|author=Jang Junyeong
 
|author=Jang Junyeong
 
|version=6.0
 
|version=6.0
 
|rating=23/8
 
|rating=23/8
 
}}
|text=
 
 
When you open a file opened in somewhere else, you would get "E325: ATTENTION" message because a swap file already exists there. I (almost) always choose the action "[O]pen Read-Only" in this case, so typing 'O' key is an annoying job for me. The following in my vimrc reduce my job.
   
  +
<pre>
When you open a file opened in somewhere else, you would get "E325: ATTENTION" message because a swap file already exists there. I (almost) always choose the action "[O]pen Read-Only" in this case, so typing 'O' key is an annoying job for me. The following in my vimrc reduce my job.
 
 
func CheckSwap()
 
swapname
 
if v:statusmsg =~ '\.sw[^p]$'
 
set ro
 
endif
 
endfunc
   
 
if &amp;swf
func CheckSwap()
 
swapname
+
set shm+=A
 
au BufReadPre * call CheckSwap()
if v:statusmsg =~ '\.sw[^p]$'
 
 
endif
set ro
 
  +
</pre>
endif
 
endfunc
 
 
if &amp;swf
 
set shm+=A
 
au BufReadPre * call CheckSwap()
 
endif
 
   
 
==Comments==
}}
 
 
Very neat trick. However, in a genuine case (when a previous session really crashed), the message is still usefull, and avoiding it could cause some confusion later. Also, you might want to recover the file immediately too. To address this issue, here is a modified attempt that echoes a message in this case. The message is hopefully easier to digest than the original ATTENTION dialog.
   
 
I improved it slighly more with a toggle option. If you want to delete the swapfile, it is easier to have Vim do it, so I added a ToggleSwapCheck() function. I usage is to toggle the behavior, reload the buffer to get the Vim dialog box where you can choose to delete the swap. You can later toggle it back on by calling the same function.
== Comments ==
 
Thanks for this super tip, I always hated the delay of 2 seconds
 
when opening the second RO file.
 
   
  +
<pre>
Super Man
 
 
let s:swapCheckEnabled = 0
, January 10, 2005 16:42
 
 
let s:_shm = &amp;shm
----
 
 
function! ToggleSwapCheck()
Very neat trick. However, in a genuine case (when a previous session really crashed), the message is still usefull, and avoidin it could cause some confusion later. Also, you might want to recover the file immediately too. To address this issue, here is a modified attempt that echoes a message in this case. The message is hopefully easier to digest than the original ATTENTION dialog.
 
 
let s:swapCheckEnabled = !s:swapCheckEnabled
 
if !s:swapCheckEnabled
 
let &amp;shm = s:_shm
 
endif
 
aug CheckSwap
 
au!
  +
if s:swapCheckEnabled
 
set shm+=A
 
au BufReadPre * call CheckSwapFile()
 
au BufRead * call WarnSwapFile()
 
endif
 
aug END
 
endfunction
 
call ToggleSwapCheck()
   
function! CheckSwap()
+
function! CheckSwapFile()
 
if !exists('*GetVimCmdOutput') || !&amp;swapfile || !s:swapCheckEnabled
swapname
 
 
return
if v:statusmsg =~ '\.sw[^p]$'
 
 
endif
set ro
 
let b:_warnSwap = 1
 
endif
 
endfunction
 
 
function! WarnIfSwap()
 
if exists('b:_warnSwap') &amp;&amp; b:_warnSwap
 
echohl ErrorMsg | echomsg "File: \"" . bufname('%') .
 
\ "\" is opened readonly, as a swapfile already existed."
 
\ | echohl NONE
 
unlet b:_warnSwap
 
endif
 
endfunction
 
 
if &amp;swf
 
set shm+=A
 
aug CheckSwap
 
au!
 
au BufReadPre * call CheckSwap()
 
au BufRead * call WarnIfSwap()
 
aug END
 
endif
 
 
hari_vim at yahoo dot com
 
, January 24, 2005 17:17
 
----
 
I improved it slighly more with a toggle option. If you want to delete the swapfile, it is easier to have vim do it, so I added a ToggleSwapCheck() function. I usage is to toggle the behavior, reload the buffer to get the Vim dialog box where you can choose to delete the swap. You can later toggle it back on by calling the same function.
 
   
 
let swapname = GetVimCmdOutput('swapname')
let s:swapCheckEnabled = 0
 
 
if swapname =~ '\.sw[^p]$'
let s:_shm = &amp;shm
 
 
set ro
function! ToggleSwapCheck()
 
let s:swapCheckEnabled = !s:swapCheckEnabled
+
let b:_warnSwap = 1
 
endif
if !s:swapCheckEnabled
 
 
endfunction
let &amp;shm = s:_shm
 
endif
 
aug CheckSwap
 
au!
 
if s:swapCheckEnabled
 
set shm+=A
 
au BufReadPre * call CheckSwapFile()
 
au BufRead * call WarnSwapFile()
 
endif
 
aug END
 
endfunction
 
call ToggleSwapCheck()
 
 
function! CheckSwapFile()
 
if !exists('*GetVimCmdOutput') || !&amp;swapfile || !s:swapCheckEnabled
 
return
 
endif
 
 
let swapname = GetVimCmdOutput('swapname')
 
if swapname =~ '\.sw[^p]$'
 
set ro
 
let b:_warnSwap = 1
 
endif
 
endfunction
 
 
function! WarnSwapFile()
 
if exists('b:_warnSwap') &amp;&amp; b:_warnSwap &amp;&amp; &amp;swapfile
 
echohl ErrorMsg | echomsg "File: \"" . bufname('%') .
 
\ "\" is opened readonly, as a swapfile already existed."
 
\ | echohl NONE
 
unlet b:_warnSwap
 
endif
 
endfunction
 
   
 
function! WarnSwapFile()
 
if exists('b:_warnSwap') &amp;&amp; b:_warnSwap &amp;&amp; &amp;swapfile
 
echohl ErrorMsg | echomsg "File: \"" . bufname('%') .
 
\ "\" is opened readonly, as a swapfile already existed."
 
\ | echohl NONE
 
unlet b:_warnSwap
 
endif
 
endfunction
  +
</pre>
   
hari_vim at yahoo dot com
 
, August 16, 2005 14:15
 
 
----
 
----
<!-- parsed by vimtips.py in 0.547775 seconds-->
 

Revision as of 01:00, 25 November 2007

Tip 847 Printable Monobook Previous Next

created January 9, 2005 · complexity basic · author Jang Junyeong · version 6.0


When you open a file opened in somewhere else, you would get "E325: ATTENTION" message because a swap file already exists there. I (almost) always choose the action "[O]pen Read-Only" in this case, so typing 'O' key is an annoying job for me. The following in my vimrc reduce my job.

func CheckSwap()
  swapname
  if v:statusmsg =~ '\.sw[^p]$'
    set ro
  endif
endfunc

if &swf
  set shm+=A
  au BufReadPre * call CheckSwap()
endif

Comments

Very neat trick. However, in a genuine case (when a previous session really crashed), the message is still usefull, and avoiding it could cause some confusion later. Also, you might want to recover the file immediately too. To address this issue, here is a modified attempt that echoes a message in this case. The message is hopefully easier to digest than the original ATTENTION dialog.

I improved it slighly more with a toggle option. If you want to delete the swapfile, it is easier to have Vim do it, so I added a ToggleSwapCheck() function. I usage is to toggle the behavior, reload the buffer to get the Vim dialog box where you can choose to delete the swap. You can later toggle it back on by calling the same function.

let s:swapCheckEnabled = 0
let s:_shm = &shm
function! ToggleSwapCheck()
  let s:swapCheckEnabled = !s:swapCheckEnabled
  if !s:swapCheckEnabled
    let &shm = s:_shm
  endif
  aug CheckSwap
    au!
    if s:swapCheckEnabled
      set shm+=A
      au BufReadPre * call CheckSwapFile()
      au BufRead * call WarnSwapFile()
    endif
  aug END
endfunction
call ToggleSwapCheck()

function! CheckSwapFile()
  if !exists('*GetVimCmdOutput') || !&swapfile || !s:swapCheckEnabled
    return
  endif

  let swapname = GetVimCmdOutput('swapname')
  if swapname =~ '\.sw[^p]$'
    set ro
    let b:_warnSwap = 1
  endif
endfunction

function! WarnSwapFile()
  if exists('b:_warnSwap') && b:_warnSwap && &swapfile
    echohl ErrorMsg | echomsg "File: \"" . bufname('%') .
     \ "\" is opened readonly, as a swapfile already existed."
     \ | echohl NONE
    unlet b:_warnSwap
  endif
endfunction