我明白这意味着强制做某事。
这样做时$ vim -R file我进入read-only模式,但这仅作为预防模式起作用
-R Readonly mode. The 'readonly' option will be set for all the
files being edited. You can still edit the buffer, but will
be prevented from accidentally overwriting a file. If you
forgot that you are in View mode and did make some changes,
you can overwrite a file by adding an exclamation mark to
the Ex command, as in ":w!". The 'readonly' option can be
reset with ":set noro" (see the options chapter, |options|).
Subsequent edits will not be done in readonly mode. Calling
the executable "view" has the same effect as the -R argument.
The 'updatecount' option will be set to 10000, meaning that
the swap file will not be updated automatically very often.
Run Code Online (Sandbox Code Playgroud)
但我还不能理解的是,即使文件的所有者是root用户,它也会指示跳过权限,此外它还更改所有者和组。
!通常意味着您对“强制”的期望,但对于特定命令的含义取决于命令。在 的情况下w!,如果 Vim 由于某种原因无法写入文件,它将尝试删除并使用当前缓冲区的内容创建一个新文件。
考虑以下示例(观察 inode 编号):
$ touch foo
$ chmod -w foo
$ stat foo
File: ‘foo’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 22h/34d Inode: 10396141 Links: 1
Access: (0444/-r--r--r--) Uid: ( 1000/ muru) Gid: ( 1000/ muru)
Access: 2015-09-10 00:24:28.259290486 +0530
Modify: 2015-09-10 00:24:28.259290486 +0530
Change: 2015-09-10 00:24:30.771263735 +0530
Birth: -
$ vim -c 'r!date' -c 'wq!' foo
$ stat foo
File: ‘foo’
Size: 30 Blocks: 8 IO Block: 4096 regular file
Device: 22h/34d Inode: 10396151 Links: 1
Access: (0444/-r--r--r--) Uid: ( 1000/ muru) Gid: ( 1000/ muru)
Access: 2015-09-10 00:24:37.727189657 +0530
Modify: 2015-09-10 00:24:37.731189614 +0530
Change: 2015-09-10 00:24:37.763189273 +0530
Birth: -
$ cat foo
Thu Sep 10 00:24:37 IST 2015
Run Code Online (Sandbox Code Playgroud)
这就是所有者和组更改的原因。权限被保留 - :h write-permissions:
write-permissions
When writing a new file the permissions are read-write. For unix the mask is
0666 with additionally umask applied. When writing a file that was read Vim
will preserve the permissions, but clear the s-bit.
Run Code Online (Sandbox Code Playgroud)
如果你想让 Vim 拒绝写入,请参阅:h write-readonly:
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).
Run Code Online (Sandbox Code Playgroud)
请注意,它说“目录必须是可写的”——因为没有可写的目录,Vim 既不能删除也不能创建新文件。