Vim: Use tabs for indentation, spaces for alignment with C source files

佐手、 提交于 2019-12-22 04:49:08

问题


Does anybody have her vim setup in a way that uses hard tabs as indentation characters, but does use spaces for alignment? The problem I have is that when starting a continuation line like in

if (condition1 && (anotherlongcondition || /* <-- Here I insert a newline */
|-------|-------   whatever  /* some additional alignment added automatically */

, then cin (which is a must for me) adds some alignment just the way I prefer positionally, but this alignment is created using as much hard tabs as possible and filling the rest with spaces (as I tried to visualize).

So, in short, cin doesn't really seem to distinguish between indentation and alignment. I'd really like that all the added alignment in the example above is spaces. This way the alignment would be preserved correctly when switching ts temporarily.

To make it clear again, I'd like to be able to write the following code, never pressing <TAB> or <SPACE> in front of the first non-blank character in any line (and not doing any manual shifting or whatever):

void foo(int bar)
{
|-------somestatement;
|-------if (somecondition && (someothercondition ||
|-------                      whatevercomesnext))
|-------|-------dosomething;
}

I have already tried out ctab.vim, but it focuses on editing an aligned line with soft tabs, which seems silly to me because manual alignment is a task which affords 1-step refinement and not tab-width-step refinement. I did not change the way cin uses mixed tabs and spaces for alignment.

I have not managed to find any built-in way to accomplish that. Perhaps still, there is one? Anyway, I doubt that there's a plugin that does that. Although I admittedly don't vim-script myself and may not have enough experience, I must say that most plugins I tried out only messed up my editor configuration...


回答1:


In addition to your :set cino=(1, you may also be interested in the 'preserveindent' and 'copyindent' options if you've not encountered them already. They don't completely solve your problem, but they do go some way towards helping.




回答2:


Okay, sorry for the question. I've finally found some good material for it.

http://vim.1045645.n5.nabble.com/Indent-with-tabs-align-with-spaces-td1183279.html

To sum up, currently vim is not flexible enough for this to be done comfortably.

My workaround currently is using :set cinoptions=(1 which adds only one alignment unit when starting a continuation line. This way, I can be sure that the added alignment is a space (as long as I did not :set ts=1, at least) and add the nice amount of spaces manually. This is still ok in terms of speed and seems to be the least distracting behaviour to me!



来源:https://stackoverflow.com/questions/8493705/vim-use-tabs-for-indentation-spaces-for-alignment-with-c-source-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!