robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Eisuke Kawashimadocs: adjust modelines for HTML rendering #396497e778205ab

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