【问题标题】:Vim: Align continous lines with spacesVim:将连续行与空格对齐
【发布时间】:2013-03-21 16:13:45
【问题描述】:

我想用制表符缩进 vim 中的所有内容,特殊情况除外。例如我有这个 c++ 代码(其中<tab> 是一个制表符系列,<s> 是一个空格字符系列):

<tab>if(true &&
<tab><s>true)
<tab>{
<tab><tab>//code here
<tab>}

我想在写完 '&&' 后按 'o' 跳到下一行并开始写,让 vim 放一个制表符和空格数直到 '(' 从前一行开始。

是否可以在vim中定义这种编码风格?

谢谢!

【问题讨论】:

    标签: c++ coding-style vi vim


    【解决方案1】:

    我认为您正在寻找的是(N 选项cinoptions。试试set cinoptions+=(0。根据文档,这看起来像您寻求的对齐方式。

    更多信息可以通过使用帮助命令::help cinoptions-values或查看help for cinoptions-values的在线版本。

    就制表符而言,您需要使用:set noexpandtab 禁用expandtab,并且您需要确保您的制表位、软制表位和移位宽度都已相应设置。例如,Linux 源代码使用您上面提到的样式,我的 vimrc 中有这个:

    setlocal ts=8 sts=8 sw=8 tw=80
    
    " Don't expand tabs to spaces.
    setlocal noexpandtab
    
    " Enable automatic C program indenting.
    setlocal cindent
    
    " Don't outdent function return types.
    setlocal cinoptions+=t0
    
    " No extra indentation for case labels.
    setlocal cinoptions+=:0
    
    " No extra indentation for "public", "protected", "private" labels.
    setlocal cinoptions+=g0
    
    " Line up function args.
    setlocal cinoptions+=(0
    
    " Setup formatoptions:
    "   c - auto-wrap comments to textwidth.
    "   r - automatically insert comment leader when pressing <Enter>.
    "   o - automatically insert comment leader after 'o' or 'O'.
    "   q - allow formatting of comments with 'gq'.
    "   l - long lines are not broken in insert mode.
    "   n - recognize numbered lists.
    "   t - autowrap using textwidth,
    setlocal formatoptions=croqlnt
    

    【讨论】:

    • 像魅力一样工作。多谢。也感谢您提供的信息,我会阅读手册以了解更多信息。
    【解决方案2】:

    在你的 .vimrc 中添加以下内容

    set tabstop=2
    set expandtab
    set shiftwidth=2
    set smarttab
    set linebreak
    set smartindent
    set cindent
    set autoindent
    

    这就是在 vim 中推出令人敬畏的一切所需的一切。 :)

    【讨论】:

    • 是我还是这个缩进到处都是空格?我应该给你看我的 vimrc 吗?它还可以与其他 c++ 块(例如 while 或 for)一起使用吗?
    • expandtab 告诉 Vim 使用空格而不是制表符;请改用noexpandtab。此外,当cindent 开启时,调用smartindent 无效,因此可以删除调用。
    • @MatthewStrawbridge:我显然专注于按照 John 的要求获得结果间距。
    • @kiddorails FWIW,我没有否决这个答案;这不是 OP 想要的一百万英里,但确实需要调整
    • 不,不只是你。他只是简单地复制了你的配置,甚至没有试图回答你的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多