user526206 user526206 - 1 year ago 89
Linux Question

How to close file which open from vi editor

I open a file using vi editor on linux to make some change and somehow i unable to close file properly i mean exit a vi editor. But now when i try to edit it again it showing me a following message on console:

Found a swap file by the name "/etc/asterisk/.sip.conf.swp"
owned by: root dated: Fri Oct 21 16:54:43 2016
file name: /etc/asterisk/sip.conf
modified: YES
user name: root host name: sfj-asterisk1
process ID: 4900
While opening file "/etc/asterisk/sip.conf"
dated: Fri Oct 21 17:01:28 2016
NEWER than swap file!

(1) Another program may be editing the same file. If this is the case,
be careful not to end up with two different instances of the same
file when making changes. Quit, or continue with caution.
(2) An edit session for this file crashed.
If this is the case, use ":recover" or "vim -r /etc/asterisk/sip.conf"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file "/etc/asterisk/.sip.conf.swp"
to avoid this message.

Swap file "/etc/asterisk/.sip.conf.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:

Can someone tell me how to close a already open file. or remove this message

Answer Source

When vi opens a file, it creates a .swp file. In addition to other purposes, this file serves as something like a write lock. When you try to open a second vi instance to edit the file, you get the message you pasted. Typically when you close the file from vi, the .swp file gets deleted and you can now open it from another vi, but if vi crashes this will not be the case. You will have to manually delete the .swp file.

My suggestion is - make sure no other vi process has opened the file(I would start with doing ps aux| grep vi) and if there isn't delete the file. Alternatively you could choose one of the other options that vi already suggests.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download