blob: 1260194524ad0358976d4834283b8f74a87d8e23 [file] [log] [blame]
Bram Moolenaar2ec618c2016-10-01 14:47:05 +02001*editing.txt* For Vim version 8.0. Last change: 2016 Sep 27
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7Editing files *edit-files*
8
91. Introduction |edit-intro|
102. Editing a file |edit-a-file|
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000113. The argument list |argument-list|
124. Writing |writing|
135. Writing and quitting |write-quit|
146. Dialogs |edit-dialogs|
157. The current directory |current-directory|
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168. Editing binary files |edit-binary|
179. Encryption |encryption|
1810. Timestamps |timestamps|
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001911. File Searching |file-searching|
Bram Moolenaar071d4272004-06-13 20:20:40 +000020
21==============================================================================
221. Introduction *edit-intro*
23
24Editing a file with Vim means:
25
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000261. reading the file into a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272. changing the buffer with editor commands
283. writing the buffer into a file
29
30 *current-file*
31As long as you don't write the buffer, the original file remains unchanged.
32If you start editing a file (read a file into the buffer), the file name is
Bram Moolenaard4755bb2004-09-02 19:12:26 +000033remembered as the "current file name". This is also known as the name of the
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000034current buffer. It can be used with "%" on the command line |:_%|.
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
36 *alternate-file*
37If there already was a current file name, then that one becomes the alternate
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000038file name. It can be used with "#" on the command line |:_#| and you can use
39the |CTRL-^| command to toggle between the current and the alternate file.
40However, the alternate file name is not changed when |:keepalt| is used.
Bram Moolenaar36782082013-11-28 13:53:34 +010041An alternate file name is remembered for each window.
Bram Moolenaard4755bb2004-09-02 19:12:26 +000042
43 *:keepalt* *:keepa*
44:keepalt {cmd} Execute {cmd} while keeping the current alternate file
45 name. Note that commands invoked indirectly (e.g.,
46 with a function) may still set the alternate file
47 name. {not in Vi}
48
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000049All file names are remembered in the buffer list. When you enter a file name,
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000050for editing (e.g., with ":e filename") or writing (e.g., with ":w filename"),
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000051the file name is added to the list. You can use the buffer list to remember
52which files you edited and to quickly switch from one file to another (e.g.,
53to copy text) with the |CTRL-^| command. First type the number of the file
54and then hit CTRL-^. {Vi: only one alternate file name is remembered}
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056
57CTRL-G or *CTRL-G* *:f* *:fi* *:file*
Bram Moolenaard9d30582005-05-18 22:10:28 +000058:f[ile] Prints the current file name (as typed, unless ":cd"
59 was used), the cursor position (unless the 'ruler'
60 option is set), and the file status (readonly,
61 modified, read errors, new file). See the 'shortmess'
62 option about how to make this message shorter.
63 {Vi does not include column number}
Bram Moolenaar071d4272004-06-13 20:20:40 +000064
Bram Moolenaar325b7a22004-07-05 15:58:32 +000065:f[ile]! like |:file|, but don't truncate the name even when
66 'shortmess' indicates this.
67
Bram Moolenaar071d4272004-06-13 20:20:40 +000068{count}CTRL-G Like CTRL-G, but prints the current file name with
69 full path. If the count is higher than 1 the current
70 buffer number is also given. {not in Vi}
71
72 *g_CTRL-G* *word-count* *byte-count*
Bram Moolenaar7c626922005-02-07 22:01:03 +000073g CTRL-G Prints the current position of the cursor in five
74 ways: Column, Line, Word, Character and Byte. If the
75 number of Characters and Bytes is the same then the
76 Character position is omitted.
77 If there are characters in the line that take more
78 than one position on the screen (<Tab> or special
79 character), both the "real" column and the screen
80 column are shown, separated with a dash.
Bram Moolenaared767a22016-01-03 22:49:16 +010081 Also see the 'ruler' option and the |wordcount()|
82 function.
83 {not in Vi}
Bram Moolenaar071d4272004-06-13 20:20:40 +000084
85 *v_g_CTRL-G*
Bram Moolenaar7c626922005-02-07 22:01:03 +000086{Visual}g CTRL-G Similar to "g CTRL-G", but Word, Character, Line, and
87 Byte counts for the visually selected region are
88 displayed.
89 In Blockwise mode, Column count is also shown. (For
Bram Moolenaar071d4272004-06-13 20:20:40 +000090 {Visual} see |Visual-mode|.)
91 {not in VI}
92
93 *:file_f*
Bram Moolenaar325b7a22004-07-05 15:58:32 +000094:f[ile][!] {name} Sets the current file name to {name}. The optional !
95 avoids truncating the message, as with |:file|.
Bram Moolenaar7171abe2004-10-11 10:06:20 +000096 If the buffer did have a name, that name becomes the
97 |alternate-file| name. An unlisted buffer is created
98 to hold the old name.
Bram Moolenaar10de2da2005-01-27 14:33:00 +000099 *:0file*
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000100:0f[ile][!] Remove the name of the current buffer. The optional !
101 avoids truncating the message, as with |:file|. {not
102 in Vi}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103
104:buffers
105:files
106:ls List all the currently known file names. See
107 'windows.txt' |:files| |:buffers| |:ls|. {not in
108 Vi}
109
110Vim will remember the full path name of a file name that you enter. In most
111cases when the file name is displayed only the name you typed is shown, but
112the full path name is being used if you used the ":cd" command |:cd|.
113
114 *home-replace*
115If the environment variable $HOME is set, and the file name starts with that
116string, it is often displayed with HOME replaced with "~". This was done to
117keep file names short. When reading or writing files the full name is still
118used, the "~" is only used when displaying file names. When replacing the
119file name would result in just "~", "~/" is used instead (to avoid confusion
Bram Moolenaar81695252004-12-29 20:58:21 +0000120between options set to $HOME with 'backupext' set to "~").
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122When writing the buffer, the default is to use the current file name. Thus
123when you give the "ZZ" or ":wq" command, the original file will be
124overwritten. If you do not want this, the buffer can be written into another
125file by giving a file name argument to the ":write" command. For example: >
126
127 vim testfile
128 [change the buffer with editor commands]
129 :w newfile
130 :q
131
132This will create a file "newfile", that is a modified copy of "testfile".
133The file "testfile" will remain unchanged. Anyway, if the 'backup' option is
134set, Vim renames or copies the original file before it will be overwritten.
135You can use this file if you discover that you need the original file. See
136also the 'patchmode' option. The name of the backup file is normally the same
137as the original file with 'backupext' appended. The default "~" is a bit
138strange to avoid accidentally overwriting existing files. If you prefer ".bak"
139change the 'backupext' option. Extra dots are replaced with '_' on MS-DOS
140machines, when Vim has detected that an MS-DOS-like filesystem is being used
141(e.g., messydos or crossdos) or when the 'shortname' option is on. The
142backup file can be placed in another directory by setting 'backupdir'.
143
144 *auto-shortname*
145Technical: On the Amiga you can use 30 characters for a file name. But on an
146 MS-DOS-compatible filesystem only 8 plus 3 characters are
147 available. Vim tries to detect the type of filesystem when it is
148 creating the .swp file. If an MS-DOS-like filesystem is suspected,
149 a flag is set that has the same effect as setting the 'shortname'
150 option. This flag will be reset as soon as you start editing a
151 new file. The flag will be used when making the file name for the
152 ".swp" and ".~" files for the current file. But when you are
153 editing a file in a normal filesystem and write to an MS-DOS-like
154 filesystem the flag will not have been set. In that case the
155 creation of the ".~" file may fail and you will get an error
156 message. Use the 'shortname' option in this case.
157
158When you started editing without giving a file name, "No File" is displayed in
159messages. If the ":write" command is used with a file name argument, the file
160name for the current file is set to that file name. This only happens when
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000161the 'F' flag is included in 'cpoptions' (by default it is included) |cpo-F|.
162This is useful when entering text in an empty buffer and then writing it to a
163file. If 'cpoptions' contains the 'f' flag (by default it is NOT included)
164|cpo-f| the file name is set for the ":read file" command. This is useful
165when starting Vim without an argument and then doing ":read file" to start
166editing a file.
167When the file name was set and 'filetype' is empty the filetype detection
168autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 *not-edited*
170Because the file name was set without really starting to edit that file, you
171are protected from overwriting that file. This is done by setting the
172"notedited" flag. You can see if this flag is set with the CTRL-G or ":file"
173command. It will include "[Not edited]" when the "notedited" flag is set.
174When writing the buffer to the current file name (with ":w!"), the "notedited"
175flag is reset.
176
177 *abandon*
178Vim remembers whether you have changed the buffer. You are protected from
179losing the changes you made. If you try to quit without writing, or want to
180start editing another file, Vim will refuse this. In order to overrule this
181protection, add a '!' to the command. The changes will then be lost. For
182example: ":q" will not work if the buffer was changed, but ":q!" will. To see
183whether the buffer was changed use the "CTRL-G" command. The message includes
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200184the string "[Modified]" if the buffer has been changed, or "+" if the 'm' flag
185is in 'shortmess'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
187If you want to automatically save the changes without asking, switch on the
188'autowriteall' option. 'autowrite' is the associated Vi-compatible option
189that does not work for all commands.
190
191If you want to keep the changed buffer without saving it, switch on the
Bram Moolenaar9d98fe92013-08-03 18:35:36 +0200192'hidden' option. See |hidden-buffer|. Some commands work like this even when
193'hidden' is not set, check the help for the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194
195==============================================================================
1962. Editing a file *edit-a-file*
197
Bram Moolenaar9d98fe92013-08-03 18:35:36 +0200198 *:e* *:edit* *reload*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199:e[dit] [++opt] [+cmd] Edit the current file. This is useful to re-edit the
200 current file, when it has been changed outside of Vim.
201 This fails when changes have been made to the current
202 buffer and 'autowriteall' isn't set or the file can't
203 be written.
204 Also see |++opt| and |+cmd|.
205 {Vi: no ++opt}
206
Bram Moolenaar9d98fe92013-08-03 18:35:36 +0200207 *:edit!* *discard*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208:e[dit]! [++opt] [+cmd]
209 Edit the current file always. Discard any changes to
210 the current buffer. This is useful if you want to
211 start all over again.
212 Also see |++opt| and |+cmd|.
213 {Vi: no ++opt}
214
215 *:edit_f*
216:e[dit] [++opt] [+cmd] {file}
217 Edit {file}.
218 This fails when changes have been made to the current
219 buffer, unless 'hidden' is set or 'autowriteall' is
220 set and the file can be written.
221 Also see |++opt| and |+cmd|.
222 {Vi: no ++opt}
223
224 *:edit!_f*
225:e[dit]! [++opt] [+cmd] {file}
226 Edit {file} always. Discard any changes to the
227 current buffer.
228 Also see |++opt| and |+cmd|.
229 {Vi: no ++opt}
230
231:e[dit] [++opt] [+cmd] #[count]
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000232 Edit the [count]th buffer (as shown by |:files|).
233 This command does the same as [count] CTRL-^. But ":e
234 #" doesn't work if the alternate buffer doesn't have a
235 file name, while CTRL-^ still works then.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 Also see |++opt| and |+cmd|.
237 {Vi: no ++opt}
238
239 *:ene* *:enew*
240:ene[w] Edit a new, unnamed buffer. This fails when changes
241 have been made to the current buffer, unless 'hidden'
242 is set or 'autowriteall' is set and the file can be
243 written.
244 If 'fileformats' is not empty, the first format given
245 will be used for the new buffer. If 'fileformats' is
246 empty, the 'fileformat' of the current buffer is used.
247 {not in Vi}
248
249 *:ene!* *:enew!*
250:ene[w]! Edit a new, unnamed buffer. Discard any changes to
251 the current buffer.
252 Set 'fileformat' like |:enew|.
253 {not in Vi}
254
255 *:fin* *:find*
256:fin[d][!] [++opt] [+cmd] {file}
257 Find {file} in 'path' and then |:edit| it.
258 {not in Vi} {not available when the |+file_in_path|
259 feature was disabled at compile time}
260
261:{count}fin[d][!] [++opt] [+cmd] {file}
262 Just like ":find", but use the {count} match in
263 'path'. Thus ":2find file" will find the second
264 "file" found in 'path'. When there are fewer matches
265 for the file in 'path' than asked for, you get an
266 error message.
267
268 *:ex*
269:ex [++opt] [+cmd] [file]
270 Same as |:edit|.
271
272 *:vi* *:visual*
273:vi[sual][!] [++opt] [+cmd] [file]
Bram Moolenaar81695252004-12-29 20:58:21 +0000274 When used in Ex mode: Leave |Ex-mode|, go back to
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 Normal mode. Otherwise same as |:edit|.
276
277 *:vie* *:view*
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100278:vie[w][!] [++opt] [+cmd] file
Bram Moolenaar81695252004-12-29 20:58:21 +0000279 When used in Ex mode: Leave |Ex mode|, go back to
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 Normal mode. Otherwise same as |:edit|, but set
281 'readonly' option for this buffer. {not in Vi}
282
283 *CTRL-^* *CTRL-6*
Bram Moolenaar662db672011-03-22 14:05:35 +0100284CTRL-^ Edit the alternate file. Mostly the alternate file is
285 the previously edited file. This is a quick way to
286 toggle between two files. It is equivalent to ":e #",
287 except that it also works when there is no file name.
288
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 If the 'autowrite' or 'autowriteall' option is on and
290 the buffer was changed, write it.
291 Mostly the ^ character is positioned on the 6 key,
292 pressing CTRL and 6 then gets you what we call CTRL-^.
293 But on some non-US keyboards CTRL-^ is produced in
294 another way.
295
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000296{count}CTRL-^ Edit [count]th file in the buffer list (equivalent to
297 ":e #[count]"). This is a quick way to switch between
298 files.
299 See |CTRL-^| above for further details.
300 {not in Vi}
301
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302[count]]f *]f* *[f*
303[count][f Same as "gf". Deprecated.
304
305 *gf* *E446* *E447*
306[count]gf Edit the file whose name is under or after the cursor.
307 Mnemonic: "goto file".
308 Uses the 'isfname' option to find out which characters
309 are supposed to be in a file name. Trailing
Bram Moolenaar2b8388b2015-02-28 13:11:45 +0100310 punctuation characters ".,:;!" are ignored. Escaped
311 spaces "\ " are reduced to a single space.
Bram Moolenaarc236c162008-07-13 17:41:49 +0000312 Uses the 'path' option as a list of directory names to
313 look for the file. See the 'path' option for details
314 about relative directories and wildcards.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 Uses the 'suffixesadd' option to check for file names
316 with a suffix added.
317 If the file can't be found, 'includeexpr' is used to
318 modify the name and another attempt is done.
319 If a [count] is given, the count'th file that is found
320 in the 'path' is edited.
321 This command fails if Vim refuses to |abandon| the
322 current file.
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000323 If you want to edit the file in a new window use
324 |CTRL-W_CTRL-F|.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 If you do want to edit a new file, use: >
326 :e <cfile>
327< To make gf always work like that: >
328 :map gf :e <cfile><CR>
329< If the name is a hypertext link, that looks like
330 "type://machine/path", you need the |netrw| plugin.
331 For Unix the '~' character is expanded, like in
332 "~user/file". Environment variables are expanded too
333 |expand-env|.
334 {not in Vi}
335 {not available when the |+file_in_path| feature was
336 disabled at compile time}
337
338 *v_gf*
339{Visual}[count]gf Same as "gf", but the highlighted text is used as the
340 name of the file to edit. 'isfname' is ignored.
341 Leading blanks are skipped, otherwise all blanks and
342 special characters are included in the file name.
343 (For {Visual} see |Visual-mode|.)
344 {not in VI}
345
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000346 *gF*
347[count]gF Same as "gf", except if a number follows the file
348 name, then the cursor is positioned on that line in
349 the file. The file name and the number must be
350 separated by a non-filename (see 'isfname') and
351 non-numeric character. White space between the
352 filename, the separator and the number are ignored.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000353 Examples:
354 eval.c:10 ~
355 eval.c @ 20 ~
356 eval.c (30) ~
357 eval.c 40 ~
358
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000359 *v_gF*
360{Visual}[count]gF Same as "v_gf".
361
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362These commands are used to start editing a single file. This means that the
363file is read into the buffer and the current file name is set. The file that
364is opened depends on the current directory, see |:cd|.
365
366See |read-messages| for an explanation of the message that is given after the
367file has been read.
368
369You can use the ":e!" command if you messed up the buffer and want to start
370all over again. The ":e" command is only useful if you have changed the
371current file name.
372
373 *:filename* *{file}*
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000374Besides the things mentioned here, more special items for where a filename is
375expected are mentioned at |cmdline-special|.
376
Bram Moolenaarc236c162008-07-13 17:41:49 +0000377Note for systems other than Unix: When using a command that accepts a single
378file name (like ":edit file") spaces in the file name are allowed, but
379trailing spaces are ignored. This is useful on systems that regularly embed
380spaces in file names (like MS-Windows and the Amiga). Example: The command
381":e Long File Name " will edit the file "Long File Name". When using a
382command that accepts more than one file name (like ":next file1 file2")
383embedded spaces must be escaped with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384
Bram Moolenaar9964e462007-05-05 17:54:07 +0000385 *wildcard* *wildcards*
Bram Moolenaar30b65812012-07-12 22:01:11 +0200386Wildcards in {file} are expanded, but as with file completion, 'wildignore'
387and 'suffixes' apply. Which wildcards are supported depends on the system.
388These are the common ones:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 ? matches one character
Bram Moolenaar02743632005-07-25 20:42:36 +0000390 * matches anything, including nothing
391 ** matches anything, including nothing, recurses into directories
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 [abc] match 'a', 'b' or 'c'
Bram Moolenaar02743632005-07-25 20:42:36 +0000393
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394To avoid the special meaning of the wildcards prepend a backslash. However,
395on MS-Windows the backslash is a path separator and "path\[abc]" is still seen
396as a wildcard when "[" is in the 'isfname' option. A simple way to avoid this
Bram Moolenaar7db8f6f2016-03-29 23:12:46 +0200397is to use "path\[[]abc]", this matches the file "path\[abc]".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398
Bram Moolenaar02743632005-07-25 20:42:36 +0000399 *starstar-wildcard*
400Expanding "**" is possible on Unix, Win32, Mac OS/X and a few other systems.
401This allows searching a directory tree. This goes up to 100 directories deep.
Bram Moolenaar9b451252012-08-15 17:43:31 +0200402Note there are some commands where this works slightly differently, see
Bram Moolenaarc236c162008-07-13 17:41:49 +0000403|file-searching|.
Bram Moolenaar02743632005-07-25 20:42:36 +0000404Example: >
405 :n **/*.txt
406Finds files:
Bram Moolenaar38a55632016-02-15 22:07:32 +0100407 aaa.txt ~
408 subdir/bbb.txt ~
409 a/b/c/d/ccc.txt ~
410When non-wildcard characters are used right before or after "**" these are
411only matched in the top directory. They are not used for directories further
412down in the tree. For example: >
413 :n /usr/inc**/types.h
Bram Moolenaar02743632005-07-25 20:42:36 +0000414Finds files:
Bram Moolenaar38a55632016-02-15 22:07:32 +0100415 /usr/include/types.h ~
416 /usr/include/sys/types.h ~
417 /usr/inc/old/types.h ~
418Note that the path with "/sys" is included because it does not need to match
419"/inc". Thus it's like matching "/usr/inc*/*/*...", not
420"/usr/inc*/inc*/inc*".
421
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 *backtick-expansion* *`-expansion*
Bram Moolenaarfc39ecf2015-08-11 20:34:49 +0200423On Unix and a few other systems you can also use backticks for the file name
424argument, for example: >
425 :next `find . -name ver\\*.c -print`
Bram Moolenaar88774fd2015-08-25 19:52:04 +0200426 :view `ls -t *.patch \| head -n1`
Bram Moolenaarfc39ecf2015-08-11 20:34:49 +0200427The backslashes before the star are required to prevent the shell from
428expanding "ver*.c" prior to execution of the find program. The backslash
429before the shell pipe symbol "|" prevents Vim from parsing it as command
430termination.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431This also works for most other systems, with the restriction that the
432backticks must be around the whole item. It is not possible to have text
433directly before the first or just after the last backtick.
434
Bram Moolenaared203462004-06-16 11:19:22 +0000435 *`=*
Bram Moolenaarfc39ecf2015-08-11 20:34:49 +0200436You can have the backticks expanded as a Vim expression, instead of as an
437external command, by putting an equal sign right after the first backtick,
438e.g.: >
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000439 :e `=tempname()`
440The expression can contain just about anything, thus this can also be used to
Bram Moolenaar30b65812012-07-12 22:01:11 +0200441avoid the special meaning of '"', '|', '%' and '#'. However, 'wildignore'
Bram Moolenaar00154502013-02-13 16:15:55 +0100442does apply like to other wildcards.
Bram Moolenaarfc39ecf2015-08-11 20:34:49 +0200443
Bram Moolenaar88774fd2015-08-25 19:52:04 +0200444Environment variables in the expression are expanded when evaluating the
445expression, thus this works: >
446 :e `=$HOME . '/.vimrc'`
447This does not work, $HOME is inside a string and used literally: >
448 :e `='$HOME' . '/.vimrc'`
Bram Moolenaarfc39ecf2015-08-11 20:34:49 +0200449
Bram Moolenaar30b65812012-07-12 22:01:11 +0200450If the expression returns a string then names are to be separated with line
451breaks. When the result is a |List| then each item is used as a name. Line
452breaks also separate names.
Bram Moolenaarf9132812015-07-21 19:19:13 +0200453Note that such expressions are only supported in places where a filename is
454expected as an argument to an Ex-command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455
456 *++opt* *[++opt]*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000457The [++opt] argument can be used to force the value of 'fileformat',
458'fileencoding' or 'binary' to a value for one command, and to specify the
459behavior for bad characters. The form is: >
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000460 ++{optname}
461Or: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 ++{optname}={value}
463
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000464Where {optname} is one of: *++ff* *++enc* *++bin* *++nobin* *++edit*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 ff or fileformat overrides 'fileformat'
466 enc or encoding overrides 'fileencoding'
467 bin or binary sets 'binary'
468 nobin or nobinary resets 'binary'
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000469 bad specifies behavior for bad characters
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000470 edit for |:read| only: keep option values as if editing
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000471 a file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472
473{value} cannot contain white space. It can be any valid value for these
474options. Examples: >
475 :e ++ff=unix
476This edits the same file again with 'fileformat' set to "unix". >
477
478 :w ++enc=latin1 newfile
479This writes the current buffer to "newfile" in latin1 format.
480
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000481There may be several ++opt arguments, separated by white space. They must all
482appear before any |+cmd| argument.
483
484 *++bad*
485The argument of "++bad=" specifies what happens with characters that can't be
486converted and illegal bytes. It can be one of three things:
487 ++bad=X A single-byte character that replaces each bad character.
488 ++bad=keep Keep bad characters without conversion. Note that this may
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000489 result in illegal bytes in your text!
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000490 ++bad=drop Remove the bad characters.
491
492The default is like "++bad=?": Replace each bad character with a question
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100493mark. In some places an inverted question mark is used (0xBF).
494
495Note that not all commands use the ++bad argument, even though they do not
496give an error when you add it. E.g. |:write|.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000497
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498Note that when reading, the 'fileformat' and 'fileencoding' options will be
499set to the used format. When writing this doesn't happen, thus a next write
500will use the old value of the option. Same for the 'binary' option.
501
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502
503 *+cmd* *[+cmd]*
504The [+cmd] argument can be used to position the cursor in the newly opened
505file, or execute any other command:
506 + Start at the last line.
507 +{num} Start at line {num}.
508 +/{pat} Start at first line containing {pat}.
509 +{command} Execute {command} after opening the new file.
510 {command} is any Ex command.
511To include a white space in the {pat} or {command}, precede it with a
512backslash. Double the number of backslashes. >
513 :edit +/The\ book file
514 :edit +/dir\ dirname\\ file
515 :edit +set\ dir=c:\\\\temp file
516Note that in the last example the number of backslashes is halved twice: Once
517for the "+cmd" argument and once for the ":set" command.
518
519 *file-formats*
520The 'fileformat' option sets the <EOL> style for a file:
521'fileformat' characters name ~
522 "dos" <CR><NL> or <NL> DOS format *DOS-format*
523 "unix" <NL> Unix format *Unix-format*
524 "mac" <CR> Mac format *Mac-format*
525Previously 'textmode' was used. It is obsolete now.
526
527When reading a file, the mentioned characters are interpreted as the <EOL>.
528In DOS format (default for MS-DOS, OS/2 and Win32), <CR><NL> and <NL> are both
529interpreted as the <EOL>. Note that when writing the file in DOS format,
530<CR> characters will be added for each single <NL>. Also see |file-read|.
531
532When writing a file, the mentioned characters are used for <EOL>. For DOS
533format <CR><NL> is used. Also see |DOS-format-write|.
534
535You can read a file in DOS format and write it in Unix format. This will
536replace all <CR><NL> pairs by <NL> (assuming 'fileformats' includes "dos"): >
537 :e file
538 :set fileformat=unix
539 :w
540If you read a file in Unix format and write with DOS format, all <NL>
541characters will be replaced with <CR><NL> (assuming 'fileformats' includes
542"unix"): >
543 :e file
544 :set fileformat=dos
545 :w
546
547If you start editing a new file and the 'fileformats' option is not empty
548(which is the default), Vim will try to detect whether the lines in the file
549are separated by the specified formats. When set to "unix,dos", Vim will
550check for lines with a single <NL> (as used on Unix and Amiga) or by a <CR>
551<NL> pair (MS-DOS). Only when ALL lines end in <CR><NL>, 'fileformat' is set
552to "dos", otherwise it is set to "unix". When 'fileformats' includes "mac",
553and no <NL> characters are found in the file, 'fileformat' is set to "mac".
554
555If the 'fileformat' option is set to "dos" on non-MS-DOS systems the message
556"[dos format]" is shown to remind you that something unusual is happening. On
557MS-DOS systems you get the message "[unix format]" if 'fileformat' is set to
558"unix". On all systems but the Macintosh you get the message "[mac format]"
559if 'fileformat' is set to "mac".
560
561If the 'fileformats' option is empty and DOS format is used, but while reading
562a file some lines did not end in <CR><NL>, "[CR missing]" will be included in
563the file message.
564If the 'fileformats' option is empty and Mac format is used, but while reading
565a file a <NL> was found, "[NL missing]" will be included in the file message.
566
567If the new file does not exist, the 'fileformat' of the current buffer is used
568when 'fileformats' is empty. Otherwise the first format from 'fileformats' is
569used for the new file.
570
571Before editing binary, executable or Vim script files you should set the
572'binary' option. A simple way to do this is by starting Vim with the "-b"
573option. This will avoid the use of 'fileformat'. Without this you risk that
574single <NL> characters are unexpectedly replaced with <CR><NL>.
575
576You can encrypt files that are written by setting the 'key' option. This
577provides some security against others reading your files. |encryption|
578
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580==============================================================================
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005813. The argument list *argument-list* *arglist*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582
583If you give more than one file name when starting Vim, this list is remembered
584as the argument list. You can jump to each file in this list.
585
586Do not confuse this with the buffer list, which you can see with the
587|:buffers| command. The argument list was already present in Vi, the buffer
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000588list is new in Vim. Every file name in the argument list will also be present
589in the buffer list (unless it was deleted with |:bdel| or |:bwipe|). But it's
590common that names in the buffer list are not in the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591
592This subject is introduced in section |07.2| of the user manual.
593
594There is one global argument list, which is used for all windows by default.
595It is possible to create a new argument list local to a window, see
596|:arglocal|.
597
598You can use the argument list with the following commands, and with the
599expression functions |argc()| and |argv()|. These all work on the argument
600list of the current window.
601
602 *:ar* *:args*
603:ar[gs] Print the argument list, with the current file in
604 square brackets.
605
606:ar[gs] [++opt] [+cmd] {arglist} *:args_f*
607 Define {arglist} as the new argument list and edit
608 the first one. This fails when changes have been made
609 and Vim does not want to |abandon| the current buffer.
610 Also see |++opt| and |+cmd|.
611 {Vi: no ++opt}
612
613:ar[gs]! [++opt] [+cmd] {arglist} *:args_f!*
614 Define {arglist} as the new argument list and edit
615 the first one. Discard any changes to the current
616 buffer.
617 Also see |++opt| and |+cmd|.
618 {Vi: no ++opt}
619
620:[count]arge[dit][!] [++opt] [+cmd] {name} *:arge* *:argedit*
621 Add {name} to the argument list and edit it.
622 When {name} already exists in the argument list, this
623 entry is edited.
624 This is like using |:argadd| and then |:edit|.
625 Note that only one file name is allowed, and spaces
626 inside the file name are allowed, like with |:edit|.
627 [count] is used like with |:argadd|.
628 [!] is required if the current file cannot be
629 |abandon|ed.
630 Also see |++opt| and |+cmd|.
631 {not in Vi}
632
633:[count]arga[dd] {name} .. *:arga* *:argadd* *E479*
Bram Moolenaar91e15e12014-09-19 22:38:48 +0200634:[count]arga[dd]
635 Add the {name}s to the argument list. When {name} is
Bram Moolenaared32d942014-12-06 23:33:00 +0100636 omitted add the current buffer name to the argument
Bram Moolenaar91e15e12014-09-19 22:38:48 +0200637 list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 If [count] is omitted, the {name}s are added just
639 after the current entry in the argument list.
640 Otherwise they are added after the [count]'th file.
641 If the argument list is "a b c", and "b" is the
642 current argument, then these commands result in:
643 command new argument list ~
644 :argadd x a b x c
645 :0argadd x x a b c
646 :1argadd x a x b c
Bram Moolenaared32d942014-12-06 23:33:00 +0100647 :$argadd x a b c x
Bram Moolenaar85084ef2016-01-17 22:26:33 +0100648 And after the last one:
Bram Moolenaared32d942014-12-06 23:33:00 +0100649 :+2argadd y a b c x y
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 There is no check for duplicates, it is possible to
651 add a file to the argument list twice.
652 The currently edited file is not changed.
653 {not in Vi} {not available when compiled without the
654 |+listcmds| feature}
655 Note: you can also use this method: >
656 :args ## x
657< This will add the "x" item and sort the new list.
658
659:argd[elete] {pattern} .. *:argd* *:argdelete* *E480*
660 Delete files from the argument list that match the
661 {pattern}s. {pattern} is used like a file pattern,
662 see |file-pattern|. "%" can be used to delete the
663 current entry.
664 This command keeps the currently edited file, also
665 when it's deleted from the argument list.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000666 Example: >
667 :argdel *.obj
668< {not in Vi} {not available when compiled without the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 |+listcmds| feature}
670
Bram Moolenaared32d942014-12-06 23:33:00 +0100671:[range]argd[elete] Delete the {range} files from the argument list.
672 Example: >
673 :10,$argdel
674< Deletes arguments 10 and further, keeping 1-9. >
675 :$argd
676< Deletes just the last one. >
677 :argd
678 :.argd
679< Deletes the current argument. >
680 :%argd
681< Removes all the files from the arglist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 When the last number in the range is too high, up to
Bram Moolenaared32d942014-12-06 23:33:00 +0100683 the last argument is deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {not in Vi} {not available when compiled without the
685 |+listcmds| feature}
686
687 *:argu* *:argument*
688:[count]argu[ment] [count] [++opt] [+cmd]
689 Edit file [count] in the argument list. When [count]
690 is omitted the current entry is used. This fails
691 when changes have been made and Vim does not want to
692 |abandon| the current buffer.
693 Also see |++opt| and |+cmd|.
694 {not in Vi} {not available when compiled without the
695 |+listcmds| feature}
696
697:[count]argu[ment]! [count] [++opt] [+cmd]
698 Edit file [count] in the argument list, discard any
699 changes to the current buffer. When [count] is
700 omitted the current entry is used.
701 Also see |++opt| and |+cmd|.
702 {not in Vi} {not available when compiled without the
703 |+listcmds| feature}
704
705:[count]n[ext] [++opt] [+cmd] *:n* *:ne* *:next* *E165* *E163*
706 Edit [count] next file. This fails when changes have
707 been made and Vim does not want to |abandon| the
708 current buffer. Also see |++opt| and |+cmd|. {Vi: no
709 count or ++opt}.
710
711:[count]n[ext]! [++opt] [+cmd]
712 Edit [count] next file, discard any changes to the
713 buffer. Also see |++opt| and |+cmd|. {Vi: no count
714 or ++opt}.
715
716:n[ext] [++opt] [+cmd] {arglist} *:next_f*
717 Same as |:args_f|.
718
719:n[ext]! [++opt] [+cmd] {arglist}
720 Same as |:args_f!|.
721
722:[count]N[ext] [count] [++opt] [+cmd] *:Next* *:N* *E164*
723 Edit [count] previous file in argument list. This
724 fails when changes have been made and Vim does not
725 want to |abandon| the current buffer.
726 Also see |++opt| and |+cmd|. {Vi: no count or ++opt}.
727
728:[count]N[ext]! [count] [++opt] [+cmd]
729 Edit [count] previous file in argument list. Discard
730 any changes to the buffer. Also see |++opt| and
731 |+cmd|. {Vi: no count or ++opt}.
732
733:[count]prev[ious] [count] [++opt] [+cmd] *:prev* *:previous*
734 Same as :Next. Also see |++opt| and |+cmd|. {Vi:
735 only in some versions}
736
737 *:rew* *:rewind*
738:rew[ind] [++opt] [+cmd]
739 Start editing the first file in the argument list.
740 This fails when changes have been made and Vim does
741 not want to |abandon| the current buffer.
742 Also see |++opt| and |+cmd|. {Vi: no ++opt}
743
744:rew[ind]! [++opt] [+cmd]
745 Start editing the first file in the argument list.
746 Discard any changes to the buffer. Also see |++opt|
747 and |+cmd|. {Vi: no ++opt}
748
749 *:fir* *:first*
750:fir[st][!] [++opt] [+cmd]
751 Other name for ":rewind". {not in Vi}
752
753 *:la* *:last*
754:la[st] [++opt] [+cmd]
755 Start editing the last file in the argument list.
756 This fails when changes have been made and Vim does
757 not want to |abandon| the current buffer.
758 Also see |++opt| and |+cmd|. {not in Vi}
759
760:la[st]! [++opt] [+cmd]
761 Start editing the last file in the argument list.
762 Discard any changes to the buffer. Also see |++opt|
763 and |+cmd|. {not in Vi}
764
765 *:wn* *:wnext*
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000766:[count]wn[ext] [++opt]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 Write current file and start editing the [count]
768 next file. Also see |++opt| and |+cmd|. {not in Vi}
769
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000770:[count]wn[ext] [++opt] {file}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 Write current file to {file} and start editing the
772 [count] next file, unless {file} already exists and
773 the 'writeany' option is off. Also see |++opt| and
774 |+cmd|. {not in Vi}
775
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000776:[count]wn[ext]! [++opt] {file}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 Write current file to {file} and start editing the
778 [count] next file. Also see |++opt| and |+cmd|. {not
779 in Vi}
780
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000781:[count]wN[ext][!] [++opt] [file] *:wN* *:wNext*
782:[count]wp[revious][!] [++opt] [file] *:wp* *:wprevious*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 Same as :wnext, but go to previous file instead of
784 next. {not in Vi}
785
786The [count] in the commands above defaults to one. For some commands it is
787possible to use two counts. The last one (rightmost one) is used.
788
789If no [+cmd] argument is present, the cursor is positioned at the last known
790cursor position for the file. If 'startofline' is set, the cursor will be
791positioned at the first non-blank in the line, otherwise the last know column
792is used. If there is no last known cursor position the cursor will be in the
793first line (the last line in Ex mode).
794
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000795 *{arglist}*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796The wildcards in the argument list are expanded and the file names are sorted.
797Thus you can use the command "vim *.c" to edit all the C files. From within
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000798Vim the command ":n *.c" does the same.
799
800White space is used to separate file names. Put a backslash before a space or
Bram Moolenaar9e368db2007-05-12 13:25:01 +0000801tab to include it in a file name. E.g., to edit the single file "foo bar": >
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000802 :next foo\ bar
803
804On Unix and a few other systems you can also use backticks, for example: >
805 :next `find . -name \\*.c -print`
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806The backslashes before the star are required to prevent "*.c" to be expanded
807by the shell before executing the find program.
808
809 *arglist-position*
810When there is an argument list you can see which file you are editing in the
811title of the window (if there is one and 'title' is on) and with the file
812message you get with the "CTRL-G" command. You will see something like
813 (file 4 of 11)
814If 'shortmess' contains 'f' it will be
815 (4 of 11)
816If you are not really editing the file at the current position in the argument
817list it will be
818 (file (4) of 11)
819This means that you are position 4 in the argument list, but not editing the
820fourth file in the argument list. This happens when you do ":e file".
821
822
823LOCAL ARGUMENT LIST
824
825{not in Vi}
Bram Moolenaardb84e452010-08-15 13:50:43 +0200826{not available when compiled without the |+windows| or |+listcmds| features}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827
828 *:arglocal*
829:argl[ocal] Make a local copy of the global argument list.
830 Doesn't start editing another file.
831
832:argl[ocal][!] [++opt] [+cmd] {arglist}
833 Define a new argument list, which is local to the
834 current window. Works like |:args_f| otherwise.
835
836 *:argglobal*
837:argg[lobal] Use the global argument list for the current window.
838 Doesn't start editing another file.
839
840:argg[lobal][!] [++opt] [+cmd] {arglist}
841 Use the global argument list for the current window.
842 Define a new global argument list like |:args_f|.
843 All windows using the global argument list will see
844 this new list.
845
846There can be several argument lists. They can be shared between windows.
847When they are shared, changing the argument list in one window will also
848change it in the other window.
849
850When a window is split the new window inherits the argument list from the
851current window. The two windows then share this list, until one of them uses
852|:arglocal| or |:argglobal| to use another argument list.
853
854
855USING THE ARGUMENT LIST
856
857 *:argdo*
Bram Moolenaara162bc52015-01-07 16:54:21 +0100858:[range]argdo[!] {cmd} Execute {cmd} for each file in the argument list or
859 if [range] is specified only for arguments in that
860 range. It works like doing this: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 :rewind
862 :{cmd}
863 :next
864 :{cmd}
865 etc.
866< When the current file can't be |abandon|ed and the [!]
867 is not present, the command fails.
868 When an error is detected on one file, further files
869 in the argument list will not be visited.
870 The last file in the argument list (or where an error
871 occurred) becomes the current file.
872 {cmd} can contain '|' to concatenate several commands.
873 {cmd} must not change the argument list.
874 Note: While this command is executing, the Syntax
875 autocommand event is disabled by adding it to
876 'eventignore'. This considerably speeds up editing
877 each file.
878 {not in Vi} {not available when compiled without the
879 |+listcmds| feature}
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200880 Also see |:windo|, |:tabdo|, |:bufdo|, |:cdo|, |:ldo|,
881 |:cfdo| and |:lfdo|
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882
883Example: >
884 :args *.c
885 :argdo set ff=unix | update
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100886This sets the 'fileformat' option to "unix" and writes the file if it is now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887changed. This is done for all *.c files.
888
889Example: >
890 :args *.[ch]
891 :argdo %s/\<my_foo\>/My_Foo/ge | update
892This changes the word "my_foo" to "My_Foo" in all *.c and *.h files. The "e"
893flag is used for the ":substitute" command to avoid an error for files where
894"my_foo" isn't used. ":update" writes the file only if changes were made.
895
896==============================================================================
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008974. Writing *writing* *save-file*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898
899Note: When the 'write' option is off, you are not able to write any file.
900
901 *:w* *:write*
902 *E502* *E503* *E504* *E505*
Bram Moolenaar9964e462007-05-05 17:54:07 +0000903 *E512* *E514* *E667* *E796*
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000904:w[rite] [++opt] Write the whole buffer to the current file. This is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 the normal way to save changes to a file. It fails
906 when the 'readonly' option is set or when there is
907 another reason why the file can't be written.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000908 For ++opt see |++opt|, but only ++bin, ++nobin, ++ff
909 and ++enc are effective.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000911:w[rite]! [++opt] Like ":write", but forcefully write when 'readonly' is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 set or there is another reason why writing was
913 refused.
914 Note: This may change the permission and ownership of
915 the file and break (symbolic) links. Add the 'W' flag
916 to 'cpoptions' to avoid this.
917
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000918:[range]w[rite][!] [++opt]
919 Write the specified lines to the current file. This
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 is unusual, because the file will not contain all
921 lines in the buffer.
922
923 *:w_f* *:write_f*
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000924:[range]w[rite] [++opt] {file}
925 Write the specified lines to {file}, unless it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 already exists and the 'writeany' option is off.
927
928 *:w!*
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000929:[range]w[rite]! [++opt] {file}
930 Write the specified lines to {file}. Overwrite an
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 existing file.
932
933 *:w_a* *:write_a* *E494*
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000934:[range]w[rite][!] [++opt] >>
935 Append the specified lines to the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000937:[range]w[rite][!] [++opt] >> {file}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 Append the specified lines to {file}. '!' forces the
939 write even if file does not exist.
940
941 *:w_c* *:write_c*
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000942:[range]w[rite] [++opt] !{cmd}
943 Execute {cmd} with [range] lines as standard input
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 (note the space in front of the '!'). {cmd} is
945 executed like with ":!{cmd}", any '!' is replaced with
946 the previous command |:!|.
947
Bram Moolenaar5c4e21c2004-10-12 19:54:52 +0000948The default [range] for the ":w" command is the whole buffer (1,$). If you
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000949write the whole buffer, it is no longer considered changed. When you
950write it to a different file with ":w somefile" it depends on the "+" flag in
951'cpoptions'. When included, the write command will reset the 'modified' flag,
952even though the buffer itself may still be different from its file.
Bram Moolenaar5c4e21c2004-10-12 19:54:52 +0000953
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954If a file name is given with ":w" it becomes the alternate file. This can be
955used, for example, when the write fails and you want to try again later with
956":w #". This can be switched off by removing the 'A' flag from the
957'cpoptions' option.
958
959 *:sav* *:saveas*
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000960:sav[eas][!] [++opt] {file}
961 Save the current buffer under the name {file} and set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 the filename of the current buffer to {file}. The
963 previous name is used for the alternate file name.
964 The [!] is needed to overwrite an existing file.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000965 When 'filetype' is empty filetype detection is done
966 with the new name, before the file is written.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000967 When the write was successful 'readonly' is reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 {not in Vi}
969
970 *:up* *:update*
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000971:[range]up[date][!] [++opt] [>>] [file]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 Like ":write", but only write when the buffer has been
973 modified. {not in Vi}
974
975
976WRITING WITH MULTIPLE BUFFERS *buffer-write*
977
978 *:wa* *:wall*
979:wa[ll] Write all changed buffers. Buffers without a file
980 name or which are readonly are not written. {not in
981 Vi}
982
983:wa[ll]! Write all changed buffers, even the ones that are
984 readonly. Buffers without a file name are not
985 written. {not in Vi}
986
987
988Vim will warn you if you try to overwrite a file that has been changed
989elsewhere. See |timestamp|.
990
991 *backup* *E207* *E506* *E507* *E508* *E509* *E510*
992If you write to an existing file (but do not append) while the 'backup',
993'writebackup' or 'patchmode' option is on, a backup of the original file is
994made. The file is either copied or renamed (see 'backupcopy'). After the
995file has been successfully written and when the 'writebackup' option is on and
996the 'backup' option is off, the backup file is deleted. When the 'patchmode'
997option is on the backup file may be renamed.
998
999 *backup-table*
1000'backup' 'writebackup' action ~
1001 off off no backup made
1002 off on backup current file, deleted afterwards (default)
1003 on off delete old backup, backup current file
1004 on on delete old backup, backup current file
1005
1006When the 'backupskip' pattern matches with the name of the file which is
1007written, no backup file is made. The values of 'backup' and 'writebackup' are
1008ignored then.
1009
1010When the 'backup' option is on, an old backup file (with the same name as the
1011new backup file) will be deleted. If 'backup' is not set, but 'writebackup'
1012is set, an existing backup file will not be deleted. The backup file that is
1013made while the file is being written will have a different name.
1014
1015On some filesystems it's possible that in a crash you lose both the backup and
1016the newly written file (it might be there but contain bogus data). In that
1017case try recovery, because the swap file is synced to disk and might still be
1018there. |:recover|
1019
Bram Moolenaare0fa3742016-02-20 15:47:01 +01001020The directories given with the 'backupdir' option are used to put the backup
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021file in. (default: same directory as the written file).
1022
1023Whether the backup is a new file, which is a copy of the original file, or the
1024original file renamed depends on the 'backupcopy' option. See there for an
1025explanation of when the copy is made and when the file is renamed.
1026
1027If the creation of a backup file fails, the write is not done. If you want
1028to write anyway add a '!' to the command.
1029
Bram Moolenaard58e9292011-02-09 17:07:58 +01001030 *write-permissions*
1031When writing a new file the permissions are read-write. For unix the mask is
10320666 with additionally umask applied. When writing a file that was read Vim
1033will preserve the permissions, but clear the s-bit.
1034
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 *write-readonly*
1036When the 'cpoptions' option contains 'W', Vim will refuse to overwrite a
1037readonly file. When 'W' is not present, ":w!" will overwrite a readonly file,
1038if the system allows it (the directory must be writable).
1039
1040 *write-fail*
1041If the writing of the new file fails, you have to be careful not to lose
1042your changes AND the original file. If there is no backup file and writing
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001043the new file failed, you have already lost the original file! DON'T EXIT VIM
1044UNTIL YOU WRITE OUT THE FILE! If a backup was made, it is put back in place
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045of the original file (if possible). If you exit Vim, and lose the changes
1046you made, the original file will mostly still be there. If putting back the
1047original file fails, there will be an error message telling you that you
1048lost the original file.
1049
1050 *DOS-format-write*
1051If the 'fileformat' is "dos", <CR> <NL> is used for <EOL>. This is default
1052for MS-DOS, Win32 and OS/2. On other systems the message "[dos format]" is
1053shown to remind you that an unusual <EOL> was used.
1054 *Unix-format-write*
1055If the 'fileformat' is "unix", <NL> is used for <EOL>. On MS-DOS, Win32 and
1056OS/2 the message "[unix format]" is shown.
1057 *Mac-format-write*
1058If the 'fileformat' is "mac", <CR> is used for <EOL>. On non-Mac systems the
1059message "[mac format]" is shown.
1060
1061See also |file-formats| and the 'fileformat' and 'fileformats' options.
1062
1063 *ACL*
1064ACL stands for Access Control List. It is an advanced way to control access
1065rights for a file. It is used on new MS-Windows and Unix systems, but only
1066when the filesystem supports it.
1067 Vim attempts to preserve the ACL info when writing a file. The backup file
1068will get the ACL info of the original file.
1069 The ACL info is also used to check if a file is read-only (when opening the
1070file).
1071
1072 *read-only-share*
1073When MS-Windows shares a drive on the network it can be marked as read-only.
1074This means that even if the file read-only attribute is absent, and the ACL
1075settings on NT network shared drives allow writing to the file, you can still
1076not write to the file. Vim on Win32 platforms will detect read-only network
1077drives and will mark the file as read-only. You will not be able to override
1078it with |:write|.
1079
1080 *write-device*
1081When the file name is actually a device name, Vim will not make a backup (that
1082would be impossible). You need to use "!", since the device already exists.
1083Example for Unix: >
1084 :w! /dev/lpt0
1085and for MS-DOS or MS-Windows: >
1086 :w! lpt0
1087For Unix a device is detected when the name doesn't refer to a normal file or
1088a directory. A fifo or named pipe also looks like a device to Vim.
1089For MS-DOS and MS-Windows the device is detected by its name:
1090 AUX
1091 CON
1092 CLOCK$
1093 NUL
1094 PRN
1095 COMn n=1,2,3... etc
1096 LPTn n=1,2,3... etc
1097The names can be in upper- or lowercase.
1098
1099==============================================================================
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011005. Writing and quitting *write-quit*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101
1102 *:q* *:quit*
1103:q[uit] Quit the current window. Quit Vim if this is the last
1104 window. This fails when changes have been made and
1105 Vim refuses to |abandon| the current buffer, and when
1106 the last file in the argument list has not been
1107 edited.
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00001108 If there are other tab pages and quitting the last
1109 window in the current tab page the current tab page is
1110 closed |tab-page|.
Bram Moolenaar30b65812012-07-12 22:01:11 +02001111 Triggers the |QuitPre| autocommand event.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112
1113:conf[irm] q[uit] Quit, but give prompt when changes have been made, or
1114 the last file in the argument list has not been
1115 edited. See |:confirm| and 'confirm'. {not in Vi}
1116
Bram Moolenaarfa735342016-01-03 22:14:44 +01001117:q[uit]! Quit without writing, also when the current buffer has
Bram Moolenaar09521312016-08-12 22:54:35 +02001118 changes. The buffer is unloaded, also when it has
1119 'hidden' set.
1120 If this is the last window and there is a modified
1121 hidden buffer, the current buffer is abandoned and the
1122 first changed hidden buffer becomes the current
1123 buffer.
Bram Moolenaar2f3b5102014-11-19 18:54:17 +01001124 Use ":qall!" to exit always.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125
1126:cq[uit] Quit always, without writing, and return an error
1127 code. See |:cq|. Used for Manx's QuickFix mode (see
1128 |quickfix|). {not in Vi}
1129
1130 *:wq*
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001131:wq [++opt] Write the current file and quit. Writing fails when
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 the file is read-only or the buffer does not have a
1133 name. Quitting fails when the last file in the
1134 argument list has not been edited.
1135
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001136:wq! [++opt] Write the current file and quit. Writing fails when
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 the current buffer does not have a name.
1138
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001139:wq [++opt] {file} Write to {file} and quit. Quitting fails when the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 last file in the argument list has not been edited.
1141
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001142:wq! [++opt] {file} Write to {file} and quit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001144:[range]wq[!] [++opt] [file]
1145 Same as above, but only write the lines in [range].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146
1147 *:x* *:xit*
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001148:[range]x[it][!] [++opt] [file]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 Like ":wq", but write only when changes have been
1150 made.
1151 When 'hidden' is set and there are more windows, the
1152 current buffer becomes hidden, after writing the file.
1153
1154 *:exi* *:exit*
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001155:[range]exi[t][!] [++opt] [file]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 Same as :xit.
1157
1158 *ZZ*
1159ZZ Write current file, if modified, and quit (same as
1160 ":x"). (Note: If there are several windows for the
1161 current file, the file is written if it was modified
1162 and the window is closed).
1163
1164 *ZQ*
1165ZQ Quit without checking for changes (same as ":q!").
1166 {not in Vi}
1167
1168MULTIPLE WINDOWS AND BUFFERS *window-exit*
1169
1170 *:qa* *:qall*
1171:qa[ll] Exit Vim, unless there are some buffers which have been
1172 changed. (Use ":bmod" to go to the next modified buffer).
1173 When 'autowriteall' is set all changed buffers will be
1174 written, like |:wqall|. {not in Vi}
1175
1176:conf[irm] qa[ll]
1177 Exit Vim. Bring up a prompt when some buffers have been
1178 changed. See |:confirm|. {not in Vi}
1179
1180:qa[ll]! Exit Vim. Any changes to buffers are lost. {not in Vi}
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00001181 Also see |:cquit|, it does the same but exits with a non-zero
1182 value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183
1184 *:quita* *:quitall*
1185:quita[ll][!] Same as ":qall". {not in Vi}
1186
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001187:wqa[ll] [++opt] *:wqa* *:wqall* *:xa* *:xall*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188:xa[ll] Write all changed buffers and exit Vim. If there are buffers
1189 without a file name, which are readonly or which cannot be
1190 written for another reason, Vim will not quit. {not in Vi}
1191
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001192:conf[irm] wqa[ll] [++opt]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193:conf[irm] xa[ll]
1194 Write all changed buffers and exit Vim. Bring up a prompt
1195 when some buffers are readonly or cannot be written for
1196 another reason. See |:confirm|. {not in Vi}
1197
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001198:wqa[ll]! [++opt]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199:xa[ll]! Write all changed buffers, even the ones that are readonly,
1200 and exit Vim. If there are buffers without a file name or
1201 which cannot be written for another reason, Vim will not quit.
1202 {not in Vi}
1203
1204==============================================================================
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012056. Dialogs *edit-dialogs*
1206
1207 *:confirm* *:conf*
1208:conf[irm] {command} Execute {command}, and use a dialog when an
1209 operation has to be confirmed. Can be used on the
Bram Moolenaar61d35bd2012-03-28 20:51:51 +02001210 |:q|, |:qa| and |:w| commands (the latter to override
1211 a read-only setting), and any other command that can
1212 fail in such a way, such as |:only|, |:buffer|,
1213 |:bdelete|, etc.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001214
1215Examples: >
1216 :confirm w foo
1217< Will ask for confirmation when "foo" already exists. >
1218 :confirm q
1219< Will ask for confirmation when there are changes. >
1220 :confirm qa
1221< If any modified, unsaved buffers exist, you will be prompted to save
1222 or abandon each one. There are also choices to "save all" or "abandon
1223 all".
1224
1225If you want to always use ":confirm", set the 'confirm' option.
1226
Bram Moolenaaraa3b15d2016-04-21 08:53:19 +02001227 *:browse* *:bro* *E338* *E614* *E615* *E616*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001228:bro[wse] {command} Open a file selection dialog for an argument to
1229 {command}. At present this works for |:e|, |:w|,
Bram Moolenaar9028b102010-07-11 16:58:51 +02001230 |:wall|, |:wq|, |:wqall|, |:x|, |:xall|, |:exit|,
1231 |:view|, |:sview|, |:r|, |:saveas|, |:sp|, |:mkexrc|,
1232 |:mkvimrc|, |:mksession|, |:mkview|, |:split|,
1233 |:vsplit|, |:tabe|, |:tabnew|, |:cfile|, |:cgetfile|,
1234 |:caddfile|, |:lfile|, |:lgetfile|, |:laddfile|,
1235 |:diffsplit|, |:diffpatch|, |:open|, |:pedit|,
1236 |:redir|, |:source|, |:update|, |:visual|, |:vsplit|,
1237 and |:qall| if 'confirm' is set.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001238 {only in Win32, Athena, Motif, GTK and Mac GUI}
1239 When ":browse" is not possible you get an error
1240 message. If the |+browse| feature is missing or the
1241 {command} doesn't support browsing, the {command} is
1242 executed without a dialog.
1243 ":browse set" works like |:options|.
Bram Moolenaar9028b102010-07-11 16:58:51 +02001244 See also |:oldfiles| for ":browse oldfiles".
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001245
1246The syntax is best shown via some examples: >
1247 :browse e $vim/foo
1248< Open the browser in the $vim/foo directory, and edit the
1249 file chosen. >
1250 :browse e
1251< Open the browser in the directory specified with 'browsedir',
1252 and edit the file chosen. >
1253 :browse w
1254< Open the browser in the directory of the current buffer,
1255 with the current buffer filename as default, and save the
1256 buffer under the filename chosen. >
1257 :browse w C:/bar
1258< Open the browser in the C:/bar directory, with the current
1259 buffer filename as default, and save the buffer under the
1260 filename chosen.
1261Also see the |'browsedir'| option.
1262For versions of Vim where browsing is not supported, the command is executed
1263unmodified.
1264
1265 *browsefilter*
Bram Moolenaar30b65812012-07-12 22:01:11 +02001266For MS Windows and GTK, you can modify the filters that are used in the browse
1267dialog. By setting the g:browsefilter or b:browsefilter variables, you can
1268change the filters globally or locally to the buffer. The variable is set to
1269a string in the format "{filter label}\t{pattern};{pattern}\n" where {filter
1270label} is the text that appears in the "Files of Type" comboBox, and {pattern}
1271is the pattern which filters the filenames. Several patterns can be given,
1272separated by ';'.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001273
1274For Motif the same format is used, but only the very first pattern is actually
1275used (Motif only offers one pattern, but you can edit it).
1276
1277For example, to have only Vim files in the dialog, you could use the following
1278command: >
1279
Bram Moolenaar30b65812012-07-12 22:01:11 +02001280 let g:browsefilter = "Vim Scripts\t*.vim\nVim Startup Files\t*vimrc\n"
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001281
1282You can override the filter setting on a per-buffer basis by setting the
1283b:browsefilter variable. You would most likely set b:browsefilter in a
1284filetype plugin, so that the browse dialog would contain entries related to
1285the type of file you are currently editing. Disadvantage: This makes it
1286difficult to start editing a file of a different type. To overcome this, you
1287may want to add "All Files\t*.*\n" as the final filter, so that the user can
1288still access any desired file.
1289
Bram Moolenaar30b65812012-07-12 22:01:11 +02001290To avoid setting browsefilter when Vim does not actually support it, you can
1291use has("browsefilter"): >
1292
1293 if has("browsefilter")
1294 let g:browsefilter = "whatever"
1295 endif
1296
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001297==============================================================================
12987. The current directory *current-directory*
1299
1300You may use the |:cd| and |:lcd| commands to change to another directory, so
1301you will not have to type that directory name in front of the file names. It
1302also makes a difference for executing external commands, e.g. ":!ls".
1303
Bram Moolenaardf177f62005-02-22 08:39:57 +00001304Changing directory fails when the current buffer is modified, the '.' flag is
1305present in 'cpoptions' and "!" is not used in the command.
1306
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001307 *:cd* *E747* *E472*
Bram Moolenaardf177f62005-02-22 08:39:57 +00001308:cd[!] On non-Unix systems: Print the current directory
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001309 name. On Unix systems: Change the current directory
1310 to the home directory. Use |:pwd| to print the
1311 current directory on all systems.
1312
Bram Moolenaardf177f62005-02-22 08:39:57 +00001313:cd[!] {path} Change the current directory to {path}.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001314 If {path} is relative, it is searched for in the
1315 directories listed in |'cdpath'|.
1316 Does not change the meaning of an already opened file,
1317 because its full path name is remembered. Files from
1318 the |arglist| may change though!
1319 On MS-DOS this also changes the active drive.
1320 To change to the directory of the current file: >
1321 :cd %:h
1322<
1323 *:cd-* *E186*
Bram Moolenaardf177f62005-02-22 08:39:57 +00001324:cd[!] - Change to the previous current directory (before the
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001325 previous ":cd {path}" command). {not in Vi}
1326
1327 *:chd* *:chdir*
Bram Moolenaardf177f62005-02-22 08:39:57 +00001328:chd[ir][!] [path] Same as |:cd|.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001329
1330 *:lc* *:lcd*
Bram Moolenaardf177f62005-02-22 08:39:57 +00001331:lc[d][!] {path} Like |:cd|, but only set the current directory for the
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001332 current window. The current directory for other
1333 windows is not changed. {not in Vi}
1334
1335 *:lch* *:lchdir*
Bram Moolenaardf177f62005-02-22 08:39:57 +00001336:lch[dir][!] Same as |:lcd|. {not in Vi}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001337
1338 *:pw* *:pwd* *E187*
1339:pw[d] Print the current directory name. {Vi: no pwd}
1340 Also see |getcwd()|.
1341
1342So long as no |:lcd| command has been used, all windows share the same current
1343directory. Using a command to jump to another window doesn't change anything
1344for the current directory.
1345When a |:lcd| command has been used for a window, the specified directory
1346becomes the current directory for that window. Windows where the |:lcd|
1347command has not been used stick to the global current directory. When jumping
1348to another window the current directory will become the last specified local
1349current directory. If none was specified, the global current directory is
1350used.
1351When a |:cd| command is used, the current window will lose his local current
1352directory and will use the global current directory from now on.
1353
1354After using |:cd| the full path name will be used for reading and writing
1355files. On some networked file systems this may cause problems. The result of
1356using the full path name is that the file names currently in use will remain
1357referring to the same file. Example: If you have a file a:test and a
1358directory a:vim the commands ":e test" ":cd vim" ":w" will overwrite the file
1359a:test and not write a:vim/test. But if you do ":w test" the file a:vim/test
1360will be written, because you gave a new file name and did not refer to a
1361filename before the ":cd".
1362
1363==============================================================================
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648. Editing binary files *edit-binary*
1365
1366Although Vim was made to edit text files, it is possible to edit binary
1367files. The |-b| Vim argument (b for binary) makes Vim do file I/O in binary
1368mode, and sets some options for editing binary files ('binary' on, 'textwidth'
1369to 0, 'modeline' off, 'expandtab' off). Setting the 'binary' option has the
1370same effect. Don't forget to do this before reading the file.
1371
1372There are a few things to remember when editing binary files:
1373- When editing executable files the number of characters must not change.
1374 Use only the "R" or "r" command to change text. Do not delete characters
1375 with "x" or by backspacing.
1376- Set the 'textwidth' option to 0. Otherwise lines will unexpectedly be
1377 split in two.
1378- When there are not many <EOL>s, the lines will become very long. If you
1379 want to edit a line that does not fit on the screen reset the 'wrap' option.
1380 Horizontal scrolling is used then. If a line becomes too long (more than
1381 about 32767 characters on the Amiga, much more on 32-bit systems, see
1382 |limits|) you cannot edit that line. The line will be split when reading
1383 the file. It is also possible that you get an "out of memory" error when
1384 reading the file.
1385- Make sure the 'binary' option is set BEFORE loading the
1386 file. Otherwise both <CR> <NL> and <NL> are considered to end a line
1387 and when the file is written the <NL> will be replaced with <CR> <NL>.
1388- <Nul> characters are shown on the screen as ^@. You can enter them with
1389 "CTRL-V CTRL-@" or "CTRL-V 000" {Vi cannot handle <Nul> characters in the
1390 file}
Bram Moolenaarfc39ecf2015-08-11 20:34:49 +02001391- To insert a <NL> character in the file split a line. When writing the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 buffer to a file a <NL> will be written for the <EOL>.
1393- Vim normally appends an <EOL> at the end of the file if there is none.
1394 Setting the 'binary' option prevents this. If you want to add the final
1395 <EOL>, set the 'endofline' option. You can also read the value of this
1396 option to see if there was an <EOL> for the last line (you cannot see this
1397 in the text).
1398
1399==============================================================================
14009. Encryption *encryption*
1401
1402Vim is able to write files encrypted, and read them back. The encrypted text
1403cannot be read without the right key.
Bram Moolenaar996343d2010-07-04 22:20:21 +02001404{only available when compiled with the |+cryptv| feature} *E833*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405
Bram Moolenaar81af9252010-12-10 20:35:50 +01001406The text in the swap file and the undo file is also encrypted. *E843*
Bram Moolenaar07d87792014-07-19 14:04:47 +02001407However, this is done block-by-block and may reduce the time needed to crack a
1408password. You can disable the swap file, but then a crash will cause you to
1409lose your work. The undo file can be disabled without much disadvantage. >
1410 :set noundofile
1411 :noswapfile edit secrets
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001412
1413Note: The text in memory is not encrypted. A system administrator may be able
1414to see your text while you are editing it. When filtering text with
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001415":!filter" or using ":w !command" the text is also not encrypted, this may
1416reveal it to others. The 'viminfo' file is not encrypted.
1417
1418You could do this to edit very secret text: >
1419 :set noundofile viminfo=
1420 :noswapfile edit secrets.txt
Bram Moolenaar88774fd2015-08-25 19:52:04 +02001421Keep in mind that without a swap file you risk losing your work in the event
Bram Moolenaarfc39ecf2015-08-11 20:34:49 +02001422of a crash or a power failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
1424WARNING: If you make a typo when entering the key and then write the file and
1425exit, the text will be lost!
1426
1427The normal way to work with encryption, is to use the ":X" command, which will
1428ask you to enter a key. A following write command will use that key to
1429encrypt the file. If you later edit the same file, Vim will ask you to enter
1430a key. If you type the same key as that was used for writing, the text will
1431be readable again. If you use a wrong key, it will be a mess.
1432
1433 *:X*
1434:X Prompt for an encryption key. The typing is done without showing the
1435 actual text, so that someone looking at the display won't see it.
1436 The typed key is stored in the 'key' option, which is used to encrypt
1437 the file when it is written. The file will remain unchanged until you
1438 write it. See also |-x|.
1439
1440The value of the 'key' options is used when text is written. When the option
1441is not empty, the written file will be encrypted, using the value as the
1442encryption key. A magic number is prepended, so that Vim can recognize that
1443the file is encrypted.
1444
1445To disable the encryption, reset the 'key' option to an empty value: >
1446 :set key=
1447
Bram Moolenaar49771f42010-07-20 17:32:38 +02001448You can use the 'cryptmethod' option to select the type of encryption, use one
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001449of these: >
1450 :setlocal cm=zip " weak method, backwards compatible
1451 :setlocal cm=blowfish " method with flaws
1452 :setlocal cm=blowfish2 " medium strong method
1453
Bram Moolenaar49771f42010-07-20 17:32:38 +02001454Do this before writing the file. When reading an encrypted file it will be
1455set automatically to the method used when that file was written. You can
1456change 'cryptmethod' before writing that file to change the method.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001457
Bram Moolenaarc2299672014-11-13 14:25:38 +01001458To set the default method, used for new files, use this in your |vimrc|
1459file: >
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001460 set cm=blowfish2
Bram Moolenaarc2299672014-11-13 14:25:38 +01001461Using "blowfish2" is highly recommended. Only use another method if you
1462must use an older Vim version that does not support it.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001463
Bram Moolenaar662db672011-03-22 14:05:35 +01001464The message given for reading and writing a file will show "[crypted]" when
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001465using zip, "[blowfish]" when using blowfish, etc.
Bram Moolenaar40e6a712010-05-16 22:32:54 +02001466
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001467When writing an undo file, the same key and method will be used for the text
1468in the undo file. |persistent-undo|.
1469
Bram Moolenaar85084ef2016-01-17 22:26:33 +01001470To test for blowfish support you can use these conditions: >
1471 has('crypt-blowfish')
1472 has('crypt-blowfish2')
1473This works since Vim 7.4.1099 while blowfish support was added earlier.
1474Thus the condition failing doesn't mean blowfish is not supported. You can
1475test for blowfish with: >
1476 v:version >= 703
1477And for blowfish2 with: >
1478 v:version > 704 || (v:version == 704 && has('patch401'))
Bram Moolenaar5e9b2fa2016-02-01 22:37:05 +01001479If you are sure Vim includes patch 7.4.237 a simpler check is: >
1480 has('patch-7.4.401')
Bram Moolenaar85084ef2016-01-17 22:26:33 +01001481<
Bram Moolenaarfa7584c2010-05-19 21:57:45 +02001482 *E817* *E818* *E819* *E820*
Bram Moolenaar0bbabe82010-05-17 20:32:55 +02001483When encryption does not work properly, you would be able to write your text
1484to a file and never be able to read it back. Therefore a test is performed to
1485check if the encryption works as expected. If you get one of these errors
1486don't write the file encrypted! You need to rebuild the Vim binary to fix
1487this.
1488
Bram Moolenaar46f9d492010-06-12 20:18:19 +02001489*E831* This is an internal error, "cannot happen". If you can reproduce it,
Bram Moolenaar56be9502010-06-06 14:20:26 +02001490please report to the developers.
1491
Bram Moolenaar0bbabe82010-05-17 20:32:55 +02001492When reading a file that has been encrypted and the 'key' option is not empty,
1493it will be used for decryption. If the value is empty, you will be prompted
1494to enter the key. If you don't enter a key, or you enter the wrong key, the
1495file is edited without being decrypted. There is no warning about using the
1496wrong key (this makes brute force methods to find the key more difficult).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497
1498If want to start reading a file that uses a different key, set the 'key'
1499option to an empty string, so that Vim will prompt for a new one. Don't use
1500the ":set" command to enter the value, other people can read the command over
1501your shoulder.
1502
1503Since the value of the 'key' option is supposed to be a secret, its value can
1504never be viewed. You should not set this option in a vimrc file.
1505
Bram Moolenaar60aad972010-07-21 20:36:22 +02001506An encrypted file can be recognized by the "file" command, if you add these
1507lines to "/etc/magic", "/usr/share/misc/magic" or wherever your system has the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508"magic" file: >
1509 0 string VimCrypt~ Vim encrypted file
Bram Moolenaarc095b282010-07-20 22:33:34 +02001510 >9 string 01 - "zip" cryptmethod
1511 >9 string 02 - "blowfish" cryptmethod
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001512 >9 string 03 - "blowfish2" cryptmethod
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513
1514Notes:
1515- Encryption is not possible when doing conversion with 'charconvert'.
1516- Text you copy or delete goes to the numbered registers. The registers can
1517 be saved in the .viminfo file, where they could be read. Change your
1518 'viminfo' option to be safe.
1519- Someone can type commands in Vim when you walk away for a moment, he should
1520 not be able to get the key.
1521- If you make a typing mistake when entering the key, you might not be able to
1522 get your text back!
1523- If you type the key with a ":set key=value" command, it can be kept in the
1524 history, showing the 'key' value in a viminfo file.
1525- There is never 100% safety. The encryption in Vim has not been tested for
1526 robustness.
Bram Moolenaar24ea3ba2010-09-19 19:01:21 +02001527- The algorithm used for 'cryptmethod' "zip" is breakable. A 4 character key
1528 in about one hour, a 6 character key in one day (on a Pentium 133 PC). This
1529 requires that you know some text that must appear in the file. An expert
1530 can break it for any key. When the text has been decrypted, this also means
1531 that the key can be revealed, and other files encrypted with the same key
1532 can be decrypted.
1533- Pkzip uses the same encryption as 'cryptmethod' "zip", and US Govt has no
1534 objection to its export. Pkzip's public file APPNOTE.TXT describes this
1535 algorithm in detail.
Bram Moolenaar3a991dd2014-10-02 01:41:41 +02001536- The implementation of 'cryptmethod' "blowfish" has a flaw. It is possible
1537 to crack the first 64 bytes of a file and in some circumstances more of the
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001538 file. Use of it is not recommended, but it's still the strongest method
1539 supported by Vim 7.3 and 7.4. The "zip" method is even weaker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540- Vim originates from the Netherlands. That is where the sources come from.
1541 Thus the encryption code is not exported from the USA.
1542
1543==============================================================================
154410. Timestamps *timestamp* *timestamps*
1545
Bram Moolenaare968e362014-05-13 20:23:24 +02001546Vim remembers the modification timestamp, mode and size of a file when you
1547begin editing it. This is used to avoid that you have two different versions
1548of the same file (without you knowing this).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549
Bram Moolenaare968e362014-05-13 20:23:24 +02001550After a shell command is run (|:!cmd| |suspend| |:read!| |K|) timestamps,
1551file modes and file sizes are compared for all buffers in a window. Vim will
1552run any associated |FileChangedShell| autocommands or display a warning for
1553any files that have changed. In the GUI this happens when Vim regains input
1554focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555
1556 *E321* *E462*
1557If you want to automatically reload a file when it has been changed outside of
1558Vim, set the 'autoread' option. This doesn't work at the moment you write the
1559file though, only when the file wasn't changed inside of Vim.
1560
1561Note that if a FileChangedShell autocommand is defined you will not get a
1562warning message or prompt. The autocommand is expected to handle this.
1563
Bram Moolenaar10de2da2005-01-27 14:33:00 +00001564There is no warning for a directory (e.g., with |netrw-browse|). But you do
1565get warned if you started editing a new file and it was created as a directory
1566later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567
1568When Vim notices the timestamp of a file has changed, and the file is being
1569edited in a buffer but has not changed, Vim checks if the contents of the file
1570is equal. This is done by reading the file again (into a hidden buffer, which
1571is immediately deleted again) and comparing the text. If the text is equal,
1572you will get no warning.
1573
1574If you don't get warned often enough you can use the following command.
1575
1576 *:checkt* *:checktime*
1577:checkt[ime] Check if any buffers were changed outside of Vim.
1578 This checks and warns you if you would end up with two
1579 versions of a file.
1580 If this is called from an autocommand, a ":global"
1581 command or is not typed the actual check is postponed
1582 until a moment the side effects (reloading the file)
1583 would be harmless.
1584 Each loaded buffer is checked for its associated file
1585 being changed. If the file was changed Vim will take
1586 action. If there are no changes in the buffer and
1587 'autoread' is set, the buffer is reloaded. Otherwise,
1588 you are offered the choice of reloading the file. If
1589 the file was deleted you get an error message.
1590 If the file previously didn't exist you get a warning
1591 if it exists now.
1592 Once a file has been checked the timestamp is reset,
1593 you will not be warned again.
1594
1595:[N]checkt[ime] {filename}
1596:[N]checkt[ime] [N]
1597 Check the timestamp of a specific buffer. The buffer
1598 may be specified by name, number or with a pattern.
1599
1600
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +01001601 *E813* *E814*
1602Vim will reload the buffer if you chose to. If a window is visible that
1603contains this buffer, the reloading will happen in the context of this window.
1604Otherwise a special window is used, so that most autocommands will work. You
1605can't close this window. A few other restrictions apply. Best is to make
1606sure nothing happens outside of the current buffer. E.g., setting
1607window-local options may end up in the wrong window. Splitting the window,
1608doing something there and closing it should be OK (if there are no side
1609effects from other autocommands). Closing unrelated windows and buffers will
1610get you into trouble.
1611
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612Before writing a file the timestamp is checked. If it has changed, Vim will
1613ask if you really want to overwrite the file:
1614
1615 WARNING: The file has been changed since reading it!!!
1616 Do you really want to write to it (y/n)?
1617
1618If you hit 'y' Vim will continue writing the file. If you hit 'n' the write is
1619aborted. If you used ":wq" or "ZZ" Vim will not exit, you will get another
1620chance to write the file.
1621
1622The message would normally mean that somebody has written to the file after
1623the edit session started. This could be another person, in which case you
1624probably want to check if your changes to the file and the changes from the
1625other person should be merged. Write the file under another name and check for
1626differences (the "diff" program can be used for this).
1627
1628It is also possible that you modified the file yourself, from another edit
1629session or with another command (e.g., a filter command). Then you will know
1630which version of the file you want to keep.
1631
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001632There is one situation where you get the message while there is nothing wrong:
1633On a Win32 system on the day daylight saving time starts. There is something
1634in the Win32 libraries that confuses Vim about the hour time difference. The
1635problem goes away the next day.
1636
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001637==============================================================================
163811. File Searching *file-searching*
1639
1640{not available when compiled without the |+path_extra| feature}
1641
1642The file searching is currently used for the 'path', 'cdpath' and 'tags'
Bram Moolenaarc236c162008-07-13 17:41:49 +00001643options, for |finddir()| and |findfile()|. Other commands use |wildcards|
1644which is slightly different.
Bram Moolenaar9964e462007-05-05 17:54:07 +00001645
1646There are three different types of searching:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001647
Bram Moolenaar02743632005-07-25 20:42:36 +000016481) Downward search: *starstar*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001649 Downward search uses the wildcards '*', '**' and possibly others
Bram Moolenaarc236c162008-07-13 17:41:49 +00001650 supported by your operating system. '*' and '**' are handled inside Vim,
1651 so they work on all operating systems. Note that "**" only acts as a
1652 special wildcard when it is at the start of a name.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001653
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00001654 The usage of '*' is quite simple: It matches 0 or more characters. In a
1655 search pattern this would be ".*". Note that the "." is not used for file
1656 searching.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001657
1658 '**' is more sophisticated:
1659 - It ONLY matches directories.
Bram Moolenaarc236c162008-07-13 17:41:49 +00001660 - It matches up to 30 directories deep by default, so you can use it to
1661 search an entire directory tree
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001662 - The maximum number of levels matched can be given by appending a number
1663 to '**'.
1664 Thus '/usr/**2' can match: >
1665 /usr
1666 /usr/include
1667 /usr/include/sys
1668 /usr/include/g++
1669 /usr/lib
1670 /usr/lib/X11
1671 ....
1672< It does NOT match '/usr/include/g++/std' as this would be three
1673 levels.
Bram Moolenaarc236c162008-07-13 17:41:49 +00001674 The allowed number range is 0 ('**0' is removed) to 100
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001675 If the given number is smaller than 0 it defaults to 30, if it's
Bram Moolenaarc236c162008-07-13 17:41:49 +00001676 bigger than 100 then 100 is used. The system also has a limit on the
1677 path length, usually 256 or 1024 bytes.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001678 - '**' can only be at the end of the path or be followed by a path
1679 separator or by a number and a path separator.
1680
1681 You can combine '*' and '**' in any order: >
1682 /usr/**/sys/*
Bram Moolenaarc236c162008-07-13 17:41:49 +00001683 /usr/*tory/sys/**
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001684 /usr/**2/sys/*
1685
16862) Upward search:
1687 Here you can give a directory and then search the directory tree upward for
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001688 a file. You could give stop-directories to limit the upward search. The
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001689 stop-directories are appended to the path (for the 'path' option) or to
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001690 the filename (for the 'tags' option) with a ';'. If you want several
1691 stop-directories separate them with ';'. If you want no stop-directory
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001692 ("search upward till the root directory) just use ';'. >
1693 /usr/include/sys;/usr
1694< will search in: >
1695 /usr/include/sys
1696 /usr/include
1697 /usr
1698<
1699 If you use a relative path the upward search is started in Vim's current
1700 directory or in the directory of the current file (if the relative path
1701 starts with './' and 'd' is not included in 'cpoptions').
1702
1703 If Vim's current path is /u/user_x/work/release and you do >
1704 :set path=include;/u/user_x
1705< and then search for a file with |gf| the file is searched in: >
1706 /u/user_x/work/release/include
1707 /u/user_x/work/include
1708 /u/user_x/include
1709
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000017103) Combined up/downward search:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001711 If Vim's current path is /u/user_x/work/release and you do >
1712 set path=**;/u/user_x
1713< and then search for a file with |gf| the file is searched in: >
1714 /u/user_x/work/release/**
1715 /u/user_x/work/**
1716 /u/user_x/**
1717<
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001718 BE CAREFUL! This might consume a lot of time, as the search of
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001719 '/u/user_x/**' includes '/u/user_x/work/**' and
1720 '/u/user_x/work/release/**'. So '/u/user_x/work/release/**' is searched
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001721 three times and '/u/user_x/work/**' is searched twice.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001722
1723 In the above example you might want to set path to: >
1724 :set path=**,/u/user_x/**
Bram Moolenaar162bd912010-07-28 22:29:10 +02001725< This searches:
1726 /u/user_x/work/release/** ~
1727 /u/user_x/** ~
1728 This searches the same directories, but in a different order.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001729
Bram Moolenaar162bd912010-07-28 22:29:10 +02001730 Note that completion for ":find", ":sfind", and ":tabfind" commands do not
1731 currently work with 'path' items that contain a url or use the double star
Bram Moolenaar5b435d62012-04-05 17:33:26 +02001732 with depth limiter (/usr/**2) or upward search (;) notations.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
1734 vim:tw=78:ts=8:ft=help:norl: