缩进注释以匹配 vim 中的代码 [英] Indenting comments to match code in vim

查看:28
本文介绍了缩进注释以匹配 vim 中的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 vim 中完成我所有的编码并且对它非常满意(所以,请不要使用不同的编辑器"回应),但有一个持续的烦恼,因为 smartindent 功能不想缩进以 # 开头的评论全部.例如,我想要

I do all my coding in vim and am quite happy with it (so, please, no "use a different editor" responses), but have an ongoing annoyance in that the smartindent feature wants to not indent comments beginning with # at all. e.g., I want

  # Do something
  $x = $x + 1;
  if ($y) {
    # Do something else
    $y = $y + $z;
  }

而不是vim的首选

# Do something
  $x = $x + 1;
  if ($y) {
# Do something else
    $y = $y + $z;
  }

我能够阻止评论被发送到行首的唯一方法是在点击 # 之前插入和删除行上的一个字符(每次都必须记住这样做的麻烦)或转完全关闭 smartindent(在打开/关闭大括号时失去自动缩进增加/减少).

The only ways I have been able to prevent comments from being sent to the start of the line are to either insert and delete a character on the line before hitting # (a nuisance to have to remember to do every time) or turn off smartindent entirely (losing automatic indentation increase/decrease as I open/close braces).

如何设置 vim 以保持注释的缩进而不是将它们发送到行首?

How can I set vim to maintain my indentation for comments instead of sending them to the start of the line?

推荐答案

看起来您正在使用 Perl 进行编码.确保在您的 .vimrc 中设置以下内容:

It looks like you're coding in Perl. Ensure that the following are set in your .vimrc:

filetype plugin indent on
syntax enable

这些将告诉 Vim 在打开缓冲区时设置文件类型并配置缩进和语法突出显示.无需显式设置 smartindent,因为 Vim 包含的 Perl 语法文件会自动设置它(以及任何其他特定于 Perl 的自定义设置).

These will tell Vim to set the filetype when opening a buffer and configure the indentation and syntax highlighting. No need to explicitly set smartindent since Vim's included Perl syntax file will set it (and any other Perl-specific customizations) automatically.

注意:在~/.vimrc 中使用set smartindent 和/或set autoindent 可能会阻止解决方案工作.如果您遇到问题,请寻找它们.

Note: having either set smartindent and/or set autoindent in ~/.vimrc may prevent the solution from working. If you're having problems, look for them.

这篇关于缩进注释以匹配 vim 中的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