/
editing.txt
1614 lines (1350 loc) · 66 KB
/
editing.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
*editing.txt* For Vim version 7.3a. Last change: 2009 Jun 10
VIM REFERENCE MANUAL by Bram Moolenaar
Editing files *edit-files*
1. Introduction |edit-intro|
2. Editing a file |edit-a-file|
3. The argument list |argument-list|
4. Writing |writing|
5. Writing and quitting |write-quit|
6. Dialogs |edit-dialogs|
7. The current directory |current-directory|
8. Editing binary files |edit-binary|
9. Encryption |encryption|
10. Timestamps |timestamps|
11. File Searching |file-searching|
==============================================================================
1. Introduction *edit-intro*
Editing a file with Vim means:
1. reading the file into a buffer
2. changing the buffer with editor commands
3. writing the buffer into a file
*current-file*
As long as you don't write the buffer, the original file remains unchanged.
If you start editing a file (read a file into the buffer), the file name is
remembered as the "current file name". This is also known as the name of the
current buffer. It can be used with "%" on the command line |:_%|.
*alternate-file*
If there already was a current file name, then that one becomes the alternate
file name. It can be used with "#" on the command line |:_#| and you can use
the |CTRL-^| command to toggle between the current and the alternate file.
However, the alternate file name is not changed when |:keepalt| is used.
*:keepalt* *:keepa*
:keepalt {cmd} Execute {cmd} while keeping the current alternate file
name. Note that commands invoked indirectly (e.g.,
with a function) may still set the alternate file
name. {not in Vi}
All file names are remembered in the buffer list. When you enter a file name,
for editing (e.g., with ":e filename") or writing (e.g., with ":w filename"),
the file name is added to the list. You can use the buffer list to remember
which files you edited and to quickly switch from one file to another (e.g.,
to copy text) with the |CTRL-^| command. First type the number of the file
and then hit CTRL-^. {Vi: only one alternate file name is remembered}
CTRL-G or *CTRL-G* *:f* *:fi* *:file*
:f[ile] Prints the current file name (as typed, unless ":cd"
was used), the cursor position (unless the 'ruler'
option is set), and the file status (readonly,
modified, read errors, new file). See the 'shortmess'
option about how to make this message shorter.
{Vi does not include column number}
:f[ile]! like |:file|, but don't truncate the name even when
'shortmess' indicates this.
{count}CTRL-G Like CTRL-G, but prints the current file name with
full path. If the count is higher than 1 the current
buffer number is also given. {not in Vi}
*g_CTRL-G* *word-count* *byte-count*
g CTRL-G Prints the current position of the cursor in five
ways: Column, Line, Word, Character and Byte. If the
number of Characters and Bytes is the same then the
Character position is omitted.
If there are characters in the line that take more
than one position on the screen (<Tab> or special
character), both the "real" column and the screen
column are shown, separated with a dash.
See also 'ruler' option. {not in Vi}
*v_g_CTRL-G*
{Visual}g CTRL-G Similar to "g CTRL-G", but Word, Character, Line, and
Byte counts for the visually selected region are
displayed.
In Blockwise mode, Column count is also shown. (For
{Visual} see |Visual-mode|.)
{not in VI}
*:file_f*
:f[ile][!] {name} Sets the current file name to {name}. The optional !
avoids truncating the message, as with |:file|.
If the buffer did have a name, that name becomes the
|alternate-file| name. An unlisted buffer is created
to hold the old name.
*:0file*
:0f[ile][!] Remove the name of the current buffer. The optional !
avoids truncating the message, as with |:file|. {not
in Vi}
:buffers
:files
:ls List all the currently known file names. See
'windows.txt' |:files| |:buffers| |:ls|. {not in
Vi}
Vim will remember the full path name of a file name that you enter. In most
cases when the file name is displayed only the name you typed is shown, but
the full path name is being used if you used the ":cd" command |:cd|.
*home-replace*
If the environment variable $HOME is set, and the file name starts with that
string, it is often displayed with HOME replaced with "~". This was done to
keep file names short. When reading or writing files the full name is still
used, the "~" is only used when displaying file names. When replacing the
file name would result in just "~", "~/" is used instead (to avoid confusion
between options set to $HOME with 'backupext' set to "~").
When writing the buffer, the default is to use the current file name. Thus
when you give the "ZZ" or ":wq" command, the original file will be
overwritten. If you do not want this, the buffer can be written into another
file by giving a file name argument to the ":write" command. For example: >
vim testfile
[change the buffer with editor commands]
:w newfile
:q
This will create a file "newfile", that is a modified copy of "testfile".
The file "testfile" will remain unchanged. Anyway, if the 'backup' option is
set, Vim renames or copies the original file before it will be overwritten.
You can use this file if you discover that you need the original file. See
also the 'patchmode' option. The name of the backup file is normally the same
as the original file with 'backupext' appended. The default "~" is a bit
strange to avoid accidentally overwriting existing files. If you prefer ".bak"
change the 'backupext' option. Extra dots are replaced with '_' on MS-DOS
machines, when Vim has detected that an MS-DOS-like filesystem is being used
(e.g., messydos or crossdos) or when the 'shortname' option is on. The
backup file can be placed in another directory by setting 'backupdir'.
*auto-shortname*
Technical: On the Amiga you can use 30 characters for a file name. But on an
MS-DOS-compatible filesystem only 8 plus 3 characters are
available. Vim tries to detect the type of filesystem when it is
creating the .swp file. If an MS-DOS-like filesystem is suspected,
a flag is set that has the same effect as setting the 'shortname'
option. This flag will be reset as soon as you start editing a
new file. The flag will be used when making the file name for the
".swp" and ".~" files for the current file. But when you are
editing a file in a normal filesystem and write to an MS-DOS-like
filesystem the flag will not have been set. In that case the
creation of the ".~" file may fail and you will get an error
message. Use the 'shortname' option in this case.
When you started editing without giving a file name, "No File" is displayed in
messages. If the ":write" command is used with a file name argument, the file
name for the current file is set to that file name. This only happens when
the 'F' flag is included in 'cpoptions' (by default it is included) |cpo-F|.
This is useful when entering text in an empty buffer and then writing it to a
file. If 'cpoptions' contains the 'f' flag (by default it is NOT included)
|cpo-f| the file name is set for the ":read file" command. This is useful
when starting Vim without an argument and then doing ":read file" to start
editing a file.
When the file name was set and 'filetype' is empty the filetype detection
autocommands will be triggered.
*not-edited*
Because the file name was set without really starting to edit that file, you
are protected from overwriting that file. This is done by setting the
"notedited" flag. You can see if this flag is set with the CTRL-G or ":file"
command. It will include "[Not edited]" when the "notedited" flag is set.
When writing the buffer to the current file name (with ":w!"), the "notedited"
flag is reset.
*abandon*
Vim remembers whether you have changed the buffer. You are protected from
losing the changes you made. If you try to quit without writing, or want to
start editing another file, Vim will refuse this. In order to overrule this
protection, add a '!' to the command. The changes will then be lost. For
example: ":q" will not work if the buffer was changed, but ":q!" will. To see
whether the buffer was changed use the "CTRL-G" command. The message includes
the string "[Modified]" if the buffer has been changed.
If you want to automatically save the changes without asking, switch on the
'autowriteall' option. 'autowrite' is the associated Vi-compatible option
that does not work for all commands.
If you want to keep the changed buffer without saving it, switch on the
'hidden' option. See |hidden-buffer|.
==============================================================================
2. Editing a file *edit-a-file*
*:e* *:edit*
:e[dit] [++opt] [+cmd] Edit the current file. This is useful to re-edit the
current file, when it has been changed outside of Vim.
This fails when changes have been made to the current
buffer and 'autowriteall' isn't set or the file can't
be written.
Also see |++opt| and |+cmd|.
{Vi: no ++opt}
*:edit!*
:e[dit]! [++opt] [+cmd]
Edit the current file always. Discard any changes to
the current buffer. This is useful if you want to
start all over again.
Also see |++opt| and |+cmd|.
{Vi: no ++opt}
*:edit_f*
:e[dit] [++opt] [+cmd] {file}
Edit {file}.
This fails when changes have been made to the current
buffer, unless 'hidden' is set or 'autowriteall' is
set and the file can be written.
Also see |++opt| and |+cmd|.
{Vi: no ++opt}
*:edit!_f*
:e[dit]! [++opt] [+cmd] {file}
Edit {file} always. Discard any changes to the
current buffer.
Also see |++opt| and |+cmd|.
{Vi: no ++opt}
:e[dit] [++opt] [+cmd] #[count]
Edit the [count]th buffer (as shown by |:files|).
This command does the same as [count] CTRL-^. But ":e
#" doesn't work if the alternate buffer doesn't have a
file name, while CTRL-^ still works then.
Also see |++opt| and |+cmd|.
{Vi: no ++opt}
*:ene* *:enew*
:ene[w] Edit a new, unnamed buffer. This fails when changes
have been made to the current buffer, unless 'hidden'
is set or 'autowriteall' is set and the file can be
written.
If 'fileformats' is not empty, the first format given
will be used for the new buffer. If 'fileformats' is
empty, the 'fileformat' of the current buffer is used.
{not in Vi}
*:ene!* *:enew!*
:ene[w]! Edit a new, unnamed buffer. Discard any changes to
the current buffer.
Set 'fileformat' like |:enew|.
{not in Vi}
*:fin* *:find*
:fin[d][!] [++opt] [+cmd] {file}
Find {file} in 'path' and then |:edit| it.
{not in Vi} {not available when the |+file_in_path|
feature was disabled at compile time}
:{count}fin[d][!] [++opt] [+cmd] {file}
Just like ":find", but use the {count} match in
'path'. Thus ":2find file" will find the second
"file" found in 'path'. When there are fewer matches
for the file in 'path' than asked for, you get an
error message.
*:ex*
:ex [++opt] [+cmd] [file]
Same as |:edit|.
*:vi* *:visual*
:vi[sual][!] [++opt] [+cmd] [file]
When used in Ex mode: Leave |Ex-mode|, go back to
Normal mode. Otherwise same as |:edit|.
*:vie* *:view*
:vie[w][!] [++opt] [+cmd] file
When used in Ex mode: Leave |Ex mode|, go back to
Normal mode. Otherwise same as |:edit|, but set
'readonly' option for this buffer. {not in Vi}
*CTRL-^* *CTRL-6*
CTRL-^ Edit the alternate file (equivalent to ":e #").
Mostly the alternate file is the previously edited
file. This is a quick way to toggle between two
files.
If the 'autowrite' or 'autowriteall' option is on and
the buffer was changed, write it.
Mostly the ^ character is positioned on the 6 key,
pressing CTRL and 6 then gets you what we call CTRL-^.
But on some non-US keyboards CTRL-^ is produced in
another way.
{count}CTRL-^ Edit [count]th file in the buffer list (equivalent to
":e #[count]"). This is a quick way to switch between
files.
See |CTRL-^| above for further details.
{not in Vi}
[count]]f *]f* *[f*
[count][f Same as "gf". Deprecated.
*gf* *E446* *E447*
[count]gf Edit the file whose name is under or after the cursor.
Mnemonic: "goto file".
Uses the 'isfname' option to find out which characters
are supposed to be in a file name. Trailing
punctuation characters ".,:;!" are ignored.
Uses the 'path' option as a list of directory names to
look for the file. See the 'path' option for details
about relative directories and wildcards.
Uses the 'suffixesadd' option to check for file names
with a suffix added.
If the file can't be found, 'includeexpr' is used to
modify the name and another attempt is done.
If a [count] is given, the count'th file that is found
in the 'path' is edited.
This command fails if Vim refuses to |abandon| the
current file.
If you want to edit the file in a new window use
|CTRL-W_CTRL-F|.
If you do want to edit a new file, use: >
:e <cfile>
< To make gf always work like that: >
:map gf :e <cfile><CR>
< If the name is a hypertext link, that looks like
"type://machine/path", you need the |netrw| plugin.
For Unix the '~' character is expanded, like in
"~user/file". Environment variables are expanded too
|expand-env|.
{not in Vi}
{not available when the |+file_in_path| feature was
disabled at compile time}
*v_gf*
{Visual}[count]gf Same as "gf", but the highlighted text is used as the
name of the file to edit. 'isfname' is ignored.
Leading blanks are skipped, otherwise all blanks and
special characters are included in the file name.
(For {Visual} see |Visual-mode|.)
{not in VI}
*gF*
[count]gF Same as "gf", except if a number follows the file
name, then the cursor is positioned on that line in
the file. The file name and the number must be
separated by a non-filename (see 'isfname') and
non-numeric character. White space between the
filename, the separator and the number are ignored.
Examples:
eval.c:10 ~
eval.c @ 20 ~
eval.c (30) ~
eval.c 40 ~
*v_gF*
{Visual}[count]gF Same as "v_gf".
These commands are used to start editing a single file. This means that the
file is read into the buffer and the current file name is set. The file that
is opened depends on the current directory, see |:cd|.
See |read-messages| for an explanation of the message that is given after the
file has been read.
You can use the ":e!" command if you messed up the buffer and want to start
all over again. The ":e" command is only useful if you have changed the
current file name.
*:filename* *{file}*
Besides the things mentioned here, more special items for where a filename is
expected are mentioned at |cmdline-special|.
Note for systems other than Unix: When using a command that accepts a single
file name (like ":edit file") spaces in the file name are allowed, but
trailing spaces are ignored. This is useful on systems that regularly embed
spaces in file names (like MS-Windows and the Amiga). Example: The command
":e Long File Name " will edit the file "Long File Name". When using a
command that accepts more than one file name (like ":next file1 file2")
embedded spaces must be escaped with a backslash.
*wildcard* *wildcards*
Wildcards in {file} are expanded. Which wildcards are supported depends on
the system. These are the common ones:
? matches one character
* matches anything, including nothing
** matches anything, including nothing, recurses into directories
[abc] match 'a', 'b' or 'c'
To avoid the special meaning of the wildcards prepend a backslash. However,
on MS-Windows the backslash is a path separator and "path\[abc]" is still seen
as a wildcard when "[" is in the 'isfname' option. A simple way to avoid this
is to use "path\[[]abc]". Then the file "path[abc]" literally.
*starstar-wildcard*
Expanding "**" is possible on Unix, Win32, Mac OS/X and a few other systems.
This allows searching a directory tree. This goes up to 100 directories deep.
Note there are some commands where this works slightly different, see
|file-searching|.
Example: >
:n **/*.txt
Finds files:
ttt.txt
subdir/ttt.txt
a/b/c/d/ttt.txt
When non-wildcard characters are used these are only matched in the first
directory. Example: >
:n /usr/inc**/*.h
Finds files:
/usr/include/types.h
/usr/include/sys/types.h
/usr/inc_old/types.h
*backtick-expansion* *`-expansion*
On Unix and a few other systems you can also use backticks in the file name,
for example: >
:e `find . -name ver\\*.c -print`
The backslashes before the star are required to prevent "ver*.c" to be
expanded by the shell before executing the find program.
This also works for most other systems, with the restriction that the
backticks must be around the whole item. It is not possible to have text
directly before the first or just after the last backtick.
*`=*
You can have the backticks expanded as a Vim expression, instead of an
external command, by using the syntax `={expr}` e.g.: >
:e `=tempname()`
The expression can contain just about anything, thus this can also be used to
avoid the special meaning of '"', '|', '%' and '#'. Names are to be separated
with line breaks. When the result is a |List| then each item is used as a
name. Line breaks also separate names.
*++opt* *[++opt]*
The [++opt] argument can be used to force the value of 'fileformat',
'fileencoding' or 'binary' to a value for one command, and to specify the
behavior for bad characters. The form is: >
++{optname}
Or: >
++{optname}={value}
Where {optname} is one of: *++ff* *++enc* *++bin* *++nobin* *++edit*
ff or fileformat overrides 'fileformat'
enc or encoding overrides 'fileencoding'
bin or binary sets 'binary'
nobin or nobinary resets 'binary'
bad specifies behavior for bad characters
edit for |:read| only: keep option values as if editing
a file
{value} cannot contain white space. It can be any valid value for these
options. Examples: >
:e ++ff=unix
This edits the same file again with 'fileformat' set to "unix". >
:w ++enc=latin1 newfile
This writes the current buffer to "newfile" in latin1 format.
There may be several ++opt arguments, separated by white space. They must all
appear before any |+cmd| argument.
*++bad*
The argument of "++bad=" specifies what happens with characters that can't be
converted and illegal bytes. It can be one of three things:
++bad=X A single-byte character that replaces each bad character.
++bad=keep Keep bad characters without conversion. Note that this may
result in illegal bytes in your text!
++bad=drop Remove the bad characters.
The default is like "++bad=?": Replace each bad character with a question
mark. In some places an inverted question mark is used (0xBF).
Note that not all commands use the ++bad argument, even though they do not
give an error when you add it. E.g. |:write|.
Note that when reading, the 'fileformat' and 'fileencoding' options will be
set to the used format. When writing this doesn't happen, thus a next write
will use the old value of the option. Same for the 'binary' option.
*+cmd* *[+cmd]*
The [+cmd] argument can be used to position the cursor in the newly opened
file, or execute any other command:
+ Start at the last line.
+{num} Start at line {num}.
+/{pat} Start at first line containing {pat}.
+{command} Execute {command} after opening the new file.
{command} is any Ex command.
To include a white space in the {pat} or {command}, precede it with a
backslash. Double the number of backslashes. >
:edit +/The\ book file
:edit +/dir\ dirname\\ file
:edit +set\ dir=c:\\\\temp file
Note that in the last example the number of backslashes is halved twice: Once
for the "+cmd" argument and once for the ":set" command.
*file-formats*
The 'fileformat' option sets the <EOL> style for a file:
'fileformat' characters name ~
"dos" <CR><NL> or <NL> DOS format *DOS-format*
"unix" <NL> Unix format *Unix-format*
"mac" <CR> Mac format *Mac-format*
Previously 'textmode' was used. It is obsolete now.
When reading a file, the mentioned characters are interpreted as the <EOL>.
In DOS format (default for MS-DOS, OS/2 and Win32), <CR><NL> and <NL> are both
interpreted as the <EOL>. Note that when writing the file in DOS format,
<CR> characters will be added for each single <NL>. Also see |file-read|.
When writing a file, the mentioned characters are used for <EOL>. For DOS
format <CR><NL> is used. Also see |DOS-format-write|.
You can read a file in DOS format and write it in Unix format. This will
replace all <CR><NL> pairs by <NL> (assuming 'fileformats' includes "dos"): >
:e file
:set fileformat=unix
:w
If you read a file in Unix format and write with DOS format, all <NL>
characters will be replaced with <CR><NL> (assuming 'fileformats' includes
"unix"): >
:e file
:set fileformat=dos
:w
If you start editing a new file and the 'fileformats' option is not empty
(which is the default), Vim will try to detect whether the lines in the file
are separated by the specified formats. When set to "unix,dos", Vim will
check for lines with a single <NL> (as used on Unix and Amiga) or by a <CR>
<NL> pair (MS-DOS). Only when ALL lines end in <CR><NL>, 'fileformat' is set
to "dos", otherwise it is set to "unix". When 'fileformats' includes "mac",
and no <NL> characters are found in the file, 'fileformat' is set to "mac".
If the 'fileformat' option is set to "dos" on non-MS-DOS systems the message
"[dos format]" is shown to remind you that something unusual is happening. On
MS-DOS systems you get the message "[unix format]" if 'fileformat' is set to
"unix". On all systems but the Macintosh you get the message "[mac format]"
if 'fileformat' is set to "mac".
If the 'fileformats' option is empty and DOS format is used, but while reading
a file some lines did not end in <CR><NL>, "[CR missing]" will be included in
the file message.
If the 'fileformats' option is empty and Mac format is used, but while reading
a file a <NL> was found, "[NL missing]" will be included in the file message.
If the new file does not exist, the 'fileformat' of the current buffer is used
when 'fileformats' is empty. Otherwise the first format from 'fileformats' is
used for the new file.
Before editing binary, executable or Vim script files you should set the
'binary' option. A simple way to do this is by starting Vim with the "-b"
option. This will avoid the use of 'fileformat'. Without this you risk that
single <NL> characters are unexpectedly replaced with <CR><NL>.
You can encrypt files that are written by setting the 'key' option. This
provides some security against others reading your files. |encryption|
==============================================================================
3. The argument list *argument-list* *arglist*
If you give more than one file name when starting Vim, this list is remembered
as the argument list. You can jump to each file in this list.
Do not confuse this with the buffer list, which you can see with the
|:buffers| command. The argument list was already present in Vi, the buffer
list is new in Vim. Every file name in the argument list will also be present
in the buffer list (unless it was deleted with |:bdel| or |:bwipe|). But it's
common that names in the buffer list are not in the argument list.
This subject is introduced in section |07.2| of the user manual.
There is one global argument list, which is used for all windows by default.
It is possible to create a new argument list local to a window, see
|:arglocal|.
You can use the argument list with the following commands, and with the
expression functions |argc()| and |argv()|. These all work on the argument
list of the current window.
*:ar* *:args*
:ar[gs] Print the argument list, with the current file in
square brackets.
:ar[gs] [++opt] [+cmd] {arglist} *:args_f*
Define {arglist} as the new argument list and edit
the first one. This fails when changes have been made
and Vim does not want to |abandon| the current buffer.
Also see |++opt| and |+cmd|.
{Vi: no ++opt}
:ar[gs]! [++opt] [+cmd] {arglist} *:args_f!*
Define {arglist} as the new argument list and edit
the first one. Discard any changes to the current
buffer.
Also see |++opt| and |+cmd|.
{Vi: no ++opt}
:[count]arge[dit][!] [++opt] [+cmd] {name} *:arge* *:argedit*
Add {name} to the argument list and edit it.
When {name} already exists in the argument list, this
entry is edited.
This is like using |:argadd| and then |:edit|.
Note that only one file name is allowed, and spaces
inside the file name are allowed, like with |:edit|.
[count] is used like with |:argadd|.
[!] is required if the current file cannot be
|abandon|ed.
Also see |++opt| and |+cmd|.
{not in Vi}
:[count]arga[dd] {name} .. *:arga* *:argadd* *E479*
Add the {name}s to the argument list.
If [count] is omitted, the {name}s are added just
after the current entry in the argument list.
Otherwise they are added after the [count]'th file.
If the argument list is "a b c", and "b" is the
current argument, then these commands result in:
command new argument list ~
:argadd x a b x c
:0argadd x x a b c
:1argadd x a x b c
:99argadd x a b c x
There is no check for duplicates, it is possible to
add a file to the argument list twice.
The currently edited file is not changed.
{not in Vi} {not available when compiled without the
|+listcmds| feature}
Note: you can also use this method: >
:args ## x
< This will add the "x" item and sort the new list.
:argd[elete] {pattern} .. *:argd* *:argdelete* *E480*
Delete files from the argument list that match the
{pattern}s. {pattern} is used like a file pattern,
see |file-pattern|. "%" can be used to delete the
current entry.
This command keeps the currently edited file, also
when it's deleted from the argument list.
Example: >
:argdel *.obj
< {not in Vi} {not available when compiled without the
|+listcmds| feature}
:{range}argd[elete] Delete the {range} files from the argument list.
When the last number in the range is too high, up to
the last argument is deleted. Example: >
:10,1000argdel
< Deletes arguments 10 and further, keeping 1-9.
{not in Vi} {not available when compiled without the
|+listcmds| feature}
*:argu* *:argument*
:[count]argu[ment] [count] [++opt] [+cmd]
Edit file [count] in the argument list. When [count]
is omitted the current entry is used. This fails
when changes have been made and Vim does not want to
|abandon| the current buffer.
Also see |++opt| and |+cmd|.
{not in Vi} {not available when compiled without the
|+listcmds| feature}
:[count]argu[ment]! [count] [++opt] [+cmd]
Edit file [count] in the argument list, discard any
changes to the current buffer. When [count] is
omitted the current entry is used.
Also see |++opt| and |+cmd|.
{not in Vi} {not available when compiled without the
|+listcmds| feature}
:[count]n[ext] [++opt] [+cmd] *:n* *:ne* *:next* *E165* *E163*
Edit [count] next file. This fails when changes have
been made and Vim does not want to |abandon| the
current buffer. Also see |++opt| and |+cmd|. {Vi: no
count or ++opt}.
:[count]n[ext]! [++opt] [+cmd]
Edit [count] next file, discard any changes to the
buffer. Also see |++opt| and |+cmd|. {Vi: no count
or ++opt}.
:n[ext] [++opt] [+cmd] {arglist} *:next_f*
Same as |:args_f|.
:n[ext]! [++opt] [+cmd] {arglist}
Same as |:args_f!|.
:[count]N[ext] [count] [++opt] [+cmd] *:Next* *:N* *E164*
Edit [count] previous file in argument list. This
fails when changes have been made and Vim does not
want to |abandon| the current buffer.
Also see |++opt| and |+cmd|. {Vi: no count or ++opt}.
:[count]N[ext]! [count] [++opt] [+cmd]
Edit [count] previous file in argument list. Discard
any changes to the buffer. Also see |++opt| and
|+cmd|. {Vi: no count or ++opt}.
:[count]prev[ious] [count] [++opt] [+cmd] *:prev* *:previous*
Same as :Next. Also see |++opt| and |+cmd|. {Vi:
only in some versions}
*:rew* *:rewind*
:rew[ind] [++opt] [+cmd]
Start editing the first file in the argument list.
This fails when changes have been made and Vim does
not want to |abandon| the current buffer.
Also see |++opt| and |+cmd|. {Vi: no ++opt}
:rew[ind]! [++opt] [+cmd]
Start editing the first file in the argument list.
Discard any changes to the buffer. Also see |++opt|
and |+cmd|. {Vi: no ++opt}
*:fir* *:first*
:fir[st][!] [++opt] [+cmd]
Other name for ":rewind". {not in Vi}
*:la* *:last*
:la[st] [++opt] [+cmd]
Start editing the last file in the argument list.
This fails when changes have been made and Vim does
not want to |abandon| the current buffer.
Also see |++opt| and |+cmd|. {not in Vi}
:la[st]! [++opt] [+cmd]
Start editing the last file in the argument list.
Discard any changes to the buffer. Also see |++opt|
and |+cmd|. {not in Vi}
*:wn* *:wnext*
:[count]wn[ext] [++opt]
Write current file and start editing the [count]
next file. Also see |++opt| and |+cmd|. {not in Vi}
:[count]wn[ext] [++opt] {file}
Write current file to {file} and start editing the
[count] next file, unless {file} already exists and
the 'writeany' option is off. Also see |++opt| and
|+cmd|. {not in Vi}
:[count]wn[ext]! [++opt] {file}
Write current file to {file} and start editing the
[count] next file. Also see |++opt| and |+cmd|. {not
in Vi}
:[count]wN[ext][!] [++opt] [file] *:wN* *:wNext*
:[count]wp[revious][!] [++opt] [file] *:wp* *:wprevious*
Same as :wnext, but go to previous file instead of
next. {not in Vi}
The [count] in the commands above defaults to one. For some commands it is
possible to use two counts. The last one (rightmost one) is used.
If no [+cmd] argument is present, the cursor is positioned at the last known
cursor position for the file. If 'startofline' is set, the cursor will be
positioned at the first non-blank in the line, otherwise the last know column
is used. If there is no last known cursor position the cursor will be in the
first line (the last line in Ex mode).
*{arglist}*
The wildcards in the argument list are expanded and the file names are sorted.
Thus you can use the command "vim *.c" to edit all the C files. From within
Vim the command ":n *.c" does the same.
White space is used to separate file names. Put a backslash before a space or
tab to include it in a file name. E.g., to edit the single file "foo bar": >
:next foo\ bar
On Unix and a few other systems you can also use backticks, for example: >
:next `find . -name \\*.c -print`
The backslashes before the star are required to prevent "*.c" to be expanded
by the shell before executing the find program.
*arglist-position*
When there is an argument list you can see which file you are editing in the
title of the window (if there is one and 'title' is on) and with the file
message you get with the "CTRL-G" command. You will see something like
(file 4 of 11)
If 'shortmess' contains 'f' it will be
(4 of 11)
If you are not really editing the file at the current position in the argument
list it will be
(file (4) of 11)
This means that you are position 4 in the argument list, but not editing the
fourth file in the argument list. This happens when you do ":e file".
LOCAL ARGUMENT LIST
{not in Vi}
{not available when compiled without the |+windows| or |+listcmds| feature}
*:arglocal*
:argl[ocal] Make a local copy of the global argument list.
Doesn't start editing another file.
:argl[ocal][!] [++opt] [+cmd] {arglist}
Define a new argument list, which is local to the
current window. Works like |:args_f| otherwise.
*:argglobal*
:argg[lobal] Use the global argument list for the current window.
Doesn't start editing another file.
:argg[lobal][!] [++opt] [+cmd] {arglist}
Use the global argument list for the current window.
Define a new global argument list like |:args_f|.
All windows using the global argument list will see
this new list.
There can be several argument lists. They can be shared between windows.
When they are shared, changing the argument list in one window will also
change it in the other window.
When a window is split the new window inherits the argument list from the
current window. The two windows then share this list, until one of them uses
|:arglocal| or |:argglobal| to use another argument list.
USING THE ARGUMENT LIST
*:argdo*
:argdo[!] {cmd} Execute {cmd} for each file in the argument list.
It works like doing this: >
:rewind
:{cmd}
:next
:{cmd}
etc.
< When the current file can't be |abandon|ed and the [!]
is not present, the command fails.
When an error is detected on one file, further files
in the argument list will not be visited.
The last file in the argument list (or where an error
occurred) becomes the current file.
{cmd} can contain '|' to concatenate several commands.
{cmd} must not change the argument list.
Note: While this command is executing, the Syntax
autocommand event is disabled by adding it to
'eventignore'. This considerably speeds up editing
each file.
{not in Vi} {not available when compiled without the
|+listcmds| feature}
Also see |:windo|, |:tabdo| and |:bufdo|.
Example: >
:args *.c
:argdo set ff=unix | update
This sets the 'fileformat' option to "unix" and writes the file if it is now
changed. This is done for all *.c files.
Example: >
:args *.[ch]
:argdo %s/\<my_foo\>/My_Foo/ge | update
This changes the word "my_foo" to "My_Foo" in all *.c and *.h files. The "e"
flag is used for the ":substitute" command to avoid an error for files where
"my_foo" isn't used. ":update" writes the file only if changes were made.
==============================================================================
4. Writing *writing* *save-file*
Note: When the 'write' option is off, you are not able to write any file.
*:w* *:write*
*E502* *E503* *E504* *E505*
*E512* *E514* *E667* *E796*
:w[rite] [++opt] Write the whole buffer to the current file. This is
the normal way to save changes to a file. It fails
when the 'readonly' option is set or when there is
another reason why the file can't be written.
For ++opt see |++opt|, but only ++bin, ++nobin, ++ff
and ++enc are effective.
:w[rite]! [++opt] Like ":write", but forcefully write when 'readonly' is
set or there is another reason why writing was
refused.
Note: This may change the permission and ownership of
the file and break (symbolic) links. Add the 'W' flag
to 'cpoptions' to avoid this.
:[range]w[rite][!] [++opt]
Write the specified lines to the current file. This
is unusual, because the file will not contain all
lines in the buffer.
*:w_f* *:write_f*
:[range]w[rite] [++opt] {file}
Write the specified lines to {file}, unless it
already exists and the 'writeany' option is off.
*:w!*
:[range]w[rite]! [++opt] {file}
Write the specified lines to {file}. Overwrite an
existing file.
*:w_a* *:write_a* *E494*
:[range]w[rite][!] [++opt] >>
Append the specified lines to the current file.
:[range]w[rite][!] [++opt] >> {file}
Append the specified lines to {file}. '!' forces the
write even if file does not exist.
*:w_c* *:write_c*
:[range]w[rite] [++opt] !{cmd}
Execute {cmd} with [range] lines as standard input
(note the space in front of the '!'). {cmd} is
executed like with ":!{cmd}", any '!' is replaced with
the previous command |:!|.
The default [range] for the ":w" command is the whole buffer (1,$). If you
write the whole buffer, it is no longer considered changed. When you
write it to a different file with ":w somefile" it depends on the "+" flag in
'cpoptions'. When included, the write command will reset the 'modified' flag,
even though the buffer itself may still be different from its file.
If a file name is given with ":w" it becomes the alternate file. This can be
used, for example, when the write fails and you want to try again later with
":w #". This can be switched off by removing the 'A' flag from the
'cpoptions' option.
*:sav* *:saveas*
:sav[eas][!] [++opt] {file}
Save the current buffer under the name {file} and set
the filename of the current buffer to {file}. The
previous name is used for the alternate file name.
The [!] is needed to overwrite an existing file.
When 'filetype' is empty filetype detection is done
with the new name, before the file is written.
When the write was successful 'readonly' is reset.
{not in Vi}
*:up* *:update*
:[range]up[date][!] [++opt] [>>] [file]
Like ":write", but only write when the buffer has been
modified. {not in Vi}
WRITING WITH MULTIPLE BUFFERS *buffer-write*
*:wa* *:wall*
:wa[ll] Write all changed buffers. Buffers without a file
name or which are readonly are not written. {not in
Vi}
:wa[ll]! Write all changed buffers, even the ones that are
readonly. Buffers without a file name are not
written. {not in Vi}
Vim will warn you if you try to overwrite a file that has been changed
elsewhere. See |timestamp|.
*backup* *E207* *E506* *E507* *E508* *E509* *E510*
If you write to an existing file (but do not append) while the 'backup',
'writebackup' or 'patchmode' option is on, a backup of the original file is
made. The file is either copied or renamed (see 'backupcopy'). After the
file has been successfully written and when the 'writebackup' option is on and
the 'backup' option is off, the backup file is deleted. When the 'patchmode'
option is on the backup file may be renamed.
*backup-table*
'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
When the 'backupskip' pattern matches with the name of the file which is
written, no backup file is made. The values of 'backup' and 'writebackup' are
ignored then.
When the 'backup' option is on, an old backup file (with the same name as the
new backup file) will be deleted. If 'backup' is not set, but 'writebackup'
is set, an existing backup file will not be deleted. The backup file that is
made while the file is being written will have a different name.
On some filesystems it's possible that in a crash you lose both the backup and
the newly written file (it might be there but contain bogus data). In that
case try recovery, because the swap file is synced to disk and might still be
there. |:recover|
The directories given with the 'backupdir' option is used to put the backup
file in. (default: same directory as the written file).
Whether the backup is a new file, which is a copy of the original file, or the
original file renamed depends on the 'backupcopy' option. See there for an
explanation of when the copy is made and when the file is renamed.
If the creation of a backup file fails, the write is not done. If you want
to write anyway add a '!' to the command.
*write-readonly*
When the 'cpoptions' option contains 'W', Vim will refuse to overwrite a
readonly file. When 'W' is not present, ":w!" will overwrite a readonly file,
if the system allows it (the directory must be writable).
*write-fail*
If the writing of the new file fails, you have to be careful not to lose
your changes AND the original file. If there is no backup file and writing
the new file failed, you have already lost the original file! DON'T EXIT VIM
UNTIL YOU WRITE OUT THE FILE! If a backup was made, it is put back in place
of the original file (if possible). If you exit Vim, and lose the changes
you made, the original file will mostly still be there. If putting back the
original file fails, there will be an error message telling you that you
lost the original file.