我可以在 Vim 中禁止将注释延续到下一行吗?

Nat*_*ong 80 vim

在 Vim 中,如果我在代码文件中输入注释,然后点击Enter,它也会自动使换行符成为注释。

例如,在一个 Ruby 文件中:

# I manually typed the pound at the start of this line and hit enter.
# This line formatted itself this way automatically.
Run Code Online (Sandbox Code Playgroud)

一般来说,这就是我想要的,但并非总是如此。如何暂时关闭此自动评论行为?

Mik*_*itz 105

我想你正在寻找

:set formatoptions-=cro
Run Code Online (Sandbox Code Playgroud)

来自:help fo-table

You can use the 'formatoptions' option  to influence how Vim formats text.
'formatoptions' is a string that can contain any of the letters below.  The
default setting is "tcq".  You can separate the option letters with commas for
readability.

letter  meaning when present in 'formatoptions'

t       Auto-wrap text using textwidth
c       Auto-wrap comments using textwidth, inserting the current comment
        leader automatically.
r       Automatically insert the current comment leader after hitting
        <Enter> in Insert mode.
o       Automatically insert the current comment leader after hitting 'o' or
        'O' in Normal mode.
...
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢!这种行为困扰着我——我从 unix vi 到 Linux vim。对于 googlers:对于 **永久使用**,另请参见 http://stackoverflow.com/questions/6076592/vim-set-formatoptions-being-lost 并将此行输入 /etc/vimrc:`autocmd BufNewFile,BufRead * setlocal formatoptions-=cro` (18认同)
  • 不起作用。z (3认同)
  • 没有帮助;输入 `/**&lt;ESC&gt;o` 仍然会在下一行添加一个星号。 (2认同)
  • 也没有为我工作。我相信问题在于 `-=cro` 仅在 `formatoptions` 包含完全按此顺序的标志时才有效。`set formatoptions-=c formatoptions-=r formatoptions-=o` 对我来说效果更好。 (2认同)

Hep*_*ite 12

临时设置 'paste' 选项可能会满足您的要求,但它也会禁用许多其他 Vim 功能:

使用:set paste来打开它,并:set nopaste把它关掉。或者,您可以使用:set paste!来切换它。

也可以看看:

:help 'paste'
:help 'pastetoggle'
Run Code Online (Sandbox Code Playgroud)

(这些命令是用单引号键入的。)

  • `:set paste` 打开它,`:set nopaste` 关闭它 (4认同)
  • 或者你可以使用 `:set paste!` 来切换它的 TRUE 和 FALSE (2认同)

小智 6

另一种选择是ctrl-w在您按下 后直接点击enter。这会在插入模式下删除前一个单词(例如#//)。事实上,ctrl-w在大多数应用程序中,这是一个非常通用的快捷方式。

  • &gt; 事实上,`ctrl-w` 在大多数应用程序中是一个非常通用的快捷方式,在我使用的大多数应用程序中它意味着“关闭窗口”(或选项卡)。jupyter 中的 vim 绑定很糟糕,例如 (3认同)

小智 5

我使用 . 输入非格式化的纯新行<CR>

当我想继续在注释块中输入下一行时,我只需O像往常一样使用该键。

尝试这个:

nnoremap <silent> <cr> :set paste<cr>o<esc>:set nopaste<cr>
Run Code Online (Sandbox Code Playgroud)