Vim restores the contents of the swap file

Source: Internet
Author: User

There may be several situations when the exchange file is generated:

1. When you use vim to edit a file, an unexpected event occurs, causing the window to close or the system to crash and restart;

2. Another vim is editing the file;



The following specifically describes the first case:

Original file:

# cat a.txt
This is a test for vim.
line 1
line 2
line 3
Added new content to the file:

This is a test for vim.
line 1
line 2
line 3
### Add new lines:
Line 4
Line 5
Line 6
Before the time to save, suddenly the terminal is disconnected, resulting in the generation of .swp file

[[email protected] ~] # ll -a
total 316
drwx ------ 5 xielf xielf 4096 Jan 7 13:12.
drwxr-xr-x 7 root root 4096 Jan 14 2014 ..
-rw-r--r-- 1 root root 45 Jan 7 13:10 a.txt
-rw-r--r-- 1 root root 12288 Jan 7 13:11 .a.txt.swp
Check the content of the original file and find that it is the most original data, there is no content that we have modified

[[email protected] ~] # cat a.txt
This is a test for vim.
line 1
line 2
line 3
View the swp file:

[[email protected] ~] # vim -r .a.txt.swp # -r read swap
You will see the following prompt:
Using swap file ".a.txt.swp"
Original file "/root/a.txt"
E308: Warning: Original file may have been changed
Recovery completed. You should check if everything is OK.
(You might want to write out this file under another name
and run diff with the original file to check for changes)
Delete the .swp file afterwards.
Press ENTER or type command to continue
Press ENTER to continue:
This is a test for vim.
line 1
line 2
line 3
### Add new lines:
Line 4
Line 5
Line 6
The content in the swp file has the latest content of the last edit, then we have to restore it to a.txt, and exit first.

Open the a.txt source file:

[[email protected] ~] # vim a.txt
E325: ATTENTION
Found a swap file by the name ".a.txt.swp"
          owned by: root dated: Wed Jan 7 13:11:26 2015
         file name: ~ root / a.txt
          modified: YES
         user name: root host name: test
        process ID: 29908
While opening file "a.txt"
             dated: Wed Jan 7 13:10:00 2015
(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 a.txt"
    to recover the changes (see ": help recovery").
    If you did this already, delete the swap file ".a.txt.swp"
    to avoid this message.
Swap file ".a.txt.swp" already exists!
[O] pen Read-Only, (E) dit anyway, (R) ecover, (D) elete it, (Q) uit, (A) bort:
##
O Open as read-only
E Continue to edit, if the file is being edited by another vim, you will probably get two versions
R recover from swp file
D Delete the swp file. If you find that the content of the swp file is the same as the original file without any changes, you can use this operation.
Q exit
A is similar to exit, but it will also cancel the execution of subsequent commands, which is useful when loading a script to edit multiple files.
The above prompt shows: modified: YES, indicating that there is a change, we restore from the swp file, press shift + r, and then press Enter to restore the contents of the swp file.

Then save it, so that you can save the contents of the swp to the original file. After saving and exiting, remember to delete the swp file, otherwise you will be prompted next time.



Confirm the recovered files:

[[email protected] ~] # cat a.txt
This is a test for vim.
line 1
line 2
line 3
### Add new lines:
Line 4
Line 5
Line 6
OK, no problem, the modified file has been restored.

Imagine if we are configuring a certain configuration file and suddenly cause the vim editor to close abnormally, resulting in the generation of the swap file, then we have done a lot of changes in vain, so knowing how to restore the swp file is necessary Oh, avoid too much repetitive work.

This article is from the "bug" blog, please be sure to keep this source http://chongzi100.blog.51cto.com/340243/1600130

vim restore the contents of the swap file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.