简体   繁体   English

vim中的哪些设置抵消了smartindent拒绝在shell脚本中缩进#comment?

[英]What setting in vim counteracts smartindent's refusal to indent # comments in shell scripts?

I recently started using vim 7 (previously vim 6) and the smartindent setting. 我最近开始使用vim 7(之前的vim 6)和smartindent设置。 For the most part, it works well, though I'm so used to typing a tab after an open brace that it is almost counter-productive. 在大多数情况下,它运作良好,虽然我习惯在打开支撑后键入一个选项卡,这几乎适得其反。

However, there is one piece of maniacal behaviour. 然而,有一种疯狂的行为。 When editing a shell script, I try to create a comment at the current indent level, but smartindent will have nothing to do with it. 在编辑shell脚本时,我尝试在当前缩进级别创建注释,但smartindent将与它无关。 It insists that the comment must be at level 0 (no indent). 它坚持认为评论必须在0级(没有缩进)。 What's worse, it breaks shift-right (' >> ' and friends) so that they do not work. 更糟糕的是,它打破右移(' >> '和朋友),使他们无法正常工作。 This is outright insubordination, and I'd like to know what's the best way to fix it? 这是彻底的不服从,我想知道解决它的最佳方法是什么?

(I'm also not keen on smartindent 's ideas about indenting then after if .) (我也不喜欢smartindent关于缩进的想法then if 。)

Preferred solutions will save me manual bashing - I'm being lazy. 首选的解决方案将节省我手动抨击 - 我很懒。 One option would be 'turn off smartindent when editing shell scripts (leave it on for the rest)'. 一种选择是“在编辑shell脚本时关闭smartindent (其余部分保持打开状态)”。 Another option would be guidelines on how to find the control script for smartindent and what to edit to change the characteristics I don't like. 另一种选择是如何为smartindent找到控制脚本以及如何编辑以改变我不喜欢的特征的指南。 The final option (which I don't need advice on how to do - just the hint that it is the best, or only, way to restore sanity) is to leave smartindent unset. 最后的选择(我不需要关于如何做的建议 - 只是暗示它是最好的,或者唯一的,恢复理智的方式)是让smartindent设置。

I saw the vaguely related question on " (PHP and) annoying vim unindent rules "; 我看到了关于“ (PHP和)恼人的vim unindent规则 ”的模糊相关问题; it doesn't provide me with the direct answer, though maybe the cindent and related items mentioned in there are in fact part of the answer. 它没有给我直接的答案,尽管其中提到的cindent和相关项目实际上是答案的一部分。

Find the indent file, (eg /usr/share/vim/vim71/indent/sh.vim on my system) 找到缩进文件,(例如我系统上的/usr/share/vim/vim71/indent/sh.vim)

This line looks like the problem: 这条线看起来像问题:

setlocal indentkeys-=:,0#

Perhaps you can fix this in your .vimrc or load a custom indent file manually. 也许您可以在.vimrc中修复此问题或手动加载自定义缩进文件。

edit: It looks more complicated than I thought, but maybe there is something specifically set in the indenting file that you would need to fix. 编辑:它看起来比我想象的要复杂,但也许在缩进文件中有一些特别设置的东西你需要修复。

2nd edit: Looks like I was completely wrong, Check out: 第二次编辑:看起来我完全错了,看看:

Restoring indent after typing hash 键入哈希后恢复缩进

or 要么

howto-configure-vim-to-not-put-comments-at-the-beginning-of-lines-while-editing HOWTO-配置的vim -到-不把-意见,在最开始-的线,而编辑

Well, after exploring some options, including using ' :set cindent ' instead of ' :set smartindent ', I've ended up reverting to just using ' :set autoindent '. 好吧,在探索了一些选项后,包括使用' :set cindent '而不是' :set smartindent ',我最终还是回到了使用' :set autoindent '。 There probably are ways to make this stuff work exactly as I want it to, but it is messy enough and fiddly enough that I can't be bothered. 可能有一些方法可以让这些东西完全按照我想要的方式工作,但它足够混乱,而且非常繁琐,我不能被打扰。 I've worked fine with autoindent for the previous 20-odd years, and the benefits from the extra bells and whistles provided by smartindent are outweighed by the what I regard as its misbehaviour. 在过去的20多年里,我和autoindent合作得很好,而smartindent提供的额外铃声和口哨的好处被我认为的不当行为所抵消。

Thank you, Juan, for your assistance. 感谢Juan,感谢您的帮助。 Believe it or not, it did help - quite a lot. 信不信由你,它确实有所帮助 - 相当多。

I also discovered a couple of other neat commands, though, while following up on this: 不过,我还发现了其他一些简洁的命令,同时对此进行了跟进:

>i}
>a}

