在Vim的命令行中,在某些命令(如“:w!")之后使用感叹号(”!“)有什么用?

xa9qqrwz  于 5个月前  发布在  其他
关注(0)|答案(5)|浏览(115)

基本上我想知道之间的区别::w:w!:wq:wq!

y4ekin9u

y4ekin9u1#

了!限定符告诉Vim强制执行操作。例如,如果文件是只读的,你可以使用用途:w!来写入它。如果文件被修改,你想退出而不保存,你可以使用用途:q!。:wq!只是意味着强制写入并在一个命令中退出。

pqwbnv8z

pqwbnv8z2#

在你的例子中,感叹号意味着 * 强制 * 操作(例如,:w!将覆盖现有文件,:q!将退出而不保存)。
也就是说,根据命令还有许多其他用途,例如:

  • :set <option>!切换布尔选项,例如:set number!
  • !后跟一些shell命令,直接从编辑器执行该命令,例如:! ls /etc
  • :w !cmd将当前缓冲区的内容传输到命令cmd,例如:w !sudo tee %(也称为write with sudo trick)。
i86rm4rw

i86rm4rw3#

除了感叹号强制执行的情况,比如写,它会把一个命令变成一个切换命令。所以如果我这样做:

:set cursorline

字符串
光标所在的行将被高亮显示。我可以用以下命令将其关闭:

:set nocursorline


或者我可以这样做:

:set cursorline!


该命令在两个设置(关闭和打开)之间切换。
我经常关闭和打开光标行高亮显示,toggle命令让我通过一个简单的功能键Map来完成。如果没有toggle,我需要两个Map:一个打开它,另一个关闭它。或者我必须编写一个函数来确定光标行设置是打开还是关闭,然后打开相反的设置。
据我所知,这适用于所有具有打开和关闭设置的命令行设置,如hlsearch,paste,cursorcolumn,number,insearch等。
还请注意,感叹号将切换命令的 no 版本。例如,您还可以使用以下命令切换光标行设置:

:set nocursorline!

cbeh67ev

cbeh67ev4#

这真的取决于所考虑的命令。关于你列举的那些,它强制命令,因为其他人已经回答了你。
然而,还有其他命令,如:global:map:make:silent,.,其中bang(!)具有其他效果。阅读他们的文档:

:help help

字符串
(and我们可以在定义的命令中给予任何我们想要的含义)

f1tvaqid

f1tvaqid5#

| 例如|意义|
| --|--|
| :wq!:q!|无论如何都要做!|
| :autocmd! {group} {event} {pat} cmd个|覆盖特定的自动命令(:help autocmd-remove*)|
| :function!个|覆盖现有|
| :com[mand][!] [{attr}...] {cmd} {repl}个|覆盖现有|
| :set number!个|(用1覆盖0,或1 → 0)切换选项|
| :!ls个|shell命令|

:com[mand][!] [{attr}...] {cmd} {repl}
                        Define a user command.  The name of the command is
                        {cmd} and its replacement text is {repl}.  The
                        command's attributes (see below) are {attr}.  If the
                        command already exists, an error is reported, unless a
                        ! is specified, in which case the command is

个字符
......

Special cases
                                        :command-bang :command-bar
                                        :command-register :command-buffer
                                        :command-keepscript
There are some special cases as well:

        -bang       The command can take a ! modifier (like :q or :w)
        -bar        The command can be followed by a "|" and another command.
                    A "|" inside the command argument is not allowed then.
                    Also checks for a " to start a comment.

shell

| 例如|意义|
| --|--|
| #!/bin/sh|沙邦|
| 我的天!|最后历史|
| !2|倒数第二历史|

ipython和ptipython

| 例如|意义|
| --|--|
| :!ls个|shell命令|

相关问题