Quick links: help overview · quick reference · user manual toc · reference manual toc · faq
Go to keyword (shortcut: k)
Site search (shortcut: s)
usr_07.txt  	For Vim version 9.1.  Last change: 2025 Nov 09


		     VIM USER MANUAL	by Bram Moolenaar


			  Editing more than one file


No matter how many files you have, you can edit them without leaving Vim.
Define a list of files to work on and jump from one to the other.  Copy text
from one file and put it in another one.

07.1  	Edit another file
07.2  	A list of files
07.3  	Jumping from file to file
07.4  	Backup files
07.5  	Copy text between files
07.6  	Viewing a file
07.7  	Changing the file name

     Next chapter: usr_08.txt  Splitting windows
 Previous chapter: usr_06.txt  Using syntax highlighting
Table of contents: usr_toc.txt

==============================================================================
07.1  	Edit another file

So far you had to start Vim for every file you wanted to edit.  There is a
simpler way.  To start editing another file, use this command: 

	:edit foo.txt

You can use any file name instead of "foo.txt".  Vim will close the current
file and open the new one.  If the current file has unsaved changes, however,
Vim displays an error message and does not open the new file:

	E37: No write since last change (use ! to override) 

	Note:
	Vim puts an error ID at the start of each error message.  If you do
	not understand the message or what caused it, look in the help system
	for this ID.  In this case: 

		:help E37

At this point, you have a number of alternatives.  You can write the file
using this command: 

	:write

Or you can force Vim to discard your changes and edit the new file, using the
force (!) character: 

	:edit! foo.txt

If you want to edit another file, but not write the changes in the current
file yet, you can make it hidden: 

	:hide edit foo.txt

The text with changes is still there, but you can't see it.  This is further
explained in section 22.4: The buffer list.

==============================================================================
07.2  	A list of files

You can start Vim to edit a sequence of files.  For example: 

	vim one.c two.c three.c

This command starts Vim and tells it that you will be editing three files.
Vim displays just the first file.  After you have done your thing in this
file, to edit the next file you use this command: 

	:next

If you have unsaved changes in the current file, you will get an error
message and the ":next" will not work.  This is the same problem as with
":edit" mentioned in the previous section.  To abandon the changes: 

	:next!

But mostly you want to save the changes and move on to the next file.  There
is a special command for this: 

	:wnext

This does the same as using two separate commands: 

	:write
	:next


WHERE AM I?

To see which file in the argument list you are editing, look in the window
title.  It should show something like "(2 of 3)".  This means you are editing
the second file out of three files.
   If you want to see the list of files, use this command: 

	:args

This is short for "arguments".  The output might look like this:

	one.c [two.c] three.c 

These are the files you started Vim with.  The one you are currently editing,
"two.c", is in square brackets.


MOVING TO OTHER ARGUMENTS

To go back one file: 

	:previous

This is just like the ":next" command, except that it moves in the other
direction.  Again, there is a shortcut command for when you want to write the
file first: 

	:wprevious

To move to the very last file in the list: 

	:last

And to move back to the first one again: 

	:first

There is no ":wlast" or ":wfirst" command though!

You can use a count for ":next" and ":previous".  To skip two files forward: 

	:2next


AUTOMATIC WRITING

When moving around the files and making changes, you have to remember to use
":write".  Otherwise you will get an error message.  If you are sure you
always want to write modified files, you can tell Vim to automatically write
them: 

	:set autowrite

When you are editing a file which you may not want to write, switch it off
again: 

	:set noautowrite


EDITING ANOTHER LIST OF FILES

You can redefine the list of files without the need to exit Vim and start it
again.  Use this command to edit three other files: 

	:args five.c six.c seven.h

Or use a wildcard, like it's used in the shell: 

	:args *.txt

Vim will take you to the first file in the list.  Again, if the current file
has changes, you can either write the file first, or use ":args!" (with !
added) to abandon the changes.


DID YOU EDIT THE LAST FILE?
							arglist-quit
When you use a list of files, Vim assumes you want to edit them all.  To
protect you from exiting too early, you will get this error when you didn't
edit the last file in the list yet:

	E173: 46 more files to edit 

If you really want to exit, just do it again.  Then it will work (but not when
you did other commands in between).

==============================================================================
07.3  	Jumping from file to file

To quickly jump between two files, press CTRL-^ (on English-US keyboards the ^
is above the 6 key).  Example: 

	:args one.c two.c three.c

You are now in one.c. 

	:next

Now you are in two.c.  Now use CTRL-^ to go back to one.c.  Another CTRL-^ and
you are back in two.c.  Another CTRL-^ and you are in one.c again.  If you now
do: 

	:next

You are in three.c.  Notice that the CTRL-^ command does not change the idea
of where you are in the list of files.  Only commands like ":next" and
":previous" do that.

The file you were previously editing is called the "alternate" file.  When you
just started Vim CTRL-^ will not work, since there isn't a previous file.


PREDEFINED MARKS

After jumping to another file, you can use two predefined marks which are very
useful: 

	`"

This takes you to the position where the cursor was when you left the file.
Another mark that is remembered is the position where you made the last
change: 

	`.

Suppose you are editing the file "one.txt".  Somewhere halfway through the
file you use "x" to delete a character.  Then you go to the last line with "G"
and write the file with ":w".  You edit several other files, and then use
":edit one.txt" to come back to "one.txt".  If you now use `" Vim jumps to the
last line of the file.  Using `. takes you to the position where you deleted
the character.  Even when you move around in the file `" and `. will take you
to the remembered position.  At least until you make another change or leave
the file.


FILE MARKS

In section 03.10 was explained how you can place a mark in a file with "mx"
and jump to that position with "`x".  That works within one file.  If you edit
another file and place marks there, these are specific for that file.  Thus
each file has its own set of marks, they are local to the file.
   So far we were using marks with a lowercase letter.  There are also marks
with an