These right-shift the block of code you are in. The ' i ' version indents the body and not the closing braces (my preferred style), and the ' a ' version indents the closing braces to (the version that is required at work). 这些右移你在代码块。在“ i ”版本缩进身体,而不是结束括号(我喜欢的风格),和“ a ”版本缩进闭幕括号到(版本,在工作需要)。

Also, you can apply qualifiers to ' % ' in commands executed at the shell: 此外,您可以在shell中执行的命令中将限定符应用于' % ':

:make %:r.o

This would run make on the 'root' of the current file name (that's ' %:r ') followed by ' .o '. 这将在当前文件名(即' %:r ')的'根'上运行make,然后是' .o '。 Or, in other words, if I'm editing somefile.c , this executes make somefile.o . 或者,换句话说,如果我正在编辑somefile.c ,则执行make somefile.o

Add the line below in your .vimrc 在.vimrc中添加以下行

filetype indent on 文件类型缩进

(it will set the right indent mode depending on the filetype) (它将根据文件类型设置正确的缩进模式)

I had this same issue for a long time, until I realized that autoindent and smartindent are both unnecessary if "filetype indent on" is set in your vimrc - 'filetype indent on' uses the indent/sh.vim (or whatever language) file in your vim directory to figure out the indentation rules, and autoindent and smartindent both can interfere with it. 我有同样的问题很长一段时间,直到我意识到如果你的vimrc中设置了“filetype indent on”,autoindent和smartindent都是不必要的 - 'filetype indent on'使用indent / sh.vim(或任何语言)文件在你的vim目录中找出缩进规则,autoindent和smartindent都可以干扰它。

I haven't tested this with sh, but perl suddenly started behaving properly when I switched. 我没有用sh测试过这个,但是当我切换时perl突然开始表现正常。

Sidenote: Juan's redirect, "Restoring indent after typing hash", is not a good solution - while it does correct the problem in one situation (typing code in), it doesn't change how the editor thinks it should be indented, so a re-indent (visual =, or normal ==) will shove it back to the left. 旁注:Juan的重定向,“在输入哈希后恢复缩进”,不是一个好的解决方案 - 虽然它确实在一种情况下纠正了问题(键入代码),但它并没有改变编辑器认为它应该缩进的方式,所以a重新缩进(visual =,或= =)会将其推回到左侧。

The previous answer suggesting: 之前的回答表明:

:inoremap # X^H# :inoremap#X ^ H#

is excellent. 很棒。 It is the answer suggested by the VIM documentation at ":help smartindent". 这是VIM文档在“:help smartindent”中提出的答案。 Note that ^H is entered using CTRL-V CTRL-H. 请注意,使用CTRL-V CTRL-H输入^ H. The relevant section from the documentation is below. 文档中的相关部分如下。

    When typing '#' as the first character in a new line, the indent for
    that line is removed, the '#' is put in the first column.  The indent
    is restored for the next line.  If you don't want this, use this
    mapping: ":inoremap # X^H#", where ^H is entered with CTRL-V CTRL-H.
    When using the ">>" command, lines starting with '#' are not shifted
    right.

I have the following lines in my .vimrc and I don't observe the problem. 我的.vimrc中有以下几行,我没有注意到这个问题。

set smartindent
inoremap # X^H#

I used to have set autoindent after these two lines but it seems that it has no effect. 我曾经在这两行之后设置了autoindent ,但它似乎没有效果。

Yes that is very annoying. 是的,这烦人。 smartindent is really only for C like languages. smartindent实际上只适用于C语言。 See how I enable the appropriate indenting based on language at: http://www.pixelbeat.org/settings/.vimrc 了解如何根据以下语言启用适当的缩进: http//www.pixelbeat.org/settings/.vimrc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM