'!' 是什么意思?当它添加到 ex 命令时(:wq! | :w! | :q! )真的会做什么吗?

'!' 是什么意思?当它添加到 ex 命令时(:wq! | :w! | :q! )真的会做什么吗?

我明白这意味着力量做某事。

执行此操作时,$ 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.

但我还不明白的是,为什么即使文件的所有者是root用户,它也会发出跳过权限的指令,此外它还会更改所有者和组。

答案1

!通常意味着您对“强制”的期望,但它对于特定命令的含义取决于命令。在 的情况下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

这就是所有者和组发生变化的原因。保留权限 -: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.

如果你想让 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).

请注意,它说“目录必须可写” - 因为如果没有可写目录,Vim 既不能删除也不能创建新文件。

答案2

如 vim(1) 中所述,-R确保文件不会被偶然当用户盲目说出 时被覆盖:w,但不会禁用使用 进行写入:w!

但它只是一个应用程序,当要实际对文件执行某些操作时,操作系统内核无论如何都会检查权限。实验:我跑了

strace vim /etc/hostname 2>vim.out

在一个用户下。即使没有明确的说明,-R它也会以只读方式启动,因为它会查看权限。更换缓冲区后

W10: Warning: Changing a readonly file

出现了。

现在:w我得到了

E45: 'readonly' option is set (add ! to override)

我采纳了建议并

"/etc/hostname" E212: Can't open file for writing

可以预见的是,vim.out我们看到:

open("/etc/hostname", O_WRONLY|O_CREAT|O_TRUNC, 0644) = -1 EACCES (Permission denied)

答案3

FORCE,对于 :w 来说,它意味着“系统权限”,即使 -R 未设置,:q 意味着丢失unsaved工作

相关内容