1*usr_07.txt*	For Vim version 7.3.  Last change: 2006 Apr 24
2
3		     VIM USER MANUAL - by Bram Moolenaar
4
5			  Editing more than one file
6
7
8No matter how many files you have, you can edit them without leaving Vim.
9Define a list of files to work on and jump from one to the other.  Copy text
10from one file and put it in another one.
11
12|07.1|	Edit another file
13|07.2|	A list of files
14|07.3|	Jumping from file to file
15|07.4|	Backup files
16|07.5|	Copy text between files
17|07.6|	Viewing a file
18|07.7|	Changing the file name
19
20     Next chapter: |usr_08.txt|  Splitting windows
21 Previous chapter: |usr_06.txt|  Using syntax highlighting
22Table of contents: |usr_toc.txt|
23
24==============================================================================
25*07.1*	Edit another file
26
27So far you had to start Vim for every file you wanted to edit.  There is a
28simpler way.  To start editing another file, use this command: >
29
30	:edit foo.txt
31
32You can use any file name instead of "foo.txt".  Vim will close the current
33file and open the new one.  If the current file has unsaved changes, however,
34Vim displays an error message and does not open the new file:
35
36	E37: No write since last change (use ! to override) ~
37
38	Note:
39	Vim puts an error ID at the start of each error message.  If you do
40	not understand the message or what caused it, look in the help system
41	for this ID.  In this case: >
42
43		:help E37
44
45At this point, you have a number of alternatives.  You can write the file
46using this command: >
47
48	:write
49
50Or you can force Vim to discard your changes and edit the new file, using the
51force (!) character: >
52
53	:edit! foo.txt
54
55If you want to edit another file, but not write the changes in the current
56file yet, you can make it hidden: >
57
58	:hide edit foo.txt
59
60The text with changes is still there, but you can't see it.  This is further
61explained in section |22.4|: The buffer list.
62
63==============================================================================
64*07.2*	A list of files
65
66You can start Vim to edit a sequence of files.  For example: >
67
68	vim one.c two.c three.c
69
70This command starts Vim and tells it that you will be editing three files.
71Vim displays just the first file.  After you have done your thing in this
72file, to edit the next file you use this command: >
73
74	:next
75
76If you have unsaved changes in the current file, you will get an error
77message and the ":next" will not work.  This is the same problem as with
78":edit" mentioned in the previous section.  To abandon the changes: >
79
80	:next!
81
82But mostly you want to save the changes and move on to the next file.  There
83is a special command for this: >
84
85	:wnext
86
87This does the same as using two separate commands: >
88
89	:write
90	:next
91
92
93WHERE AM I?
94
95To see which file in the argument list you are editing, look in the window
96title.  It should show something like "(2 of 3)".  This means you are editing
97the second file out of three files.
98   If you want to see the list of files, use this command: >
99
100	:args
101
102This is short for "arguments".  The output might look like this:
103
104	one.c [two.c] three.c ~
105
106These are the files you started Vim with.  The one you are currently editing,
107"two.c", is in square brackets.
108
109
110MOVING TO OTHER ARGUMENTS
111
112To go back one file: >
113
114	:previous
115
116This is just like the ":next" command, except that it moves in the other
117direction.  Again, there is a shortcut command for when you want to write the
118file first: >
119
120	:wprevious
121
122To move to the very last file in the list: >
123
124	:last
125
126And to move back to the first one again: >
127
128	:first
129
130There is no ":wlast" or ":wfirst" command though!
131
132You can use a count for ":next" and ":previous".  To skip two files forward: >
133
134	:2next
135
136
137AUTOMATIC WRITING
138
139When moving around the files and making changes, you have to remember to use
140":write".  Otherwise you will get an error message.  If you are sure you
141always want to write modified files, you can tell Vim to automatically write
142them: >
143
144	:set autowrite
145
146When you are editing a file which you may not want to write, switch it off
147again: >
148
149	:set noautowrite
150
151
152EDITING ANOTHER LIST OF FILES
153
154You can redefine the list of files without the need to exit Vim and start it
155again.  Use this command to edit three other files: >
156
157	:args five.c six.c seven.h
158
159Or use a wildcard, like it's used in the shell: >
160
161	:args *.txt
162
163Vim will take you to the first file in the list.  Again, if the current file
164has changes, you can either write the file first, or use ":args!" (with !
165added) to abandon the changes.
166
167
168DID YOU EDIT THE LAST FILE?
169							*arglist-quit*
170When you use a list of files, Vim assumes you want to edit them all.  To
171protect you from exiting too early, you will get this error when you didn't
172edit the last file in the list yet:
173
174	E173: 46 more files to edit ~
175
176If you really want to exit, just do it again.  Then it will work (but not when
177you did other commands in between).
178
179==============================================================================
180*07.3*	Jumping from file to file
181
182To quickly jump between two files, press CTRL-^ (on English-US keyboards the ^
183is above the 6 key).  Example: >
184
185	:args one.c two.c three.c
186
187You are now in one.c. >
188
189	:next
190
191Now you are in two.c.  Now use CTRL-^ to go back to one.c.  Another CTRL-^ and
192you are back in two.c.  Another CTRL-^ and you are in one.c again.  If you now
193do: >
194
195	:next
196
197You are in three.c.  Notice that the CTRL-^ command does not change the idea
198of where you are in the list of files.  Only commands like ":next" and
199":previous" do that.
200
201The file you were previously editing is called the "alternate" file.  When you
202just started Vim CTRL-^ will not work, since there isn't a previous file.
203
204
205PREDEFINED MARKS
206
207After jumping to another file, you can use two predefined marks which are very
208useful: >
209
210	`"
211
212This takes you to the position where the cursor was when you left the file.
213Another mark that is remembered is the position where you made the last
214change: >
215
216	`.
217
218Suppose you are editing the file "one.txt".  Somewhere halfway the file you
219use "x" to delete a character.  Then you go to the last line with "G" and
220write the file with ":w".  You edit several other files, and then use ":edit
221one.txt" to come back to "one.txt".  If you now use `" Vim jumps to the last
222line of the file.  Using `. takes you to the position where you deleted the
223character.  Even when you move around in the file `" and `. will take you to
224the remembered position.  At least until you make another change or leave the
225file.
226
227
228FILE MARKS
229
230In chapter 4 was explained how you can place a mark in a file with "mx" and
231jump to that position with "`x".  That works within one file.  If you edit
232another file and place marks there, these are specific for that file.  Thus
233each file has its own set of marks, they are local to the file.
234   So far we were using marks with a lowercase letter.  There are also marks
235with an uppercase letter.  These are global, they can be used from any file.
236For example suppose that we are editing the file "foo.txt".  Go to halfway the
237file ("50%") and place the F mark there (F for foo): >
238
239	50%mF
240
241Now edit the file "bar.txt" and place the B mark (B for bar) at its last line:
242>
243	GmB
244
245Now you can use the "'F" command to jump back to halfway foo.txt.  Or edit yet
246another file, type "'B" and you are at the end of bar.txt again.
247
248The file marks are remembered until they are placed somewhere else.  Thus you
249can place the mark, do hours of editing and still be able to jump back to that
250mark.
251   It's often useful to think of a simple connection between the mark letter
252and where it is placed.  For example, use the H mark in a header file, M in
253a Makefile and C in a C code file.
254
255To see where a specific mark is, give an argument to the ":marks" command: >
256
257	:marks M
258
259You can also give several arguments: >
260
261	:marks MCP
262
263Don't forget that you can use CTRL-O and CTRL-I to jump to older and newer
264positions without placing marks there.
265
266==============================================================================
267*07.4*	Backup files
268
269Usually Vim does not produce a backup file.  If you want to have one, all you
270need to do is execute the following command: >
271
272	:set backup
273
274The name of the backup file is the original file with a  ~  added to the end.
275If your file is named data.txt, for example, the backup file name is
276data.txt~.
277   If you do not like the fact that the backup files end with ~, you can
278change the extension: >
279
280	:set backupext=.bak
281
282This will use data.txt.bak instead of data.txt~.
283   Another option that matters here is 'backupdir'.  It specifies where the
284backup file is written.  The default, to write the backup in the same
285directory as the original file, will mostly be the right thing.
286
287	Note:
288	When the 'backup' option isn't set but the 'writebackup' is, Vim will
289	still create a backup file.  However, it is deleted as soon as writing
290	the file was completed successfully.  This functions as a safety
291	against losing your original file when writing fails in some way (disk
292	full is the most common cause; being hit by lightning might be
293	another, although less common).
294
295
296KEEPING THE ORIGINAL FILE
297
298If you are editing source files, you might want to keep the file before you
299make any changes.  But the backup file will be overwritten each time you write
300the file.  Thus it only contains the previous version, not the first one.
301   To make Vim keep the original file, set the 'patchmode' option.  This
302specifies the extension used for the first backup of a changed file.  Usually
303you would do this: >
304
305	:set patchmode=.orig
306
307When you now edit the file data.txt for the first time, make changes and write
308the file, Vim will keep a copy of the unchanged file under the name
309"data.txt.orig".
310   If you make further changes to the file, Vim will notice that
311"data.txt.orig" already exists and leave it alone.  Further backup files will
312then be called "data.txt~" (or whatever you specified with 'backupext').
313   If you leave 'patchmode' empty (that is the default), the original file
314will not be kept.
315
316==============================================================================
317*07.5*	Copy text between files
318
319This explains how to copy text from one file to another.  Let's start with a
320simple example.  Edit the file that contains the text you want to copy.  Move
321the cursor to the start of the text and press "v".  This starts Visual mode.
322Now move the cursor to the end of the text and press "y".  This yanks (copies)
323the selected text.
324   To copy the above paragraph, you would do: >
325
326	:edit thisfile
327	/This
328	vjjjj$y
329
330Now edit the file you want to put the text in.  Move the cursor to the
331character where you want the text to appear after.  Use "p" to put the text
332there. >
333	:edit otherfile
334	/There
335	p
336
337Of course you can use many other commands to yank the text.  For example, to
338select whole lines start Visual mode with "V".  Or use CTRL-V to select a
339rectangular block.  Or use "Y" to yank a single line, "yaw" to yank-a-word,
340etc.
341   The "p" command puts the text after the cursor.  Use "P" to put the text
342before the cursor.  Notice that Vim remembers if you yanked a whole line or a
343block, and puts it back that way.
344
345
346USING REGISTERS
347
348When you want to copy several pieces of text from one file to another, having
349to switch between the files and writing the target file takes a lot of time.
350To avoid this, copy each piece of text to its own register.
351   A register is a place where Vim stores text.  Here we will use the
352registers named a to z (later you will find out there are others).  Let's copy
353a sentence to the f register (f for First): >
354
355	"fyas
356
357The "yas" command yanks a sentence like before.  It's the "f that tells Vim
358the text should be place in the f register.  This must come just before the
359yank command.
360   Now yank three whole lines to the l register (l for line): >
361
362	"l3Y
363
364The count could be before the "l just as well.  To yank a block of text to the
365b (for block) register: >
366
367	CTRL-Vjjww"by
368
369Notice that the register specification "b is just before the "y" command.
370This is required.  If you would have put it before the "w" command, it would
371not have worked.
372   Now you have three pieces of text in the f, l and b registers.  Edit
373another file, move around and place the text where you want it: >
374
375	"fp
376
377Again, the register specification "f comes before the "p" command.
378   You can put the registers in any order.  And the text stays in the register
379until you yank something else into it.  Thus you can put it as many times as
380you like.
381
382When you delete text, you can also specify a register.  Use this to move
383several pieces of text around.  For example, to delete-a-word and write it in
384the w register: >
385
386	"wdaw
387
388Again, the register specification comes before the delete command "d".
389
390
391APPENDING TO A FILE
392
393When collecting lines of text into one file, you can use this command: >
394
395	:write >> logfile
396
397This will write the text of the current file to the end of "logfile".  Thus it
398is appended.  This avoids that you have to copy the lines, edit the log file
399and put them there.  Thus you save two steps.  But you can only append to the
400end of a file.
401   To append only a few lines, select them in Visual mode before typing
402":write".  In chapter 10 you will learn other ways to select a range of lines.
403
404==============================================================================
405*07.6*	Viewing a file
406
407Sometimes you only want to see what a file contains, without the intention to
408ever write it back.  There is the risk that you type ":w" without thinking and
409overwrite the original file anyway.  To avoid this, edit the file read-only.
410   To start Vim in readonly mode, use this command: >
411
412	vim -R file
413
414On Unix this command should do the same thing: >
415
416	view file
417
418You are now editing "file" in read-only mode.  When you try using ":w" you
419will get an error message and the file won't be written.
420   When you try to make a change to the file Vim will give you a warning:
421
422	W10: Warning: Changing a readonly file ~
423
424The change will be done though.  This allows for formatting the file, for
425example, to be able to read it easily.
426   If you make changes to a file and forgot that it was read-only, you can
427still write it.  Add the ! to the write command to force writing.
428
429If you really want to forbid making changes in a file, do this: >
430
431	vim -M file
432
433Now every attempt to change the text will fail.  The help files are like this,
434for example.  If you try to make a change you get this error message:
435
436	E21: Cannot make changes, 'modifiable' is off ~
437
438You could use the -M argument to setup Vim to work in a viewer mode.  This is
439only voluntary though, since these commands will remove the protection: >
440
441	:set modifiable
442	:set write
443
444==============================================================================
445*07.7*	Changing the file name
446
447A clever way to start editing a new file is by using an existing file that
448contains most of what you need.  For example, you start writing a new program
449to move a file.  You know that you already have a program that copies a file,
450thus you start with: >
451
452	:edit copy.c
453
454You can delete the stuff you don't need.  Now you need to save the file under
455a new name.  The ":saveas" command can be used for this: >
456
457	:saveas move.c
458
459Vim will write the file under the given name, and edit that file.  Thus the
460next time you do ":write", it will write "move.c".  "copy.c" remains
461unmodified.
462   When you want to change the name of the file you are editing, but don't
463want to write the file, you can use this command: >
464
465	:file move.c
466
467Vim will mark the file as "not edited".  This means that Vim knows this is not
468the file you started editing.  When you try to write the file, you might get
469this message:
470
471	E13: File exists (use ! to override) ~
472
473This protects you from accidentally overwriting another file.
474
475==============================================================================
476
477Next chapter: |usr_08.txt|  Splitting windows
478
479Copyright: see |manual-copyright|  vim:tw=78:ts=8:ft=help:norl:
480