vim - 设置自动缩进以用空格或制表符填充前导空格
看起来如果我们启用 'ai',vim 会用 tabstop 填充前导空格。 我可以用“et”让它只填充空格。我不喜欢混有空格和制表符的 C 文件。
我的 vimrc:
set ts=4 et
set ai
set hlsearch
syntax on
filetype plugin indent on
autocmd FileType make setlocal noexpandtab
但是,在某些情况下,当我点击键盘上的“TAB”时,我确实需要输入制表符,例如,在 makefile 和其他一些文件中。 “autocmd FileType”命令不好:我无法在 vimrc 中添加每种文件类型。
我想要的很简单:
- 自动缩进以填充前导区域 空间;
- 当按键盘上的“TAB”时,制表符停止 输入,而不是空格(所以没有“et”)
怎么办?
It seems if we enable 'ai', vim will fill the the leading space with tabstop.
I can make it fill with just space with 'et'. I don't like a C file mixed with space and tabstop.
My vimrc:
set ts=4 et
set ai
set hlsearch
syntax on
filetype plugin indent on
autocmd FileType make setlocal noexpandtab
However, in some condition I do need to input tabstop when I hit the 'TAB' on keyboard, for example, in makefile and some others.
The 'autocmd FileType' command is not good: I can't add every file type in vimrc.
What I want is simple:
- autoindent to fill leading area with
space; - when hit 'TAB' on keyboard, tabstop
input, not space (so no 'et')
How to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我读得正确的话,它与@Lynch 的答案相同。
您还可以使用出于某种原因。
:这将插入
而不调用任何映射,并忽略 Expandtab,除非您重新映射;
或如果您只想插入选项卡,
它将忽略 Expandtab 设置。
It does the same as @Lynch answer if I read it correctly.
You can also use
<C-v><Tab>
: this will insert<Tab>
without invoking any mappings and ignores expandtab unless you remapped<C-v>
or<C-v><Tab>
for some reason.If you want to just insert tab do
It will ignore expandtab setting.
我用一个函数做到了。我测试了它,但也许在某些特定情况下你将不得不修复一些错误。尝试将其添加到您的 vimrc 中:
基本上,它会将可视模式下的键重新映射到函数 Inserttab()。另请注意,如果将 ts 更改为 4 以外的值,它仍会输出 4 个空格而不是两个,因为该值是硬编码的。
我对 vim 脚本也不是很熟悉,但我认为使用的所有变量都是全局的,这是一件坏事。
我忘了提及,要“查看”空格,您可以使用
set list
。您可以使用set nolist
禁用此功能。此外,在正常模式下,您可以使用 ga 来查看有关光标所在字符的信息。编辑
我意识到您可能想在行的开头插入制表符。我的脚本在开头插入空格并在其他位置插入制表符。
如果你真的想要每次点击 tab 键时都有一个制表符,你可以简单地使用这个:
但我不明白这一点,在这个版本中,你将永远无法从插入模式手动缩进。
I did it using a function. I tested it, but maybe in some particular case you will have to fix some bugs. Try adding this to your vimrc:
Basicaly it does remap your key in visual mode to the function Inserttab(). Also note that if you change ts for something other than 4 it will still output 4 spaces instead of two because the value is hard coded.
Also im not very familiar with vim scripts, but I think all the variables used will be global which is a bad thing.
I forgot to mention that to "see" white spaces you can use
set list
. You disable this withset nolist
. Also in normal mode you can usega
to see information about the character your cursor is on.Edit
I realise that you may want to insert tab at the beginin of the line. My script insert space at the begining and tab anywhere else.
If you really want a tab every time you hit tab key you could simply use this:
But I dont see the point, with this version you will never be able to indent manually from insert mode.
一种方法是
那么自动缩进将不会插入制表符,除非你达到 46 个空格,在这种情况下你可以输入更大的数字。
唯一需要拖动的是,如果其他人正在使用选项卡,那么您必须重置 ts 以同意您正在编辑的文件。另一方面,它将使选项卡立即显而易见,这也是可取的。
One way to do it is
Then autoindent will not insert tabs unless you reach 46 spaces, in which case you can put in a higher number.
Only drag about this is if someone else is using tabs, then you have to reset ts to agree with the file you are editing. On the other hand, it will make the tabs immediately obvious, which can be desirable as well.