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