Email Preview

Knowledge Pill

07.4  Backup files

Usually Vim does not produce a backup file. If you want to have one, all you need to do is execute the following command:
:set backup

The name of the backup file is the original file with a  ~  added to the end. If your file is named data.txt, for example, the backup file name is data.txt~.

If you do not like the fact that the backup files end with ~, you can change the extension:

:set backupext=.bak

This will use data.txt.bak instead of data.txt~.

Another option that matters here is ‘backupdir’. It specifies where the backup file is written. The default, to write the backup in the same directory as the original file, will mostly be the right thing.

Note: When the ‘backup’ option isn’t set but the ‘writebackup’ is, Vim will still create a backup file. However, it is deleted as soon as writing the file was completed successfully. This functions as a safety against losing your original file when writing fails in some way (disk full is the most common cause; being hit by lightning might be another, although less common).


If you are editing source files, you might want to keep the file before you make any changes. But the backup file will be overwritten each time you write the file. Thus it only contains the previous version, not the first one.

To make Vim keep the original file, set the ‘patchmode’ option. This specifies the extension used for the first backup of a changed file. Usually you would do this:

:set patchmode=.orig

When you now edit the file data.txt for the first time, make changes and write the file, Vim will keep a copy of the unchanged file under the name “data.txt.orig”.

If you make further changes to the file, Vim will notice that “data.txt.orig” already exists and leave it alone. Further backup files will then be called “data.txt~” (or whatever you specified with ‘backupext’).

If you leave ‘patchmode’ empty (that is the default), the original file will not be kept.


07.5  Copy text between files

This explains how to copy text from one file to another. Let’s start with a simple example. Edit the file that contains the text you want to copy. Move the cursor to the start of the text and press "v“. This starts Visual mode. Now move the cursor to the end of the text and press ”y". This yanks (copies) the selected text.

Move the cursor to the character where you want the text to appear after. Use "p" to put the text there.

Of course you can use many other commands to yank the text. For example, to select whole lines start Visual mode with "V“. Or use CTRL-V to select a rectangular block. Or use ”Y" to yank a single line, "yaw" to yank-a-word, etc.

The "p" command puts the text after the cursor. Use "P" to put the text before the cursor.

Notice that Vim remembers if you yanked a whole line or a block, and puts it back that way.


When you want to copy several pieces of text from one file to another, having to switch between the files and writing the target file takes a lot of time. To avoid this, copy each piece of text to its own register.

A register is a place where Vim stores text. Here we will use the registers named a to z (later you will find out there are others). Let’s copy a sentence to the f register (f for First):


The "yas" command yanks a sentence like before. It’s the "f that tells Vim the text should be place in the f register. This must come just before the yank command.

Now yank three whole lines to the l register (l for line):


The count could be before the "l just as well. To yank a block of text to the b (for block) register:

Notice that the register specification "b is just before the "y" command. This is required. If you would have put it before the "w" command, it would not have worked.

Now you have three pieces of text in the f, l and b registers. Edit another file, move around and place the text where you want it:


Again, the register specification “f comes before the ”p" command.

You can put the registers in any order. And the text stays in the register until you yank something else into it. Thus you can put it as many times as you like.

When you delete text, you can also specify a register. Use this to move several pieces of text around. For example, to delete-a-word and write it in the w register:


Again, the register specification comes before the delete command "d".


When collecting lines of text into one file, you can use this command:

:write >> logfile

This will write the text of the current file to the end of “logfile”. Thus it is appended. This avoids that you have to copy the lines, edit the log file and put them there. Thus you save two steps. But you can only append to the end of a file.

To append only a few lines, select them in Visual mode before typing ":write". In chapter 10 you will learn other ways to select a range of lines.

07.6  Viewing a file

Sometimes you only want to see what a file contains, without the intention to ever write it back. There is the risk that you type ":w" without thinking and overwrite the original file anyway. To avoid this, edit the file read-only.

To start Vim in readonly mode, use this command:

vim -R file

On Unix this command should do the same thing:

view file

You are now editing “file” in read-only mode. When you try using ":w" you will get an error message and the file won’t be written.

When you try to make a change to the file Vim will give you a warning:

W10: Warning: Changing a readonly file

The change will be done though. This allows for formatting the file, for example, to be able to read it easily.

If you make changes to a file and forgot that it was read-only, you can still write it. Add the ! to the write command to force writing.

If you really want to forbid making changes in a file, do this:

vim -M file

Now every attempt to change the text will fail. The help files are like this, for example. If you try to make a change you get this error message:
E21: Cannot make changes, ‘modifiable’ is off

You could use the -M argument to setup Vim to work in a viewer mode. This is only voluntary though, since these commands will remove the protection:

:set modifiable
:set write

07.7  Changing the file name

A clever way to start editing a new file is by using an existing file that contains most of what you need. For example, you start writing a new program to move a file. You know that you already have a program that copies a file, thus you start with:
:edit copy.c

You can delete the stuff you don’t need. Now you need to save the file under a new name. The ":saveas" command can be used for this:
:saveas move.c

Vim will write the file under the given name, and edit that file. Thus the next time you do ":write“, it will write ”move.c“. ”copy.c" remains unmodified.

When you want to change the name of the file you are editing, but don’t want to write the file, you can use this command:

:file move.c

Vim will mark the file as “not edited”. This means that Vim knows this is not the file you started editing. When you try to write the file, you might get this message:
E13: File exists (use ! to override)

This protects you from accidentally overwriting another file.


  1. Is backup files alternatives to swap files?
  2. Write the truth table for the combinations of options ‘backup’ and ‘writebackup’.
    Fill the action statement (XXXX):

    ‘backup’ ‘writebackup’  action
    off       off           XXXX
    off       on            XXXX
    on        off           XXXX
    on        on                              XXXX
  3. Can you make changes to files open in viewer mode (-M) if really needed?
  4. When you change the name of the current file, with the command :file NEWNAME, does VIM delete the old one?
Have Fun :)

Answers (*SPOILER*)

  1. No, even with or without backup files, swap files still exist and let you recover contents of files from crash using the command :recover.

  2. ‘backup’ ‘writebackup’  action
    off    off    no backup made

    off    on    backup current file, deleted afterwards (default)

    on    off    delete old backup, backup current file

    on    on    delete old backup, backup current file

  3. Yes, you can. Change deliberatly the options ’modifiable‘ and ’write’.
  4. No, the old file still exist after write and exit.


It is possible to make a secure donation by PayPal.Me. Your help keep this site alive and half of the donation will be devolved to help children in Uganda (ICCF Holland). Thanks for your support and have a nice day! :)