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


65

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

例如,在Ruby文件中:

# I manually typed the pound at the start of this line and hit enter.
# This line formatted itself this way automatically.

通常,这是我想要的,但并非总是如此。如何暂时关闭此自动注释行为?


暂时来说,您是要执行一次命令还是要设置一些选项,该选项将持续几个命令直到您将其重新打开?可能的跨站点重复:stackoverflow.com/questions/4896003/...
西罗桑蒂利新疆改造中心法轮功六四事件

Answers:


86

我想你在找

:set formatoptions-=cro

来自: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.
...

13
非常感谢!这种行为困扰着我-我从Unix vi到Linux vim。对于Google员工:要永久使用,还请参见stackoverflow.com/questions/6076592/…并将此行输入到/ etc / vimrc中:autocmd BufNewFile,BufRead * setlocal formatoptions-=cro
bgStack15

没有帮助;键入/**<ESC>o仍然在下一行添加星号。
2016年

不起作用 ž
felwithe

8

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

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

也可以看看:

:help 'paste'
:help 'pastetoggle'

(这些命令用单引号引起来。)


4
:set paste将其打开和:set nopaste关闭
用户

2
或者,您也可以使用:set paste!它来切换“真”和“假”
Felipe Alvarez 2014年


0

我结束了:

nnoremap <Leader>o o<Esc>^Da
nnoremap <Leader>O O<Esc>^Da

它将添加新行,删除已插入该行的所有内容,并使光标处于缩进列的插入模式,而不会弄乱格式选项。


0

这个答案适用于Debian及其一些派生工具。

在Debian发行版上,Vim的默认设置是不合理的。它们位于/usr/share/vim/vim80/defaults.vim(!)/etc/vim/vimrc运行之后并应用。告诉Vim不使用其默认值的唯一方法是~/.vimrc即使存在空白也要确保存在。Vim在启动时会尝试读取.vimrc,但是如果找不到该文件,它将使用默认值,这会带来很多不良行为,例如鼠标集成,复制粘贴怪癖,注释自动换行等。

在Debian上,您只需运行即可修复所有问题 touch ~/.vimrc

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.