Change to TipImported template + severe manual clean
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) |
||
{{review}}
{{TipImported
|id=847
|previous=846
|next=848
|created=January 9, 2005
|complexity=basic
|author=Jang Junyeong
|version=6.0
|rating=23/8
}}▼
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()
set
▲ if v:statusmsg =~ '\.sw[^p]$'
▲ set ro
</pre>
▲ endif
▲ endfunc
▲ if &swf
set shm+=A ▼
▲ au BufReadPre * call CheckSwap()
▲ endif
▲}}
Very neat trick. However, in a genuine case (when a previous session really crashed), the message is still usefull, and
I improved it slighly more with a toggle option. If you want to delete the swapfile, it is easier to have
▲== Comments ==
<pre>
▲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.
if s:swapCheckEnabled
▲ swapname
if v:statusmsg =~ '\.sw[^p]$' ▼
set ro ▼
let b:_warnSwap = 1 ▼
▲ endif
▲ endfunction
function! WarnIfSwap() ▼
if exists('b:_warnSwap') && b:_warnSwap ▼
echohl ErrorMsg | echomsg "File: \"" . bufname('%') . ▼
\ "\" is opened readonly, as a swapfile already existed." ▼
\ | echohl NONE ▼
▲ endif
endfunction ▼
▲ aug CheckSwap
▲ au!
▲ aug END
▲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
▲ if !s:swapCheckEnabled
▲ let &shm = s:_shm
▲ endif
▲ if s:swapCheckEnabled
▲ au BufReadPre * call CheckSwapFile()
▲ au BufRead * call WarnSwapFile()
▲ endif
endfunction ▼
▲ call ToggleSwapCheck()
▲ if !exists('*GetVimCmdOutput') || !&swapfile || !s:swapCheckEnabled
▲ return
endif ▼
▲ let swapname = GetVimCmdOutput('swapname')
</pre>
----
|