如何在 vim 中仅使用制表符(而不是空格)
我更喜欢使用 tab
而不是 空格
(可能与大多数其他人有点不同)
但是我发现,当我在行尾,它会添加一些空格,但不会添加制表符。所以,我必须删除它们并按tab。
我想知道如何将vim设置为:
- 仅使用tab来缩进一行制表
- 符看起来像4个空格,但实际上
- 在最后按
enter
时 是一个制表符一行中,新行仅以选项卡开始,
我已经在谷歌上搜索了一段时间,但没有找到一个好的答案。预先感谢您
更新
@Alok 提供的答案在大多数情况下效果很好。但我发现,有时,这取决于文件类型。例如,如果您正在编辑 haml
文件,并且 vimfiles/indent/
中有一个 haml.vim
,则所有选项卡将被转换为空格
。所以如果你希望它只是tab
,你应该修改(或删除)相应的缩进文件。
I prefer to use tab
than white space
(may be a little different from most of others)
But I found, when I hit Enter
at the end of line, it will add some white spaces, but not tab. So, I have to delete them and press tab.
I want to know how to set vim as:
- use only tab to indent the lines
- a tab looks like 4-spaces, but actually is a tab
- when hit
enter
at the end of a line, the new line is started with only tabs
I've googled for this for a while, but not found a good answer. Thank you in advance
UPDATE
The answer @Alok has provided works well in most of cases. But I just found, sometimes, it depends on the file type. For example, if you are editing a haml
file, and there is a haml.vim
in your vimfiles/indent/
, then all the tabs will be converted to space
. So if you want it to be tab
only, you should modify(or delete) the corresponding indent file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在寻找的设置是:
单行:
autoindent
可以替换为smartindent
或cindent
,具体取决于您的喜好。另请参阅文件类型插件缩进
。http://vim.wikia.com/wiki/Indenting_source_code
The settings you are looking for are:
As single line:
autoindent
can be replaced withsmartindent
orcindent
, depending upon your tastes. Also look atfiletype plugin indent on
.http://vim.wikia.com/wiki/Indenting_source_code
讨论迟到了,仅适用于那些努力强制使用选项卡的人。使用当前版本的 vim (>8) 需要设置:
扩展为:
我们还可以仅对特定文件类型使用此设置,利用
setlocal
:如需进一步参考,请参阅:https://vim.fandom。 com/wiki/Indent_with_tabs,_align_with_spaces
Late to the discussion, just for anyone who struggled to force the use of tabs. With the current version of vim (>8) it was required to set:
which expands to:
We can also use this setup with only specific file types, taking advantage of
setlocal
:For further reference, see: https://vim.fandom.com/wiki/Indent_with_tabs,_align_with_spaces
就我而言,
set indentexpr=
成功了。我发现配置文件使用
fd python.vim /
设置了烦人的行为。在/usr/share/vim/vim90/indent/python.vim
中有一行setlocal indentexpr=python#GetIndent(v:lnum)
。函数python#GetIndent
定义在/usr/share/vim/vim90/autoload/python.vim
中。该函数返回一个奇怪的值,该值不是shiftwidth
和tabstop
的倍数。因此 vim 插入了一个制表符,后跟一些空格。现在,我已将
indentexpr
设置为空值,我认为 vim 会退回到autoindent
。smartindent
和cindent
已关闭。 (https://vimhelp.org/indent.txt.html)当然
还需要设置 noexpandtab
。我不确定这里是否有任何其他相关设置。编辑:
autoindent
并不完美,它只不过将新行缩进与前一行一样多。无法按预期工作,并且
cindent
似乎不适合 python,因此使用indentexpr
毕竟是正确的方法。幸运的是,可以配置缩进功能,请参阅
help ft-python-indent
或查看代码 - 这提供了有关disable_parentheses_indenting 功能的更多信息。看来这对我来说是解决方案:(https://vi.stackexchange.com/a/38824/43863)
In my case
set indentexpr=
did the trick.I have found the config files which set the annoying behavior with
fd python.vim /
. In/usr/share/vim/vim90/indent/python.vim
there is the linesetlocal indentexpr=python#GetIndent(v:lnum)
. The functionpython#GetIndent
is defined in/usr/share/vim/vim90/autoload/python.vim
. This function returned a weird value which was not a multiple ofshiftwidth
andtabstop
. Therefore vim inserted a tab followed by some spaces.Now, that I have set
indentexpr
to an empty value, I think vim falls back toautoindent
.smartindent
andcindent
are turned off. (https://vimhelp.org/indent.txt.html)Of course
set noexpandtab
is required, too. I am not sure if any other settings are relevant here.EDIT:
autoindent
is not perfect, it does nothing more than indent the new line as much as the previous line.does not work as expected and
cindent
does not seem suitable for python, so usingindentexpr
is the way to go after all.Luckily the indentation function can be configured, see
help ft-python-indent
or look at the code – that was more informative regarding the disable_parentheses_indenting feature. It seems this is the solution for me:(https://vi.stackexchange.com/a/38824/43863)